MOBILE-3977 core: Rename database table getters
This commit is contained in:
		
							parent
							
								
									359d7ab5a5
								
							
						
					
					
						commit
						b8dbc9aa5b
					
				| @ -67,22 +67,22 @@ export class CoreDatabaseTableProxy< | |||||||
|     /** |     /** | ||||||
|      * @inheritdoc |      * @inheritdoc | ||||||
|      */ |      */ | ||||||
|     async all(conditions?: Partial<DBRecord>): Promise<DBRecord[]> { |     async getMany(conditions?: Partial<DBRecord>): Promise<DBRecord[]> { | ||||||
|         return this.target.all(conditions); |         return this.target.getMany(conditions); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * @inheritdoc |      * @inheritdoc | ||||||
|      */ |      */ | ||||||
|     async find(conditions: Partial<DBRecord>): Promise<DBRecord> { |     async getOne(conditions: Partial<DBRecord>): Promise<DBRecord> { | ||||||
|         return this.target.find(conditions); |         return this.target.getOne(conditions); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * @inheritdoc |      * @inheritdoc | ||||||
|      */ |      */ | ||||||
|     async findByPrimaryKey(primaryKey: PrimaryKey): Promise<DBRecord> { |     async getOneByPrimaryKey(primaryKey: PrimaryKey): Promise<DBRecord> { | ||||||
|         return this.target.findByPrimaryKey(primaryKey); |         return this.target.getOneByPrimaryKey(primaryKey); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|  | |||||||
| @ -80,7 +80,7 @@ export class CoreDatabaseTable< | |||||||
|      * @param conditions Matching conditions. If this argument is missing, all records in the table will be returned. |      * @param conditions Matching conditions. If this argument is missing, all records in the table will be returned. | ||||||
|      * @returns Database records. |      * @returns Database records. | ||||||
|      */ |      */ | ||||||
|     all(conditions?: Partial<DBRecord>): Promise<DBRecord[]> { |     getMany(conditions?: Partial<DBRecord>): Promise<DBRecord[]> { | ||||||
|         return conditions |         return conditions | ||||||
|             ? this.database.getRecords(this.tableName, conditions) |             ? this.database.getRecords(this.tableName, conditions) | ||||||
|             : this.database.getAllRecords(this.tableName); |             : this.database.getAllRecords(this.tableName); | ||||||
| @ -92,7 +92,7 @@ export class CoreDatabaseTable< | |||||||
|      * @param conditions Matching conditions. |      * @param conditions Matching conditions. | ||||||
|      * @returns Database record. |      * @returns Database record. | ||||||
|      */ |      */ | ||||||
|     find(conditions: Partial<DBRecord>): Promise<DBRecord> { |     getOne(conditions: Partial<DBRecord>): Promise<DBRecord> { | ||||||
|         return this.database.getRecord<DBRecord>(this.tableName, conditions); |         return this.database.getRecord<DBRecord>(this.tableName, conditions); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -102,7 +102,7 @@ export class CoreDatabaseTable< | |||||||
|      * @param primaryKey Primary key. |      * @param primaryKey Primary key. | ||||||
|      * @returns Database record. |      * @returns Database record. | ||||||
|      */ |      */ | ||||||
|     findByPrimaryKey(primaryKey: PrimaryKey): Promise<DBRecord> { |     getOneByPrimaryKey(primaryKey: PrimaryKey): Promise<DBRecord> { | ||||||
|         return this.database.getRecord<DBRecord>(this.tableName, primaryKey); |         return this.database.getRecord<DBRecord>(this.tableName, primaryKey); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -58,28 +58,28 @@ export class CoreDebugDatabaseTable< | |||||||
|     /** |     /** | ||||||
|      * @inheritdoc |      * @inheritdoc | ||||||
|      */ |      */ | ||||||
|     all(conditions?: Partial<DBRecord>): Promise<DBRecord[]> { |     getMany(conditions?: Partial<DBRecord>): Promise<DBRecord[]> { | ||||||
|         this.logger.log('all', conditions); |         this.logger.log('getMany', conditions); | ||||||
| 
 | 
 | ||||||
|         return this.target.all(conditions); |         return this.target.getMany(conditions); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * @inheritdoc |      * @inheritdoc | ||||||
|      */ |      */ | ||||||
|     find(conditions: Partial<DBRecord>): Promise<DBRecord> { |     getOne(conditions: Partial<DBRecord>): Promise<DBRecord> { | ||||||
|         this.logger.log('find', conditions); |         this.logger.log('getOne', conditions); | ||||||
| 
 | 
 | ||||||
|         return this.target.find(conditions); |         return this.target.getOne(conditions); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * @inheritdoc |      * @inheritdoc | ||||||
|      */ |      */ | ||||||
|     findByPrimaryKey(primaryKey: PrimaryKey): Promise<DBRecord> { |     getOneByPrimaryKey(primaryKey: PrimaryKey): Promise<DBRecord> { | ||||||
|         this.logger.log('findByPrimaryKey', primaryKey); |         this.logger.log('findByPrimaryKey', primaryKey); | ||||||
| 
 | 
 | ||||||
|         return this.target.findByPrimaryKey(primaryKey); |         return this.target.getOneByPrimaryKey(primaryKey); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|  | |||||||
| @ -34,7 +34,7 @@ export class CoreEagerDatabaseTable< | |||||||
|      * @inheritdoc |      * @inheritdoc | ||||||
|      */ |      */ | ||||||
|     async initialize(): Promise<void> { |     async initialize(): Promise<void> { | ||||||
|         const records = await super.all(); |         const records = await super.getMany(); | ||||||
| 
 | 
 | ||||||
|         this.records = records.reduce((data, record) => { |         this.records = records.reduce((data, record) => { | ||||||
|             const primaryKey = this.serializePrimaryKey(this.getPrimaryKeyFromRecord(record)); |             const primaryKey = this.serializePrimaryKey(this.getPrimaryKeyFromRecord(record)); | ||||||
| @ -48,7 +48,7 @@ export class CoreEagerDatabaseTable< | |||||||
|     /** |     /** | ||||||
|      * @inheritdoc |      * @inheritdoc | ||||||
|      */ |      */ | ||||||
|     async all(conditions?: Partial<DBRecord>): Promise<DBRecord[]> { |     async getMany(conditions?: Partial<DBRecord>): Promise<DBRecord[]> { | ||||||
|         const records = Object.values(this.records); |         const records = Object.values(this.records); | ||||||
| 
 | 
 | ||||||
|         return conditions |         return conditions | ||||||
| @ -59,7 +59,7 @@ export class CoreEagerDatabaseTable< | |||||||
|     /** |     /** | ||||||
|      * @inheritdoc |      * @inheritdoc | ||||||
|      */ |      */ | ||||||
|     async find(conditions: Partial<DBRecord>): Promise<DBRecord> { |     async getOne(conditions: Partial<DBRecord>): Promise<DBRecord> { | ||||||
|         const record = Object.values(this.records).find(record => this.recordMatches(record, conditions)) ?? null; |         const record = Object.values(this.records).find(record => this.recordMatches(record, conditions)) ?? null; | ||||||
| 
 | 
 | ||||||
|         if (record === null) { |         if (record === null) { | ||||||
| @ -72,7 +72,7 @@ export class CoreEagerDatabaseTable< | |||||||
|     /** |     /** | ||||||
|      * @inheritdoc |      * @inheritdoc | ||||||
|      */ |      */ | ||||||
|     async findByPrimaryKey(primaryKey: PrimaryKey): Promise<DBRecord> { |     async getOneByPrimaryKey(primaryKey: PrimaryKey): Promise<DBRecord> { | ||||||
|         const record = this.records[this.serializePrimaryKey(primaryKey)] ?? null; |         const record = this.records[this.serializePrimaryKey(primaryKey)] ?? null; | ||||||
| 
 | 
 | ||||||
|         if (record === null) { |         if (record === null) { | ||||||
|  | |||||||
| @ -33,12 +33,12 @@ export class CoreLazyDatabaseTable< | |||||||
|     /** |     /** | ||||||
|      * @inheritdoc |      * @inheritdoc | ||||||
|      */ |      */ | ||||||
|     async find(conditions: Partial<DBRecord>): Promise<DBRecord> { |     async getOne(conditions: Partial<DBRecord>): Promise<DBRecord> { | ||||||
|         let record: DBRecord | null = |         let record: DBRecord | null = | ||||||
|             Object.values(this.records).find(record => record && this.recordMatches(record, conditions)) ?? null; |             Object.values(this.records).find(record => record && this.recordMatches(record, conditions)) ?? null; | ||||||
| 
 | 
 | ||||||
|         if (!record) { |         if (!record) { | ||||||
|             record = await super.find(conditions); |             record = await super.getOne(conditions); | ||||||
| 
 | 
 | ||||||
|             this.records[this.serializePrimaryKey(this.getPrimaryKeyFromRecord(record))] = record; |             this.records[this.serializePrimaryKey(this.getPrimaryKeyFromRecord(record))] = record; | ||||||
|         } |         } | ||||||
| @ -49,12 +49,12 @@ export class CoreLazyDatabaseTable< | |||||||
|     /** |     /** | ||||||
|      * @inheritdoc |      * @inheritdoc | ||||||
|      */ |      */ | ||||||
|     async findByPrimaryKey(primaryKey: PrimaryKey): Promise<DBRecord> { |     async getOneByPrimaryKey(primaryKey: PrimaryKey): Promise<DBRecord> { | ||||||
|         const serializePrimaryKey = this.serializePrimaryKey(primaryKey); |         const serializePrimaryKey = this.serializePrimaryKey(primaryKey); | ||||||
| 
 | 
 | ||||||
|         if (!(serializePrimaryKey in this.records)) { |         if (!(serializePrimaryKey in this.records)) { | ||||||
|             try { |             try { | ||||||
|                 const record = await super.findByPrimaryKey(primaryKey); |                 const record = await super.getOneByPrimaryKey(primaryKey); | ||||||
| 
 | 
 | ||||||
|                 this.records[serializePrimaryKey] = record; |                 this.records[serializePrimaryKey] = record; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -938,11 +938,11 @@ export class CoreSite { | |||||||
|         let entry: CoreSiteWSCacheRecord | undefined; |         let entry: CoreSiteWSCacheRecord | undefined; | ||||||
| 
 | 
 | ||||||
|         if (preSets.getCacheUsingCacheKey || (emergency && preSets.getEmergencyCacheUsingCacheKey)) { |         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) { |             if (!entries.length) { | ||||||
|                 // Cache key not found, get by params sent.
 |                 // Cache key not found, get by params sent.
 | ||||||
|                 entry = await this.cacheTable.findByPrimaryKey({ id }); |                 entry = await this.cacheTable.getOneByPrimaryKey({ id }); | ||||||
|             } else { |             } else { | ||||||
|                 if (entries.length > 1) { |                 if (entries.length > 1) { | ||||||
|                     // More than one entry found. Search the one with same ID as this call.
 |                     // More than one entry found. Search the one with same ID as this call.
 | ||||||
| @ -954,7 +954,7 @@ export class CoreSite { | |||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } else { |         } else { | ||||||
|             entry = await this.cacheTable.findByPrimaryKey({ id }); |             entry = await this.cacheTable.getOneByPrimaryKey({ id }); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         if (entry === undefined) { |         if (entry === undefined) { | ||||||
|  | |||||||
| @ -78,10 +78,10 @@ async function testFindItems(records: User[], table: CoreDatabaseTable<User>) { | |||||||
| 
 | 
 | ||||||
|     await table.initialize(); |     await table.initialize(); | ||||||
| 
 | 
 | ||||||
|     await expect(table.findByPrimaryKey({ id: 1 })).resolves.toEqual(john); |     await expect(table.getOneByPrimaryKey({ id: 1 })).resolves.toEqual(john); | ||||||
|     await expect(table.findByPrimaryKey({ id: 2 })).resolves.toEqual(amy); |     await expect(table.getOneByPrimaryKey({ id: 2 })).resolves.toEqual(amy); | ||||||
|     await expect(table.find({ surname: 'Doe', name: 'John' })).resolves.toEqual(john); |     await expect(table.getOne({ surname: 'Doe', name: 'John' })).resolves.toEqual(john); | ||||||
|     await expect(table.find({ surname: 'Doe', name: 'Amy' })).resolves.toEqual(amy); |     await expect(table.getOne({ surname: 'Doe', name: 'Amy' })).resolves.toEqual(amy); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function testInsertItems(records: User[], database: SQLiteDB, table: CoreDatabaseTable<User>) { | async function testInsertItems(records: User[], database: SQLiteDB, table: CoreDatabaseTable<User>) { | ||||||
| @ -96,7 +96,7 @@ async function testInsertItems(records: User[], database: SQLiteDB, table: CoreD | |||||||
|     // Assert.
 |     // Assert.
 | ||||||
|     expect(database.insertRecord).toHaveBeenCalledWith('users', john); |     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<User>) { | async function testDeleteItems(records: User[], database: SQLiteDB, table: CoreDatabaseTable<User>) { | ||||||
| @ -117,9 +117,9 @@ async function testDeleteItems(records: User[], database: SQLiteDB, table: CoreD | |||||||
|     // Assert.
 |     // Assert.
 | ||||||
|     expect(database.deleteRecords).toHaveBeenCalledWith('users', { surname: 'Doe' }); |     expect(database.deleteRecords).toHaveBeenCalledWith('users', { surname: 'Doe' }); | ||||||
| 
 | 
 | ||||||
|     await expect(table.findByPrimaryKey({ id: 1 })).rejects.toThrow(); |     await expect(table.getOneByPrimaryKey({ id: 1 })).rejects.toThrow(); | ||||||
|     await expect(table.findByPrimaryKey({ id: 2 })).rejects.toThrow(); |     await expect(table.getOneByPrimaryKey({ id: 2 })).rejects.toThrow(); | ||||||
|     await expect(table.findByPrimaryKey({ id: 3 })).resolves.toEqual(jane); |     await expect(table.getOneByPrimaryKey({ id: 3 })).resolves.toEqual(jane); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function testDeleteItemsByPrimaryKey(records: User[], database: SQLiteDB, table: CoreDatabaseTable<User>) { | async function testDeleteItemsByPrimaryKey(records: User[], database: SQLiteDB, table: CoreDatabaseTable<User>) { | ||||||
| @ -138,8 +138,8 @@ async function testDeleteItemsByPrimaryKey(records: User[], database: SQLiteDB, | |||||||
|     // Assert.
 |     // Assert.
 | ||||||
|     expect(database.deleteRecords).toHaveBeenCalledWith('users', { id: 1 }); |     expect(database.deleteRecords).toHaveBeenCalledWith('users', { id: 1 }); | ||||||
| 
 | 
 | ||||||
|     await expect(table.findByPrimaryKey({ id: 1 })).rejects.toThrow(); |     await expect(table.getOneByPrimaryKey({ id: 1 })).rejects.toThrow(); | ||||||
|     await expect(table.findByPrimaryKey({ id: 2 })).resolves.toEqual(amy); |     await expect(table.getOneByPrimaryKey({ id: 2 })).resolves.toEqual(amy); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| describe('CoreDatabaseTable with eager caching', () => { | describe('CoreDatabaseTable with eager caching', () => { | ||||||
|  | |||||||
| @ -89,7 +89,7 @@ export class CoreConfigProvider { | |||||||
|      */ |      */ | ||||||
|     async get<T>(name: string, defaultValue?: T): Promise<T> { |     async get<T>(name: string, defaultValue?: T): Promise<T> { | ||||||
|         try { |         try { | ||||||
|             const record = await this.table.findByPrimaryKey({ name }); |             const record = await this.table.getOneByPrimaryKey({ name }); | ||||||
| 
 | 
 | ||||||
|             return record.value; |             return record.value; | ||||||
|         } catch (error) { |         } catch (error) { | ||||||
|  | |||||||
| @ -586,7 +586,7 @@ export class CoreFilepoolProvider { | |||||||
|         const db = await CoreSites.getSiteDb(siteId); |         const db = await CoreSites.getSiteDb(siteId); | ||||||
| 
 | 
 | ||||||
|         // Read the data first to be able to notify the deletions.
 |         // 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<CoreFilepoolLinksRecord>(LINKS_TABLE_NAME); |         const filesLinks = await db.getAllRecords<CoreFilepoolLinksRecord>(LINKS_TABLE_NAME); | ||||||
| 
 | 
 | ||||||
|         await Promise.all([ |         await Promise.all([ | ||||||
| @ -1427,7 +1427,7 @@ export class CoreFilepoolProvider { | |||||||
| 
 | 
 | ||||||
|         await Promise.all(items.map(async (item) => { |         await Promise.all(items.map(async (item) => { | ||||||
|             try { |             try { | ||||||
|                 const fileEntry = await this.filesTables[siteId].findByPrimaryKey({ fileId: item.fileId }); |                 const fileEntry = await this.filesTables[siteId].getOneByPrimaryKey({ fileId: item.fileId }); | ||||||
| 
 | 
 | ||||||
|                 if (!fileEntry) { |                 if (!fileEntry) { | ||||||
|                     return; |                     return; | ||||||
| @ -2160,7 +2160,7 @@ export class CoreFilepoolProvider { | |||||||
|      * @return Resolved with file object from DB on success, rejected otherwise. |      * @return Resolved with file object from DB on success, rejected otherwise. | ||||||
|      */ |      */ | ||||||
|     protected async hasFileInPool(siteId: string, fileId: string): Promise<CoreFilepoolFileEntry> { |     protected async hasFileInPool(siteId: string, fileId: string): Promise<CoreFilepoolFileEntry> { | ||||||
|         return this.filesTables[siteId].findByPrimaryKey({ fileId }); |         return this.filesTables[siteId].getOneByPrimaryKey({ fileId }); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user