diff --git a/backend/database/manager.ts b/backend/database/manager.ts index 80519c79..33d7e434 100644 --- a/backend/database/manager.ts +++ b/backend/database/manager.ts @@ -14,6 +14,10 @@ export class DatabaseManager { return this.db !== undefined && this.db.knex !== undefined; } + getSchemaMap() { + return this.db?.schemaMap ?? {}; + } + async createNewDatabase(dbPath: string, countryCode?: string) { await this.#unlinkIfExists(dbPath); await this.connectToDatabase(dbPath, countryCode); diff --git a/frappe/core/dbHandler.ts b/frappe/core/dbHandler.ts index 4a20d117..5957b903 100644 --- a/frappe/core/dbHandler.ts +++ b/frappe/core/dbHandler.ts @@ -1,7 +1,6 @@ import { DatabaseDemux } from '@/demux/db'; import { Frappe } from 'frappe/core/frappe'; import Money from 'pesa/dist/types/src/money'; -import { getSchemas } from 'schemas'; import { FieldType, FieldTypeEnum, RawValue, SchemaMap } from 'schemas/types'; import { DatabaseBase, GetAllOptions } from 'utils/db/types'; import { DocValue, DocValueMap, RawValueMap, SingleValue } from './types'; @@ -9,7 +8,7 @@ import { DocValue, DocValueMap, RawValueMap, SingleValue } from './types'; export class DatabaseHandler extends DatabaseBase { #frappe: Frappe; #demux: DatabaseDemux; - // #schemaMap: Readonly; + schemaMap: Readonly = {}; constructor(frappe: Frappe) { super(); @@ -25,9 +24,8 @@ export class DatabaseHandler extends DatabaseBase { await this.#demux.connectToDatabase(dbPath, countryCode); } - init() { - // do nothing - // this.#schemaMap = getSchemas(); + async init() { + this.schemaMap = (await this.#demux.getSchemaMap()) as Readonly; } async insert( @@ -81,6 +79,9 @@ export class DatabaseHandler extends DatabaseBase { 'getSingleValues', ...fieldnames )) as SingleValue; + + // TODO: Complete this + throw new Error('Not implemented'); } // Update @@ -114,11 +115,17 @@ export class DatabaseHandler extends DatabaseBase { #toDocValueMap( schemaName: string, rawValueMap: RawValueMap | RawValueMap[] - ): DocValueMap | DocValueMap[] {} + ): DocValueMap | DocValueMap[] { + // TODO: Complete this + throw new Error('Not implemented'); + } #toRawValueMap( schemaName: string, docValueMap: DocValueMap | DocValueMap[] - ): RawValueMap | RawValueMap[] {} + ): RawValueMap | RawValueMap[] { + // TODO: Complete this + throw new Error('Not implemented'); + } #toDocValue(value: RawValue, fieldtype: FieldType): DocValue { switch (fieldtype) { diff --git a/frappe/core/frappe.ts b/frappe/core/frappe.ts index 8eb51342..a884b35d 100644 --- a/frappe/core/frappe.ts +++ b/frappe/core/frappe.ts @@ -64,7 +64,7 @@ export class Frappe { } async initializeAndRegister(customModels = {}, force = false) { - this.init(force); + await this.init(force); this.Meta = (await import('frappe/model/meta')).default; this.Document = (await import('frappe/model/document')).default; @@ -74,7 +74,7 @@ export class Frappe { this.doc.registerModels(customModels); } - init(force: boolean) { + async init(force: boolean) { if (this._initialized && !force) return; this.methods = {}; @@ -83,9 +83,9 @@ export class Frappe { // temp params while calling routes this.temp = {}; - this.doc.init(); - this.auth.init(); - this.db.init(); + await this.doc.init(); + await this.auth.init(); + await this.db.init(); this._initialized = true; } diff --git a/main/registerIpcMainActionListeners.ts b/main/registerIpcMainActionListeners.ts index dd7387ba..03b62b96 100644 --- a/main/registerIpcMainActionListeners.ts +++ b/main/registerIpcMainActionListeners.ts @@ -171,4 +171,10 @@ export default function registerIpcMainActionListeners(main: Main) { return response; } ); + + ipcMain.handle(IPC_ACTIONS.DB_SCHEMA, async (_) => { + const response: DatabaseResponse = { error: '', data: undefined }; + response.data = await databaseManager.getSchemaMap(); + return response; + }); } diff --git a/src/demux/db.ts b/src/demux/db.ts index 99119110..1fce6cf3 100644 --- a/src/demux/db.ts +++ b/src/demux/db.ts @@ -1,4 +1,5 @@ import { ipcRenderer } from 'electron'; +import { SchemaMap } from 'schemas/types'; import { DatabaseMethod } from 'utils/db/types'; import { IPC_ACTIONS } from 'utils/messages'; import { DatabaseResponse } from '../../utils/ipc/types'; @@ -9,6 +10,22 @@ export class DatabaseDemux { this.#isElectron = isElectron; } + async getSchemaMap(): Promise { + 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; + } + async createNewDatabase(dbPath: string, countryCode?: string): Promise { let response: DatabaseResponse; if (this.#isElectron) { diff --git a/utils/messages.ts b/utils/messages.ts index b3745872..d0fbdb55 100644 --- a/utils/messages.ts +++ b/utils/messages.ts @@ -32,6 +32,7 @@ export enum IPC_ACTIONS { DB_CREATE = 'db-create', DB_CONNECT = 'db-connect', DB_CALL = 'db-call', + DB_SCHEMA = 'db-schema', } // ipcMain.send(...)