2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 19:29:02 +00:00
books/backend/database/manager.ts

137 lines
3.2 KiB
TypeScript
Raw Normal View History

import fs from 'fs/promises';
import { DatabaseDemuxBase, DatabaseMethod } from 'utils/db/types';
import { getSchemas } from '../../schemas';
2022-03-31 07:33:58 +00:00
import { databaseMethodSet } from '../helpers';
import patches from '../patches';
import DatabaseCore from './core';
import { runPatches } from './runPatch';
import { Patch } from './types';
export class DatabaseManager extends DatabaseDemuxBase {
2022-03-25 10:12:39 +00:00
db?: DatabaseCore;
2022-03-31 07:18:32 +00:00
get #isInitialized(): boolean {
return this.db !== undefined && this.db.knex !== undefined;
}
getSchemaMap() {
return this.db?.schemaMap ?? {};
}
2022-03-31 07:18:32 +00:00
async createNewDatabase(dbPath: string, countryCode?: string) {
await this.#unlinkIfExists(dbPath);
2022-03-31 07:18:32 +00:00
await this.connectToDatabase(dbPath, countryCode);
}
async connectToDatabase(dbPath: string, countryCode?: string) {
this.db = new DatabaseCore(dbPath);
this.db.connect();
countryCode ??= await this.#getCountryCode();
const schemaMap = getSchemas(countryCode);
this.db.setSchemaMap(schemaMap);
2022-03-31 07:18:32 +00:00
await this.#migrate();
}
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;
}
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();
}
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();
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'
);
const executedPatches = query.map((q) => q.name);
return patches.filter((p) => !executedPatches.includes(p.name));
}
async #getCountryCode(): Promise<string | undefined> {
2022-03-25 10:12:39 +00:00
if (this.db === undefined) {
return undefined;
}
2022-03-31 07:18:32 +00:00
let query: { countryCode: string }[] = [];
try {
query = await this.db.knex!('SingleValue').where({
fieldname: 'countryCode',
parent: 'SystemSettings',
});
} catch {
// Database not inialized and no countryCode passed
}
if (query.length > 0) {
return query[0].countryCode as string;
}
return undefined;
}
async #unlinkIfExists(dbPath: string) {
try {
fs.unlink(dbPath);
} catch (err) {
2022-03-25 10:12:39 +00:00
if ((err as NodeJS.ErrnoException).code === 'ENOENT') {
return;
}
throw err;
}
}
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;
}
}
export default new DatabaseManager();