From b8dbc9aa5b2c87fa0a7e419869969cdf1e456151 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Mon, 7 Feb 2022 14:02:29 +0100 Subject: [PATCH] MOBILE-3977 core: Rename database table getters --- .../classes/database/database-table-proxy.ts | 12 +++++------ src/core/classes/database/database-table.ts | 6 +++--- .../classes/database/debug-database-table.ts | 16 +++++++-------- .../classes/database/eager-database-table.ts | 8 ++++---- .../classes/database/lazy-database-table.ts | 8 ++++---- src/core/classes/site.ts | 6 +++--- src/core/classes/tests/database-table.test.ts | 20 +++++++++---------- src/core/services/config.ts | 2 +- src/core/services/filepool.ts | 6 +++--- 9 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/core/classes/database/database-table-proxy.ts b/src/core/classes/database/database-table-proxy.ts index 0a9006282..58393f943 100644 --- a/src/core/classes/database/database-table-proxy.ts +++ b/src/core/classes/database/database-table-proxy.ts @@ -67,22 +67,22 @@ export class CoreDatabaseTableProxy< /** * @inheritdoc */ - async all(conditions?: Partial): Promise { - return this.target.all(conditions); + async getMany(conditions?: Partial): Promise { + return this.target.getMany(conditions); } /** * @inheritdoc */ - async find(conditions: Partial): Promise { - return this.target.find(conditions); + async getOne(conditions: Partial): Promise { + return this.target.getOne(conditions); } /** * @inheritdoc */ - async findByPrimaryKey(primaryKey: PrimaryKey): Promise { - return this.target.findByPrimaryKey(primaryKey); + async getOneByPrimaryKey(primaryKey: PrimaryKey): Promise { + return this.target.getOneByPrimaryKey(primaryKey); } /** diff --git a/src/core/classes/database/database-table.ts b/src/core/classes/database/database-table.ts index 2bb34ff89..f018b062c 100644 --- a/src/core/classes/database/database-table.ts +++ b/src/core/classes/database/database-table.ts @@ -80,7 +80,7 @@ export class CoreDatabaseTable< * @param conditions Matching conditions. If this argument is missing, all records in the table will be returned. * @returns Database records. */ - all(conditions?: Partial): Promise { + getMany(conditions?: Partial): Promise { return conditions ? this.database.getRecords(this.tableName, conditions) : this.database.getAllRecords(this.tableName); @@ -92,7 +92,7 @@ export class CoreDatabaseTable< * @param conditions Matching conditions. * @returns Database record. */ - find(conditions: Partial): Promise { + getOne(conditions: Partial): Promise { return this.database.getRecord(this.tableName, conditions); } @@ -102,7 +102,7 @@ export class CoreDatabaseTable< * @param primaryKey Primary key. * @returns Database record. */ - findByPrimaryKey(primaryKey: PrimaryKey): Promise { + getOneByPrimaryKey(primaryKey: PrimaryKey): Promise { return this.database.getRecord(this.tableName, primaryKey); } diff --git a/src/core/classes/database/debug-database-table.ts b/src/core/classes/database/debug-database-table.ts index 09d80dad0..209c9abb1 100644 --- a/src/core/classes/database/debug-database-table.ts +++ b/src/core/classes/database/debug-database-table.ts @@ -58,28 +58,28 @@ export class CoreDebugDatabaseTable< /** * @inheritdoc */ - all(conditions?: Partial): Promise { - this.logger.log('all', conditions); + getMany(conditions?: Partial): Promise { + this.logger.log('getMany', conditions); - return this.target.all(conditions); + return this.target.getMany(conditions); } /** * @inheritdoc */ - find(conditions: Partial): Promise { - this.logger.log('find', conditions); + getOne(conditions: Partial): Promise { + this.logger.log('getOne', conditions); - return this.target.find(conditions); + return this.target.getOne(conditions); } /** * @inheritdoc */ - findByPrimaryKey(primaryKey: PrimaryKey): Promise { + getOneByPrimaryKey(primaryKey: PrimaryKey): Promise { this.logger.log('findByPrimaryKey', primaryKey); - return this.target.findByPrimaryKey(primaryKey); + return this.target.getOneByPrimaryKey(primaryKey); } /** diff --git a/src/core/classes/database/eager-database-table.ts b/src/core/classes/database/eager-database-table.ts index 7cc3cf53c..37b09342c 100644 --- a/src/core/classes/database/eager-database-table.ts +++ b/src/core/classes/database/eager-database-table.ts @@ -34,7 +34,7 @@ export class CoreEagerDatabaseTable< * @inheritdoc */ async initialize(): Promise { - const records = await super.all(); + const records = await super.getMany(); this.records = records.reduce((data, record) => { const primaryKey = this.serializePrimaryKey(this.getPrimaryKeyFromRecord(record)); @@ -48,7 +48,7 @@ export class CoreEagerDatabaseTable< /** * @inheritdoc */ - async all(conditions?: Partial): Promise { + async getMany(conditions?: Partial): Promise { const records = Object.values(this.records); return conditions @@ -59,7 +59,7 @@ export class CoreEagerDatabaseTable< /** * @inheritdoc */ - async find(conditions: Partial): Promise { + async getOne(conditions: Partial): Promise { const record = Object.values(this.records).find(record => this.recordMatches(record, conditions)) ?? null; if (record === null) { @@ -72,7 +72,7 @@ export class CoreEagerDatabaseTable< /** * @inheritdoc */ - async findByPrimaryKey(primaryKey: PrimaryKey): Promise { + async getOneByPrimaryKey(primaryKey: PrimaryKey): Promise { const record = this.records[this.serializePrimaryKey(primaryKey)] ?? null; if (record === null) { diff --git a/src/core/classes/database/lazy-database-table.ts b/src/core/classes/database/lazy-database-table.ts index cf64623a7..9ec890a20 100644 --- a/src/core/classes/database/lazy-database-table.ts +++ b/src/core/classes/database/lazy-database-table.ts @@ -33,12 +33,12 @@ export class CoreLazyDatabaseTable< /** * @inheritdoc */ - async find(conditions: Partial): Promise { + async getOne(conditions: Partial): Promise { let record: DBRecord | null = Object.values(this.records).find(record => record && this.recordMatches(record, conditions)) ?? null; if (!record) { - record = await super.find(conditions); + record = await super.getOne(conditions); this.records[this.serializePrimaryKey(this.getPrimaryKeyFromRecord(record))] = record; } @@ -49,12 +49,12 @@ export class CoreLazyDatabaseTable< /** * @inheritdoc */ - async findByPrimaryKey(primaryKey: PrimaryKey): Promise { + async getOneByPrimaryKey(primaryKey: PrimaryKey): Promise { const serializePrimaryKey = this.serializePrimaryKey(primaryKey); if (!(serializePrimaryKey in this.records)) { try { - const record = await super.findByPrimaryKey(primaryKey); + const record = await super.getOneByPrimaryKey(primaryKey); this.records[serializePrimaryKey] = record; diff --git a/src/core/classes/site.ts b/src/core/classes/site.ts index 4bcdf942e..d95590192 100644 --- a/src/core/classes/site.ts +++ b/src/core/classes/site.ts @@ -938,11 +938,11 @@ export class CoreSite { let entry: CoreSiteWSCacheRecord | undefined; if (preSets.getCacheUsingCacheKey || (emergency && preSets.getEmergencyCacheUsingCacheKey)) { - const entries = await this.cacheTable.all({ key: preSets.cacheKey }); + const entries = await this.cacheTable.getMany({ key: preSets.cacheKey }); if (!entries.length) { // Cache key not found, get by params sent. - entry = await this.cacheTable.findByPrimaryKey({ id }); + entry = await this.cacheTable.getOneByPrimaryKey({ id }); } else { if (entries.length > 1) { // More than one entry found. Search the one with same ID as this call. @@ -954,7 +954,7 @@ export class CoreSite { } } } else { - entry = await this.cacheTable.findByPrimaryKey({ id }); + entry = await this.cacheTable.getOneByPrimaryKey({ id }); } if (entry === undefined) { diff --git a/src/core/classes/tests/database-table.test.ts b/src/core/classes/tests/database-table.test.ts index 4d02603d2..9c7f1757f 100644 --- a/src/core/classes/tests/database-table.test.ts +++ b/src/core/classes/tests/database-table.test.ts @@ -78,10 +78,10 @@ async function testFindItems(records: User[], table: CoreDatabaseTable) { await table.initialize(); - await expect(table.findByPrimaryKey({ id: 1 })).resolves.toEqual(john); - await expect(table.findByPrimaryKey({ id: 2 })).resolves.toEqual(amy); - await expect(table.find({ surname: 'Doe', name: 'John' })).resolves.toEqual(john); - await expect(table.find({ surname: 'Doe', name: 'Amy' })).resolves.toEqual(amy); + await expect(table.getOneByPrimaryKey({ id: 1 })).resolves.toEqual(john); + await expect(table.getOneByPrimaryKey({ id: 2 })).resolves.toEqual(amy); + await expect(table.getOne({ surname: 'Doe', name: 'John' })).resolves.toEqual(john); + await expect(table.getOne({ surname: 'Doe', name: 'Amy' })).resolves.toEqual(amy); } async function testInsertItems(records: User[], database: SQLiteDB, table: CoreDatabaseTable) { @@ -96,7 +96,7 @@ async function testInsertItems(records: User[], database: SQLiteDB, table: CoreD // Assert. expect(database.insertRecord).toHaveBeenCalledWith('users', john); - await expect(table.findByPrimaryKey({ id: 1 })).resolves.toEqual(john); + await expect(table.getOneByPrimaryKey({ id: 1 })).resolves.toEqual(john); } async function testDeleteItems(records: User[], database: SQLiteDB, table: CoreDatabaseTable) { @@ -117,9 +117,9 @@ async function testDeleteItems(records: User[], database: SQLiteDB, table: CoreD // Assert. expect(database.deleteRecords).toHaveBeenCalledWith('users', { surname: 'Doe' }); - await expect(table.findByPrimaryKey({ id: 1 })).rejects.toThrow(); - await expect(table.findByPrimaryKey({ id: 2 })).rejects.toThrow(); - await expect(table.findByPrimaryKey({ id: 3 })).resolves.toEqual(jane); + await expect(table.getOneByPrimaryKey({ id: 1 })).rejects.toThrow(); + await expect(table.getOneByPrimaryKey({ id: 2 })).rejects.toThrow(); + await expect(table.getOneByPrimaryKey({ id: 3 })).resolves.toEqual(jane); } async function testDeleteItemsByPrimaryKey(records: User[], database: SQLiteDB, table: CoreDatabaseTable) { @@ -138,8 +138,8 @@ async function testDeleteItemsByPrimaryKey(records: User[], database: SQLiteDB, // Assert. expect(database.deleteRecords).toHaveBeenCalledWith('users', { id: 1 }); - await expect(table.findByPrimaryKey({ id: 1 })).rejects.toThrow(); - await expect(table.findByPrimaryKey({ id: 2 })).resolves.toEqual(amy); + await expect(table.getOneByPrimaryKey({ id: 1 })).rejects.toThrow(); + await expect(table.getOneByPrimaryKey({ id: 2 })).resolves.toEqual(amy); } describe('CoreDatabaseTable with eager caching', () => { diff --git a/src/core/services/config.ts b/src/core/services/config.ts index 0ed248301..ef0f65d5a 100644 --- a/src/core/services/config.ts +++ b/src/core/services/config.ts @@ -89,7 +89,7 @@ export class CoreConfigProvider { */ async get(name: string, defaultValue?: T): Promise { try { - const record = await this.table.findByPrimaryKey({ name }); + const record = await this.table.getOneByPrimaryKey({ name }); return record.value; } catch (error) { diff --git a/src/core/services/filepool.ts b/src/core/services/filepool.ts index 08bd6f078..b1d4c2380 100644 --- a/src/core/services/filepool.ts +++ b/src/core/services/filepool.ts @@ -586,7 +586,7 @@ export class CoreFilepoolProvider { const db = await CoreSites.getSiteDb(siteId); // Read the data first to be able to notify the deletions. - const filesEntries = await this.filesTables[siteId].all(); + const filesEntries = await this.filesTables[siteId].getMany(); const filesLinks = await db.getAllRecords(LINKS_TABLE_NAME); await Promise.all([ @@ -1427,7 +1427,7 @@ export class CoreFilepoolProvider { await Promise.all(items.map(async (item) => { try { - const fileEntry = await this.filesTables[siteId].findByPrimaryKey({ fileId: item.fileId }); + const fileEntry = await this.filesTables[siteId].getOneByPrimaryKey({ fileId: item.fileId }); if (!fileEntry) { return; @@ -2160,7 +2160,7 @@ export class CoreFilepoolProvider { * @return Resolved with file object from DB on success, rejected otherwise. */ protected async hasFileInPool(siteId: string, fileId: string): Promise { - return this.filesTables[siteId].findByPrimaryKey({ fileId }); + return this.filesTables[siteId].getOneByPrimaryKey({ fileId }); } /**