import { DatabaseError, NotImplemented } from 'fyo/utils/errors'; import { SchemaMap } from 'schemas/types'; import { DatabaseDemuxBase, DatabaseMethod } from 'utils/db/types'; import { BackendResponse } from 'utils/ipc/types'; export class DatabaseDemux extends DatabaseDemuxBase { #isElectron = false; constructor(isElectron: boolean) { super(); this.#isElectron = isElectron; } async #handleDBCall(func: () => Promise): Promise { const response = await func(); if (response.error?.name) { const { name, message, stack } = response.error; const dberror = new DatabaseError(`${name}\n${message}`); dberror.stack = stack; throw dberror; } return response.data; } async getSchemaMap(): Promise { if (!this.#isElectron) { throw new NotImplemented(); } return (await this.#handleDBCall(async () => { return await ipc.db.getSchema(); })) as SchemaMap; } async createNewDatabase( dbPath: string, countryCode?: string ): Promise { if (!this.#isElectron) { throw new NotImplemented(); } return (await this.#handleDBCall(async () => { return ipc.db.create(dbPath, countryCode); })) as string; } async connectToDatabase( dbPath: string, countryCode?: string ): Promise { if (!this.#isElectron) { throw new NotImplemented(); } return (await this.#handleDBCall(async () => { return ipc.db.connect(dbPath, countryCode); })) as string; } async call(method: DatabaseMethod, ...args: unknown[]): Promise { if (!this.#isElectron) { throw new NotImplemented(); } return await this.#handleDBCall(async () => { return await ipc.db.call(method, ...args); }); } async callBespoke(method: string, ...args: unknown[]): Promise { if (!this.#isElectron) { throw new NotImplemented(); } return await this.#handleDBCall(async () => { return await ipc.db.bespoke(method, ...args); }); } }