mirror of
https://github.com/frappe/books.git
synced 2025-02-03 12:38:34 +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)`.
|
* 1. Init core: `const db = new DatabaseCore(dbPath)`.
|
||||||
* 2. Connect db: `db.connect()`. This will allow for raw queries to be executed.
|
* 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.
|
* 4. Migrate: `await db.migrate()`. This will create absent tables and update the tables' shape.
|
||||||
* 5. ORM function execution: `db.get(...)`, `db.insert(...)`, etc.
|
* 5. ORM function execution: `db.get(...)`, `db.insert(...)`, etc.
|
||||||
* 6. Close connection: `await db.close()`.
|
* 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) {
|
setSchemaMap(schemaMap: SchemaMap) {
|
||||||
this.schemaMap = schemaMap;
|
this.schemaMap = schemaMap;
|
||||||
}
|
}
|
||||||
|
@ -25,10 +25,11 @@ export class DatabaseManager extends DatabaseDemuxBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async connectToDatabase(dbPath: string, countryCode?: string) {
|
async connectToDatabase(dbPath: string, countryCode?: string) {
|
||||||
|
countryCode ??= await DatabaseCore.getCountryCode(dbPath);
|
||||||
|
|
||||||
this.db = new DatabaseCore(dbPath);
|
this.db = new DatabaseCore(dbPath);
|
||||||
this.db.connect();
|
this.db.connect();
|
||||||
|
|
||||||
countryCode ??= await this.#getCountryCode();
|
|
||||||
const schemaMap = getSchemas(countryCode);
|
const schemaMap = getSchemas(countryCode);
|
||||||
this.db.setSchemaMap(schemaMap);
|
this.db.setSchemaMap(schemaMap);
|
||||||
|
|
||||||
@ -102,28 +103,6 @@ export class DatabaseManager extends DatabaseDemuxBase {
|
|||||||
return patches.filter((p) => !executedPatches.includes(p.name));
|
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) {
|
async #unlinkIfExists(dbPath: string) {
|
||||||
try {
|
try {
|
||||||
fs.unlink(dbPath);
|
fs.unlink(dbPath);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user