2
0
mirror of https://github.com/frappe/books.git synced 2024-09-19 19:19:02 +00:00
books/fyo/demux/db.ts
2023-07-10 14:42:20 +05:30

83 lines
2.0 KiB
TypeScript

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<BackendResponse>): Promise<unknown> {
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<SchemaMap> {
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<string> {
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<string> {
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<unknown> {
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<unknown> {
if (!this.#isElectron) {
throw new NotImplemented();
}
return await this.#handleDBCall(async () => {
return await ipc.db.bespoke(method, ...args);
});
}
}