2022-03-31 09:04:30 +00:00
|
|
|
import { ipcRenderer } from 'electron';
|
2022-03-31 12:04:30 +00:00
|
|
|
import { SchemaMap } from 'schemas/types';
|
2022-04-07 13:38:17 +00:00
|
|
|
import { DatabaseDemuxBase, DatabaseMethod } from 'utils/db/types';
|
2022-03-31 09:04:30 +00:00
|
|
|
import { IPC_ACTIONS } from 'utils/messages';
|
|
|
|
import { DatabaseResponse } from '../../utils/ipc/types';
|
|
|
|
|
2022-04-07 13:38:17 +00:00
|
|
|
export class DatabaseDemux extends DatabaseDemuxBase {
|
2022-03-31 09:04:30 +00:00
|
|
|
#isElectron: boolean = false;
|
|
|
|
constructor(isElectron: boolean) {
|
2022-04-07 13:38:17 +00:00
|
|
|
super();
|
2022-03-31 09:04:30 +00:00
|
|
|
this.#isElectron = isElectron;
|
|
|
|
}
|
|
|
|
|
2022-03-31 12:04:30 +00:00
|
|
|
async getSchemaMap(): Promise<SchemaMap> {
|
|
|
|
let response: DatabaseResponse;
|
|
|
|
if (this.#isElectron) {
|
|
|
|
response = await ipcRenderer.invoke(IPC_ACTIONS.DB_SCHEMA);
|
|
|
|
} else {
|
|
|
|
// TODO: API Call
|
|
|
|
response = { error: '', data: undefined };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.error) {
|
|
|
|
throw new Error(response.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.data as SchemaMap;
|
|
|
|
}
|
|
|
|
|
2022-03-31 09:04:30 +00:00
|
|
|
async createNewDatabase(dbPath: string, countryCode?: string): Promise<void> {
|
|
|
|
let response: DatabaseResponse;
|
|
|
|
if (this.#isElectron) {
|
|
|
|
response = await ipcRenderer.invoke(
|
|
|
|
IPC_ACTIONS.DB_CREATE,
|
|
|
|
dbPath,
|
|
|
|
countryCode
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// TODO: API Call
|
|
|
|
response = { error: '', data: undefined };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.error) {
|
|
|
|
throw new Error(response.error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async connectToDatabase(dbPath: string, countryCode?: string): Promise<void> {
|
|
|
|
let response: DatabaseResponse;
|
|
|
|
if (this.#isElectron) {
|
|
|
|
response = await ipcRenderer.invoke(
|
|
|
|
IPC_ACTIONS.DB_CONNECT,
|
|
|
|
dbPath,
|
|
|
|
countryCode
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// TODO: API Call
|
|
|
|
response = { error: '', data: undefined };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.error) {
|
|
|
|
throw new Error(response.error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async call(method: DatabaseMethod, ...args: unknown[]): Promise<unknown> {
|
|
|
|
let response: DatabaseResponse;
|
|
|
|
if (this.#isElectron) {
|
|
|
|
response = await ipcRenderer.invoke(IPC_ACTIONS.DB_CALL, method, ...args);
|
|
|
|
} else {
|
|
|
|
// TODO: API Call
|
|
|
|
response = { error: '', data: undefined };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.error) {
|
|
|
|
throw new Error(response.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.data;
|
|
|
|
}
|
|
|
|
}
|