Merge branch 'main' into new-admin

This commit is contained in:
baldo 2022-07-21 12:07:57 +02:00
commit 8a5c8c332c
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"; import * as sqlite3 from "sqlite3";
export async function init(): Promise<void> { export async function init(): Promise<void> {

View file

@ -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};

View file

@ -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
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"; 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";