mirror of
https://github.com/frappe/books.git
synced 2024-11-09 23:30:56 +00:00
refactor: make getCountryCode a static db core function
This commit is contained in:
parent
cb7ac55357
commit
8ba8d7a284
@ -29,7 +29,7 @@ import { ColumnDiff, FieldValueMap, GetQueryBuilderOptions } from './types';
|
||||
*
|
||||
* 1. Init core: `const db = new DatabaseCore(dbPath)`.
|
||||
* 2. Connect db: `db.connect()`. This will allow for raw queries to be executed.
|
||||
* 3. Set schemas: `bb.setSchemaMap(schemaMap)`. This will allow for ORM functions to be executed.
|
||||
* 3. Set schemas: `db.setSchemaMap(schemaMap)`. This will allow for ORM functions to be executed.
|
||||
* 4. Migrate: `await db.migrate()`. This will create absent tables and update the tables' shape.
|
||||
* 5. ORM function execution: `db.get(...)`, `db.insert(...)`, etc.
|
||||
* 6. Close connection: `await db.close()`.
|
||||
@ -65,6 +65,29 @@ export default class DatabaseCore extends DatabaseBase {
|
||||
};
|
||||
}
|
||||
|
||||
static async getCountryCode(dbPath: string): Promise<string> {
|
||||
let countryCode = 'in';
|
||||
const db = new DatabaseCore(dbPath);
|
||||
db.connect();
|
||||
|
||||
let query: { countryCode: string }[] = [];
|
||||
try {
|
||||
query = await db.knex!('SingleValue').where({
|
||||
fieldname: 'countryCode',
|
||||
parent: 'SystemSettings',
|
||||
});
|
||||
} catch {
|
||||
// Database not inialized and no countryCode passed
|
||||
}
|
||||
|
||||
if (query.length > 0) {
|
||||
countryCode = query[0].countryCode as string;
|
||||
}
|
||||
|
||||
await db.close();
|
||||
return countryCode;
|
||||
}
|
||||
|
||||
setSchemaMap(schemaMap: SchemaMap) {
|
||||
this.schemaMap = schemaMap;
|
||||
}
|
||||
|
@ -25,10 +25,11 @@ export class DatabaseManager extends DatabaseDemuxBase {
|
||||
}
|
||||
|
||||
async connectToDatabase(dbPath: string, countryCode?: string) {
|
||||
countryCode ??= await DatabaseCore.getCountryCode(dbPath);
|
||||
|
||||
this.db = new DatabaseCore(dbPath);
|
||||
this.db.connect();
|
||||
|
||||
countryCode ??= await this.#getCountryCode();
|
||||
const schemaMap = getSchemas(countryCode);
|
||||
this.db.setSchemaMap(schemaMap);
|
||||
|
||||
@ -102,28 +103,6 @@ export class DatabaseManager extends DatabaseDemuxBase {
|
||||
return patches.filter((p) => !executedPatches.includes(p.name));
|
||||
}
|
||||
|
||||
async #getCountryCode(): Promise<string | undefined> {
|
||||
if (this.db === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let query: { countryCode: string }[] = [];
|
||||
try {
|
||||
query = await this.db.knex!('SingleValue').where({
|
||||
fieldname: 'countryCode',
|
||||
parent: 'SystemSettings',
|
||||
});
|
||||
} catch {
|
||||
// Database not inialized and no countryCode passed
|
||||
}
|
||||
|
||||
if (query.length > 0) {
|
||||
return query[0].countryCode as string;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
async #unlinkIfExists(dbPath: string) {
|
||||
try {
|
||||
fs.unlink(dbPath);
|
||||
|
Loading…
Reference in New Issue
Block a user