2022-05-20 07:40:21 +00:00
|
|
|
import { constants } from 'fs';
|
2022-03-24 13:13:59 +00:00
|
|
|
import fs from 'fs/promises';
|
2022-04-07 13:38:17 +00:00
|
|
|
import { DatabaseDemuxBase, DatabaseMethod } from 'utils/db/types';
|
2022-03-24 13:13:59 +00:00
|
|
|
import { getSchemas } from '../../schemas';
|
2022-03-31 07:33:58 +00:00
|
|
|
import { databaseMethodSet } from '../helpers';
|
2022-03-24 13:13:59 +00:00
|
|
|
import patches from '../patches';
|
2022-04-11 06:04:55 +00:00
|
|
|
import { BespokeQueries } from './bespoke';
|
2022-03-24 13:13:59 +00:00
|
|
|
import DatabaseCore from './core';
|
|
|
|
import { runPatches } from './runPatch';
|
2022-04-11 06:04:55 +00:00
|
|
|
import { BespokeFunction, Patch } from './types';
|
2022-03-24 13:13:59 +00:00
|
|
|
|
2022-04-07 13:38:17 +00:00
|
|
|
export class DatabaseManager extends DatabaseDemuxBase {
|
2022-03-25 10:12:39 +00:00
|
|
|
db?: DatabaseCore;
|
2022-03-24 13:13:59 +00:00
|
|
|
|
2022-03-31 07:18:32 +00:00
|
|
|
get #isInitialized(): boolean {
|
|
|
|
return this.db !== undefined && this.db.knex !== undefined;
|
|
|
|
}
|
|
|
|
|
2022-03-31 12:04:30 +00:00
|
|
|
getSchemaMap() {
|
|
|
|
return this.db?.schemaMap ?? {};
|
|
|
|
}
|
|
|
|
|
2022-04-18 06:42:56 +00:00
|
|
|
async createNewDatabase(dbPath: string, countryCode: string) {
|
2022-03-24 13:13:59 +00:00
|
|
|
await this.#unlinkIfExists(dbPath);
|
2022-04-18 11:29:20 +00:00
|
|
|
return await this.connectToDatabase(dbPath, countryCode);
|
2022-03-24 13:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async connectToDatabase(dbPath: string, countryCode?: string) {
|
2022-04-16 07:25:36 +00:00
|
|
|
countryCode ??= await DatabaseCore.getCountryCode(dbPath);
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
this.db = new DatabaseCore(dbPath);
|
2022-05-20 11:12:32 +00:00
|
|
|
await this.db.connect();
|
2022-03-24 13:13:59 +00:00
|
|
|
|
|
|
|
const schemaMap = getSchemas(countryCode);
|
|
|
|
this.db.setSchemaMap(schemaMap);
|
|
|
|
|
2022-03-31 07:18:32 +00:00
|
|
|
await this.#migrate();
|
2022-04-18 06:42:56 +00:00
|
|
|
return countryCode;
|
2022-03-24 13:13:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 07:18:32 +00:00
|
|
|
async call(method: DatabaseMethod, ...args: unknown[]) {
|
|
|
|
if (!this.#isInitialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!databaseMethodSet.has(method)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
const response = await this.db[method](...args);
|
|
|
|
if (method === 'close') {
|
|
|
|
delete this.db;
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2022-04-11 06:04:55 +00:00
|
|
|
async callBespoke(method: string, ...args: unknown[]): Promise<unknown> {
|
|
|
|
if (!this.#isInitialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BespokeQueries.hasOwnProperty(method)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
const queryFunction: BespokeFunction = BespokeQueries[method];
|
|
|
|
return await queryFunction(this.db!, ...args);
|
|
|
|
}
|
|
|
|
|
2022-03-31 07:18:32 +00:00
|
|
|
async #migrate(): Promise<void> {
|
|
|
|
if (!this.#isInitialized) {
|
2022-03-25 10:12:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-31 07:18:32 +00:00
|
|
|
const isFirstRun = await this.#getIsFirstRun();
|
|
|
|
if (isFirstRun) {
|
|
|
|
await this.db!.migrate();
|
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
const patchesToExecute = await this.#getPatchesToExecute();
|
|
|
|
const preMigrationPatches = patchesToExecute.filter(
|
|
|
|
(p) => p.patch.beforeMigrate
|
|
|
|
);
|
|
|
|
const postMigrationPatches = patchesToExecute.filter(
|
|
|
|
(p) => !p.patch.beforeMigrate
|
|
|
|
);
|
|
|
|
|
|
|
|
await runPatches(preMigrationPatches, this);
|
2022-03-31 07:18:32 +00:00
|
|
|
await this.db!.migrate();
|
2022-03-24 13:13:59 +00:00
|
|
|
await runPatches(postMigrationPatches, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
async #getPatchesToExecute(): Promise<Patch[]> {
|
2022-03-25 10:12:39 +00:00
|
|
|
if (this.db === undefined) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const query: { name: string }[] = await this.db.knex!('PatchRun').select(
|
|
|
|
'name'
|
|
|
|
);
|
2022-03-24 13:13:59 +00:00
|
|
|
const executedPatches = query.map((q) => q.name);
|
|
|
|
return patches.filter((p) => !executedPatches.includes(p.name));
|
|
|
|
}
|
|
|
|
|
|
|
|
async #unlinkIfExists(dbPath: string) {
|
2022-05-20 07:40:21 +00:00
|
|
|
const exists = await fs
|
|
|
|
.access(dbPath, constants.W_OK)
|
|
|
|
.then(() => true)
|
|
|
|
.catch(() => false);
|
2022-03-24 13:13:59 +00:00
|
|
|
|
2022-05-20 07:40:21 +00:00
|
|
|
if (exists) {
|
|
|
|
fs.unlink(dbPath);
|
2022-03-24 13:13:59 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-31 07:18:32 +00:00
|
|
|
|
|
|
|
async #getIsFirstRun(): Promise<boolean> {
|
|
|
|
if (!this.#isInitialized) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tableList: unknown[] = await this.db!.knex!.raw(
|
|
|
|
"SELECT name FROM sqlite_master WHERE type='table'"
|
|
|
|
);
|
|
|
|
return tableList.length === 0;
|
|
|
|
}
|
2022-03-24 13:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new DatabaseManager();
|