2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 03:19:01 +00:00
books/fyo/demux/db.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

83 lines
2.0 KiB
TypeScript
Raw Permalink Normal View History

2022-05-20 10:06:38 +00:00
import { DatabaseError, NotImplemented } from 'fyo/utils/errors';
import { SchemaMap } from 'schemas/types';
import { DatabaseDemuxBase, DatabaseMethod } from 'utils/db/types';
2022-07-30 11:47:37 +00:00
import { BackendResponse } from 'utils/ipc/types';
2022-03-31 09:04:30 +00:00
export class DatabaseDemux extends DatabaseDemuxBase {
#isElectron = false;
2022-03-31 09:04:30 +00:00
constructor(isElectron: boolean) {
super();
2022-03-31 09:04:30 +00:00
this.#isElectron = isElectron;
}
2022-07-30 11:47:37 +00:00
async #handleDBCall(func: () => Promise<BackendResponse>): Promise<unknown> {
2022-05-20 10:06:38 +00:00
const response = await func();
if (response.error?.name) {
const { name, message, stack } = response.error;
2022-05-20 11:12:32 +00:00
const dberror = new DatabaseError(`${name}\n${message}`);
2022-05-20 10:06:38 +00:00
dberror.stack = stack;
throw dberror;
}
2022-05-20 10:06:38 +00:00
return response.data;
}
async getSchemaMap(): Promise<SchemaMap> {
if (!this.#isElectron) {
throw new NotImplemented();
}
return (await this.#handleDBCall(async () => {
2023-07-10 09:12:20 +00:00
return await ipc.db.getSchema();
})) as SchemaMap;
}
async createNewDatabase(
dbPath: string,
countryCode?: string
): Promise<string> {
if (!this.#isElectron) {
throw new NotImplemented();
2022-03-31 09:04:30 +00:00
}
return (await this.#handleDBCall(async () => {
2023-07-10 09:12:20 +00:00
return ipc.db.create(dbPath, countryCode);
})) as string;
2022-03-31 09:04:30 +00:00
}
async connectToDatabase(
dbPath: string,
countryCode?: string
): Promise<string> {
if (!this.#isElectron) {
throw new NotImplemented();
2022-03-31 09:04:30 +00:00
}
return (await this.#handleDBCall(async () => {
2023-07-10 09:12:20 +00:00
return ipc.db.connect(dbPath, countryCode);
})) as string;
2022-03-31 09:04:30 +00:00
}
async call(method: DatabaseMethod, ...args: unknown[]): Promise<unknown> {
if (!this.#isElectron) {
throw new NotImplemented();
2022-03-31 09:04:30 +00:00
}
return await this.#handleDBCall(async () => {
2023-07-10 09:12:20 +00:00
return await ipc.db.call(method, ...args);
});
2022-03-31 09:04:30 +00:00
}
async callBespoke(method: string, ...args: unknown[]): Promise<unknown> {
if (!this.#isElectron) {
throw new NotImplemented();
}
return await this.#handleDBCall(async () => {
2023-07-10 09:12:20 +00:00
return await ipc.db.bespoke(method, ...args);
});
}
2022-03-31 09:04:30 +00:00
}