From d76c53043112c85b2e73a9b9e02eee14f2db4c57 Mon Sep 17 00:00:00 2001 From: baldo Date: Thu, 21 Jul 2022 12:07:18 +0200 Subject: [PATCH] Extract database types into own file to fix tests. --- server/db/__mocks__/database.ts | 2 +- server/db/database.ts | 53 ++-------------------------- server/services/monitoringService.ts | 3 +- server/types/database.ts | 51 ++++++++++++++++++++++++++ server/types/index.ts | 1 + 5 files changed, 57 insertions(+), 53 deletions(-) create mode 100644 server/types/database.ts diff --git a/server/db/__mocks__/database.ts b/server/db/__mocks__/database.ts index 4345393..0becad7 100644 --- a/server/db/__mocks__/database.ts +++ b/server/db/__mocks__/database.ts @@ -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 { diff --git a/server/db/database.ts b/server/db/database.ts index 9e02eca..5d64028 100644 --- a/server/db/database.ts +++ b/server/db/database.ts @@ -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; - - /** - * @see Database.run - */ - run(sql: SqlType, ...params: any[]): Promise; - - /** - * @see Database.get - */ - get(sql: SqlType, ...params: any[]): Promise; - - /** - * @see Database.each - */ - each(sql: SqlType, callback: (err: any, row: T) => void): Promise; - - each(sql: SqlType, param1: any, callback: (err: any, row: T) => void): Promise; - - each(sql: SqlType, param1: any, param2: any, callback: (err: any, row: T) => void): Promise; - - each(sql: SqlType, param1: any, param2: any, param3: any, callback: (err: any, row: T) => void): Promise; - - each(sql: SqlType, ...params: any[]): Promise; - - /** - * @see Database.all - */ - all(sql: SqlType, ...params: any[]): Promise; - - /** - * @see Database.exec - */ - exec(sql: SqlType, ...params: any[]): Promise; - - /** - * @see Database.prepare - */ - prepare(sql: SqlType, ...params: any[]): Promise; -} - /** * Typesafe database wrapper. * @@ -174,5 +127,3 @@ export async function init(): Promise { throw error; } } - -export {Statement}; diff --git a/server/services/monitoringService.ts b/server/services/monitoringService.ts index 962f988..40a7c70 100644 --- a/server/services/monitoringService.ts +++ b/server/services/monitoringService.ts @@ -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"; diff --git a/server/types/database.ts b/server/types/database.ts new file mode 100644 index 0000000..a78d84e --- /dev/null +++ b/server/types/database.ts @@ -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; + + /** + * @see Database.run + */ + run(sql: SqlType, ...params: any[]): Promise; + + /** + * @see Database.get + */ + get(sql: SqlType, ...params: any[]): Promise; + + /** + * @see Database.each + */ + each(sql: SqlType, callback: (err: any, row: T) => void): Promise; + + each(sql: SqlType, param1: any, callback: (err: any, row: T) => void): Promise; + + each(sql: SqlType, param1: any, param2: any, callback: (err: any, row: T) => void): Promise; + + each(sql: SqlType, param1: any, param2: any, param3: any, callback: (err: any, row: T) => void): Promise; + + each(sql: SqlType, ...params: any[]): Promise; + + /** + * @see Database.all + */ + all(sql: SqlType, ...params: any[]): Promise; + + /** + * @see Database.exec + */ + exec(sql: SqlType, ...params: any[]): Promise; + + /** + * @see Database.prepare + */ + prepare(sql: SqlType, ...params: any[]): Promise; +} diff --git a/server/types/index.ts b/server/types/index.ts index d2d62ba..1c28d29 100644 --- a/server/types/index.ts +++ b/server/types/index.ts @@ -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";