Extract database types into own file to fix tests.

This commit is contained in:
baldo 2022-07-21 12:07:18 +02:00
parent 8a2d108051
commit d76c530431
5 changed files with 57 additions and 53 deletions

View file

@ -1,4 +1,4 @@
import {RunResult, SqlType, Statement, TypedDatabase} from "../database";
import {RunResult, SqlType, Statement, TypedDatabase} from "../../types";
import * as sqlite3 from "sqlite3";
export async function init(): Promise<void> {

View file

@ -4,60 +4,13 @@ import glob from "glob";
import path from "path";
import {config} from "../config";
import Logger from "../logger";
import {Database, ISqlite, open, Statement} from "sqlite";
import {Database, open, Statement} from "sqlite";
import * as sqlite3 from "sqlite3";
import {RunResult, SqlType, TypedDatabase} from "../types";
const pglob = util.promisify(glob);
const pReadFile = util.promisify(fs.readFile);
export type RunResult = ISqlite.RunResult;
export type SqlType = ISqlite.SqlType;
export interface TypedDatabase {
/**
* @see Database.on
*/
on(event: string, listener: any): Promise<void>;
/**
* @see Database.run
*/
run(sql: SqlType, ...params: any[]): Promise<RunResult>;
/**
* @see Database.get
*/
get<T>(sql: SqlType, ...params: any[]): Promise<T | undefined>;
/**
* @see Database.each
*/
each<T>(sql: SqlType, callback: (err: any, row: T) => void): Promise<number>;
each<T>(sql: SqlType, param1: any, callback: (err: any, row: T) => void): Promise<number>;
each<T>(sql: SqlType, param1: any, param2: any, callback: (err: any, row: T) => void): Promise<number>;
each<T>(sql: SqlType, param1: any, param2: any, param3: any, callback: (err: any, row: T) => void): Promise<number>;
each<T>(sql: SqlType, ...params: any[]): Promise<number>;
/**
* @see Database.all
*/
all<T = never>(sql: SqlType, ...params: any[]): Promise<T[]>;
/**
* @see Database.exec
*/
exec(sql: SqlType, ...params: any[]): Promise<void>;
/**
* @see Database.prepare
*/
prepare(sql: SqlType, ...params: any[]): Promise<Statement>;
}
/**
* Typesafe database wrapper.
*
@ -174,5 +127,3 @@ export async function init(): Promise<void> {
throw error;
}
}
export {Statement};

View file

@ -3,7 +3,7 @@ import moment, {Moment, unitOfTime} from "moment";
import request from "request";
import {config} from "../config";
import {db, RunResult} from "../db/database";
import {db} from "../db/database";
import * as DatabaseUtil from "../utils/databaseUtil";
import ErrorTypes from "../utils/errorTypes";
import Logger from "../logger";
@ -29,6 +29,7 @@ import {
NodeId,
NodeStateData,
OnlineState,
RunResult,
Site,
UnixTimestampSeconds
} from "../types";

51
server/types/database.ts Normal file
View file

@ -0,0 +1,51 @@
import {ISqlite, Statement} from "sqlite";
export type RunResult = ISqlite.RunResult;
export type SqlType = ISqlite.SqlType;
export {Statement};
export interface TypedDatabase {
/**
* @see Database.on
*/
on(event: string, listener: any): Promise<void>;
/**
* @see Database.run
*/
run(sql: SqlType, ...params: any[]): Promise<RunResult>;
/**
* @see Database.get
*/
get<T>(sql: SqlType, ...params: any[]): Promise<T | undefined>;
/**
* @see Database.each
*/
each<T>(sql: SqlType, callback: (err: any, row: T) => void): Promise<number>;
each<T>(sql: SqlType, param1: any, callback: (err: any, row: T) => void): Promise<number>;
each<T>(sql: SqlType, param1: any, param2: any, callback: (err: any, row: T) => void): Promise<number>;
each<T>(sql: SqlType, param1: any, param2: any, param3: any, callback: (err: any, row: T) => void): Promise<number>;
each<T>(sql: SqlType, ...params: any[]): Promise<number>;
/**
* @see Database.all
*/
all<T = never>(sql: SqlType, ...params: any[]): Promise<T[]>;
/**
* @see Database.exec
*/
exec(sql: SqlType, ...params: any[]): Promise<void>;
/**
* @see Database.prepare
*/
prepare(sql: SqlType, ...params: any[]): Promise<Statement>;
}

View file

@ -1,6 +1,7 @@
import {Domain, EmailAddress, JSONObject, MonitoringToken, OnlineState, Site, toIsEnum} from "./shared";
export * from "./config";
export * from "./database";
export * from "./logger";
export * from "./shared";