MOBILE-3981 core: Optimize app tables

main
Noel De Martin 2022-02-08 14:46:51 +01:00
parent a626930e72
commit df2388297d
1 changed files with 36 additions and 32 deletions

View File

@ -25,14 +25,9 @@ import { CoreColors } from '@singletons/colors';
import { DBNAME, SCHEMA_VERSIONS_TABLE_NAME, SCHEMA_VERSIONS_TABLE_SCHEMA, SchemaVersionsDBEntry } from '@services/database/app'; import { DBNAME, SCHEMA_VERSIONS_TABLE_NAME, SCHEMA_VERSIONS_TABLE_SCHEMA, SchemaVersionsDBEntry } from '@services/database/app';
import { CoreObject } from '@singletons/object'; import { CoreObject } from '@singletons/object';
import { CoreRedirectPayload } from './navigator'; import { CoreRedirectPayload } from './navigator';
import { CoreDatabaseCachingStrategy, CoreDatabaseTableProxy } from '@classes/database/database-table-proxy';
/** import { asyncInstance } from '../utils/async-instance';
* Object responsible of managing schema versions. import { CoreDatabaseTable } from '@classes/database/database-table';
*/
type SchemaVersionsManager = {
get(schemaName: string): Promise<number>;
set(schemaName: string, version: number): Promise<void>;
};
/** /**
* Factory to provide some global functionalities, like access to the global app database. * Factory to provide some global functionalities, like access to the global app database.
@ -58,13 +53,9 @@ export class CoreAppProvider {
protected keyboardClosing = false; protected keyboardClosing = false;
protected forceOffline = false; protected forceOffline = false;
protected redirect?: CoreRedirectData; protected redirect?: CoreRedirectData;
protected schemaVersionsTable = asyncInstance<CoreDatabaseTable<SchemaVersionsDBEntry, 'name'>>();
// Variables for DB.
protected schemaVersionsManager: Promise<SchemaVersionsManager>;
protected resolveSchemaVersionsManager!: (schemaVersionsManager: SchemaVersionsManager) => void;
constructor() { constructor() {
this.schemaVersionsManager = new Promise(resolve => this.resolveSchemaVersionsManager = resolve);
this.logger = CoreLogger.getInstance('CoreAppProvider'); this.logger = CoreLogger.getInstance('CoreAppProvider');
} }
@ -81,24 +72,20 @@ export class CoreAppProvider {
* Initialize database. * Initialize database.
*/ */
async initializeDatabase(): Promise<void> { async initializeDatabase(): Promise<void> {
await this.getDB().createTableFromSchema(SCHEMA_VERSIONS_TABLE_SCHEMA); const database = this.getDB();
this.resolveSchemaVersionsManager({ await database.createTableFromSchema(SCHEMA_VERSIONS_TABLE_SCHEMA);
get: async name => {
try {
// Fetch installed version of the schema.
const entry = await this.getDB().getRecord<SchemaVersionsDBEntry>(SCHEMA_VERSIONS_TABLE_NAME, { name });
return entry.version; const schemaVersionsTable = new CoreDatabaseTableProxy<SchemaVersionsDBEntry, 'name'>(
} catch (error) { { cachingStrategy: CoreDatabaseCachingStrategy.Eager },
// No installed version yet. database,
return 0; SCHEMA_VERSIONS_TABLE_NAME,
} ['name'],
}, );
set: async (name, version) => {
await this.getDB().insertRecord(SCHEMA_VERSIONS_TABLE_NAME, { name, version }); await schemaVersionsTable.initialize();
},
}); this.schemaVersionsTable.setInstance(schemaVersionsTable);
} }
/** /**
@ -137,8 +124,7 @@ export class CoreAppProvider {
async createTablesFromSchema(schema: CoreAppSchema): Promise<void> { async createTablesFromSchema(schema: CoreAppSchema): Promise<void> {
this.logger.debug(`Apply schema to app DB: ${schema.name}`); this.logger.debug(`Apply schema to app DB: ${schema.name}`);
const schemaVersionsManager = await this.schemaVersionsManager; const oldVersion = await this.getInstalledSchemaVersion(schema);
const oldVersion = await schemaVersionsManager.get(schema.name);
if (oldVersion >= schema.version) { if (oldVersion >= schema.version) {
// Version already installed, nothing else to do. // Version already installed, nothing else to do.
@ -155,7 +141,7 @@ export class CoreAppProvider {
} }
// Set installed version. // Set installed version.
schemaVersionsManager.set(schema.name, schema.version); await this.schemaVersionsTable.insert({ name: schema.name, version: schema.version });
} }
/** /**
@ -683,6 +669,24 @@ export class CoreAppProvider {
this.forceOffline = !!value; this.forceOffline = !!value;
} }
/**
* Get the installed version for the given schema.
*
* @param schema App schema.
* @returns Installed version number, or 0 if the schema is not installed.
*/
protected async getInstalledSchemaVersion(schema: CoreAppSchema): Promise<number> {
try {
// Fetch installed version of the schema.
const entry = await this.schemaVersionsTable.getOneByPrimaryKey({ name: schema.name });
return entry.version;
} catch (error) {
// No installed version yet.
return 0;
}
}
} }
export const CoreApp = makeSingleton(CoreAppProvider); export const CoreApp = makeSingleton(CoreAppProvider);