From a04147120517a5018ec9e7acd7a7632998dd464a Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Thu, 3 Feb 2022 13:45:57 +0100 Subject: [PATCH] MOBILE-3977 core: Implement debug database table --- .../classes/database/database-table-proxy.ts | 6 +- .../classes/database/debug-database-table.ts | 139 ++++++++++++++++++ 2 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 src/core/classes/database/debug-database-table.ts diff --git a/src/core/classes/database/database-table-proxy.ts b/src/core/classes/database/database-table-proxy.ts index ef7e50b35..330640e9c 100644 --- a/src/core/classes/database/database-table-proxy.ts +++ b/src/core/classes/database/database-table-proxy.ts @@ -18,6 +18,7 @@ import { SQLiteDB, SQLiteDBRecordValues } from '@classes/sqlitedb'; import { CoreConfigProvider } from '@services/config'; import { CoreEventObserver, CoreEvents } from '@singletons/events'; import { CoreDatabaseReducer, CoreDatabaseTable, CoreDatabaseConditions, GetDBRecordPrimaryKey } from './database-table'; +import { CoreDebugDatabaseTable } from './debug-database-table'; import { CoreEagerDatabaseTable } from './eager-database-table'; import { CoreLazyDatabaseTable } from './lazy-database-table'; @@ -152,6 +153,7 @@ export class CoreDatabaseTableProxy< protected getConfigDefaults(): CoreDatabaseConfiguration { return { cachingStrategy: CoreDatabaseCachingStrategy.None, + debug: false, }; } @@ -193,8 +195,9 @@ export class CoreDatabaseTableProxy< */ protected createTarget(): CoreDatabaseTable { const config = this.getRuntimeConfig(); + const table = this.createTable(config.cachingStrategy); - return this.createTable(config.cachingStrategy); + return config.debug ? new CoreDebugDatabaseTable(table) : table; } /** @@ -221,6 +224,7 @@ export class CoreDatabaseTableProxy< */ export interface CoreDatabaseConfiguration { cachingStrategy: CoreDatabaseCachingStrategy; + debug: boolean; } /** diff --git a/src/core/classes/database/debug-database-table.ts b/src/core/classes/database/debug-database-table.ts new file mode 100644 index 000000000..09d80dad0 --- /dev/null +++ b/src/core/classes/database/debug-database-table.ts @@ -0,0 +1,139 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { SQLiteDBRecordValues } from '@classes/sqlitedb'; +import { CoreLogger } from '@singletons/logger'; +import { CoreDatabaseTable, CoreDatabaseConditions, GetDBRecordPrimaryKey, CoreDatabaseReducer } from './database-table'; + +/** + * Database table proxy used to debug runtime operations. + * + * This proxy should only be used for development purposes. + */ +export class CoreDebugDatabaseTable< + DBRecord extends SQLiteDBRecordValues = SQLiteDBRecordValues, + PrimaryKeyColumn extends keyof DBRecord = 'id', + PrimaryKey extends GetDBRecordPrimaryKey = GetDBRecordPrimaryKey +> extends CoreDatabaseTable { + + protected target: CoreDatabaseTable; + protected logger: CoreLogger; + + constructor(target: CoreDatabaseTable) { + super(target.getDatabase(), target.getTableName(), target.getPrimaryKeyColumns()); + + this.target = target; + this.logger = CoreLogger.getInstance(`CoreDatabase[${this.tableName}]`); + } + + /** + * @inheritdoc + */ + initialize(): Promise { + this.logger.log('initialize'); + + return this.target.initialize(); + } + + /** + * @inheritdoc + */ + destroy(): Promise { + this.logger.log('destroy'); + + return this.target.destroy(); + } + + /** + * @inheritdoc + */ + all(conditions?: Partial): Promise { + this.logger.log('all', conditions); + + return this.target.all(conditions); + } + + /** + * @inheritdoc + */ + find(conditions: Partial): Promise { + this.logger.log('find', conditions); + + return this.target.find(conditions); + } + + /** + * @inheritdoc + */ + findByPrimaryKey(primaryKey: PrimaryKey): Promise { + this.logger.log('findByPrimaryKey', primaryKey); + + return this.target.findByPrimaryKey(primaryKey); + } + + /** + * @inheritdoc + */ + reduce(reducer: CoreDatabaseReducer, conditions?: CoreDatabaseConditions): Promise { + this.logger.log('reduce', reducer, conditions); + + return this.target.reduce(reducer, conditions); + } + + /** + * @inheritdoc + */ + insert(record: DBRecord): Promise { + this.logger.log('insert', record); + + return this.target.insert(record); + } + + /** + * @inheritdoc + */ + update(updates: Partial, conditions?: Partial): Promise { + this.logger.log('update', updates, conditions); + + return this.target.update(updates, conditions); + } + + /** + * @inheritdoc + */ + updateWhere(updates: Partial, conditions: CoreDatabaseConditions): Promise { + this.logger.log('updateWhere', updates, conditions); + + return this.target.updateWhere(updates, conditions); + } + + /** + * @inheritdoc + */ + delete(conditions?: Partial): Promise { + this.logger.log('delete', conditions); + + return this.target.delete(conditions); + } + + /** + * @inheritdoc + */ + deleteByPrimaryKey(primaryKey: PrimaryKey): Promise { + this.logger.log('deleteByPrimaryKey', primaryKey); + + return this.target.deleteByPrimaryKey(primaryKey); + } + +}