2
0
mirror of https://github.com/frappe/books.git synced 2025-01-30 18:48:54 +00:00

incr: get schemaMap on db handler init

This commit is contained in:
18alantom 2022-03-31 17:34:30 +05:30
parent c9cac7410f
commit 98e1a44686
6 changed files with 47 additions and 12 deletions

View File

@ -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);

View File

@ -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>;
schemaMap: Readonly<SchemaMap> = {};
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<SchemaMap>;
}
async insert(
@ -81,6 +79,9 @@ export class DatabaseHandler extends DatabaseBase {
'getSingleValues',
...fieldnames
)) as SingleValue<RawValue>;
// 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) {

View File

@ -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;
}

View File

@ -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;
});
}

View File

@ -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<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;
}
async createNewDatabase(dbPath: string, countryCode?: string): Promise<void> {
let response: DatabaseResponse;
if (this.#isElectron) {

View File

@ -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(...)