Extract database types into own file to fix tests.
This commit is contained in:
parent
8a2d108051
commit
d76c530431
|
@ -1,4 +1,4 @@
|
||||||
import {RunResult, SqlType, Statement, TypedDatabase} from "../database";
|
import {RunResult, SqlType, Statement, TypedDatabase} from "../../types";
|
||||||
import * as sqlite3 from "sqlite3";
|
import * as sqlite3 from "sqlite3";
|
||||||
|
|
||||||
export async function init(): Promise<void> {
|
export async function init(): Promise<void> {
|
||||||
|
|
|
@ -4,60 +4,13 @@ import glob from "glob";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import {config} from "../config";
|
import {config} from "../config";
|
||||||
import Logger from "../logger";
|
import Logger from "../logger";
|
||||||
import {Database, ISqlite, open, Statement} from "sqlite";
|
import {Database, open, Statement} from "sqlite";
|
||||||
import * as sqlite3 from "sqlite3";
|
import * as sqlite3 from "sqlite3";
|
||||||
|
import {RunResult, SqlType, TypedDatabase} from "../types";
|
||||||
|
|
||||||
const pglob = util.promisify(glob);
|
const pglob = util.promisify(glob);
|
||||||
const pReadFile = util.promisify(fs.readFile);
|
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.
|
* Typesafe database wrapper.
|
||||||
*
|
*
|
||||||
|
@ -174,5 +127,3 @@ export async function init(): Promise<void> {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export {Statement};
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ import moment, {Moment, unitOfTime} from "moment";
|
||||||
import request from "request";
|
import request from "request";
|
||||||
|
|
||||||
import {config} from "../config";
|
import {config} from "../config";
|
||||||
import {db, RunResult} from "../db/database";
|
import {db} from "../db/database";
|
||||||
import * as DatabaseUtil from "../utils/databaseUtil";
|
import * as DatabaseUtil from "../utils/databaseUtil";
|
||||||
import ErrorTypes from "../utils/errorTypes";
|
import ErrorTypes from "../utils/errorTypes";
|
||||||
import Logger from "../logger";
|
import Logger from "../logger";
|
||||||
|
@ -29,6 +29,7 @@ import {
|
||||||
NodeId,
|
NodeId,
|
||||||
NodeStateData,
|
NodeStateData,
|
||||||
OnlineState,
|
OnlineState,
|
||||||
|
RunResult,
|
||||||
Site,
|
Site,
|
||||||
UnixTimestampSeconds
|
UnixTimestampSeconds
|
||||||
} from "../types";
|
} from "../types";
|
||||||
|
|
51
server/types/database.ts
Normal file
51
server/types/database.ts
Normal 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>;
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
import {Domain, EmailAddress, JSONObject, MonitoringToken, OnlineState, Site, toIsEnum} from "./shared";
|
import {Domain, EmailAddress, JSONObject, MonitoringToken, OnlineState, Site, toIsEnum} from "./shared";
|
||||||
|
|
||||||
export * from "./config";
|
export * from "./config";
|
||||||
|
export * from "./database";
|
||||||
export * from "./logger";
|
export * from "./logger";
|
||||||
export * from "./shared";
|
export * from "./shared";
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue