MOBILE-3977 core: Rename database table getters
parent
359d7ab5a5
commit
b8dbc9aa5b
|
@ -67,22 +67,22 @@ export class CoreDatabaseTableProxy<
|
|||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
async all(conditions?: Partial<DBRecord>): Promise<DBRecord[]> {
|
||||
return this.target.all(conditions);
|
||||
async getMany(conditions?: Partial<DBRecord>): Promise<DBRecord[]> {
|
||||
return this.target.getMany(conditions);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
async find(conditions: Partial<DBRecord>): Promise<DBRecord> {
|
||||
return this.target.find(conditions);
|
||||
async getOne(conditions: Partial<DBRecord>): Promise<DBRecord> {
|
||||
return this.target.getOne(conditions);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
async findByPrimaryKey(primaryKey: PrimaryKey): Promise<DBRecord> {
|
||||
return this.target.findByPrimaryKey(primaryKey);
|
||||
async getOneByPrimaryKey(primaryKey: PrimaryKey): Promise<DBRecord> {
|
||||
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.
|
||||
* @returns Database records.
|
||||
*/
|
||||
all(conditions?: Partial<DBRecord>): Promise<DBRecord[]> {
|
||||
getMany(conditions?: Partial<DBRecord>): Promise<DBRecord[]> {
|
||||
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<DBRecord>): Promise<DBRecord> {
|
||||
getOne(conditions: Partial<DBRecord>): Promise<DBRecord> {
|
||||
return this.database.getRecord<DBRecord>(this.tableName, conditions);
|
||||
}
|
||||
|
||||
|
@ -102,7 +102,7 @@ export class CoreDatabaseTable<
|
|||
* @param primaryKey Primary key.
|
||||
* @returns Database record.
|
||||
*/
|
||||
findByPrimaryKey(primaryKey: PrimaryKey): Promise<DBRecord> {
|
||||
getOneByPrimaryKey(primaryKey: PrimaryKey): Promise<DBRecord> {
|
||||
return this.database.getRecord<DBRecord>(this.tableName, primaryKey);
|
||||
}
|
||||
|
||||
|
|
|
@ -58,28 +58,28 @@ export class CoreDebugDatabaseTable<
|
|||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
all(conditions?: Partial<DBRecord>): Promise<DBRecord[]> {
|
||||
this.logger.log('all', conditions);
|
||||
getMany(conditions?: Partial<DBRecord>): Promise<DBRecord[]> {
|
||||
this.logger.log('getMany', conditions);
|
||||
|
||||
return this.target.all(conditions);
|
||||
return this.target.getMany(conditions);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
find(conditions: Partial<DBRecord>): Promise<DBRecord> {
|
||||
this.logger.log('find', conditions);
|
||||
getOne(conditions: Partial<DBRecord>): Promise<DBRecord> {
|
||||
this.logger.log('getOne', conditions);
|
||||
|
||||
return this.target.find(conditions);
|
||||
return this.target.getOne(conditions);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
findByPrimaryKey(primaryKey: PrimaryKey): Promise<DBRecord> {
|
||||
getOneByPrimaryKey(primaryKey: PrimaryKey): Promise<DBRecord> {
|
||||
this.logger.log('findByPrimaryKey', primaryKey);
|
||||
|
||||
return this.target.findByPrimaryKey(primaryKey);
|
||||
return this.target.getOneByPrimaryKey(primaryKey);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -34,7 +34,7 @@ export class CoreEagerDatabaseTable<
|
|||
* @inheritdoc
|
||||
*/
|
||||
async initialize(): Promise<void> {
|
||||
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<DBRecord>): Promise<DBRecord[]> {
|
||||
async getMany(conditions?: Partial<DBRecord>): Promise<DBRecord[]> {
|
||||
const records = Object.values(this.records);
|
||||
|
||||
return conditions
|
||||
|
@ -59,7 +59,7 @@ export class CoreEagerDatabaseTable<
|
|||
/**
|
||||
* @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;
|
||||
|
||||
if (record === null) {
|
||||
|
@ -72,7 +72,7 @@ export class CoreEagerDatabaseTable<
|
|||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
async findByPrimaryKey(primaryKey: PrimaryKey): Promise<DBRecord> {
|
||||
async getOneByPrimaryKey(primaryKey: PrimaryKey): Promise<DBRecord> {
|
||||
const record = this.records[this.serializePrimaryKey(primaryKey)] ?? null;
|
||||
|
||||
if (record === null) {
|
||||
|
|
|
@ -33,12 +33,12 @@ export class CoreLazyDatabaseTable<
|
|||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
async find(conditions: Partial<DBRecord>): Promise<DBRecord> {
|
||||
async getOne(conditions: Partial<DBRecord>): Promise<DBRecord> {
|
||||
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<DBRecord> {
|
||||
async getOneByPrimaryKey(primaryKey: PrimaryKey): Promise<DBRecord> {
|
||||
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;
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -78,10 +78,10 @@ async function testFindItems(records: User[], table: CoreDatabaseTable<User>) {
|
|||
|
||||
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<User>) {
|
||||
|
@ -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<User>) {
|
||||
|
@ -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<User>) {
|
||||
|
@ -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', () => {
|
||||
|
|
|
@ -89,7 +89,7 @@ export class CoreConfigProvider {
|
|||
*/
|
||||
async get<T>(name: string, defaultValue?: T): Promise<T> {
|
||||
try {
|
||||
const record = await this.table.findByPrimaryKey({ name });
|
||||
const record = await this.table.getOneByPrimaryKey({ name });
|
||||
|
||||
return record.value;
|
||||
} catch (error) {
|
||||
|
|
|
@ -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<CoreFilepoolLinksRecord>(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<CoreFilepoolFileEntry> {
|
||||
return this.filesTables[siteId].findByPrimaryKey({ fileId });
|
||||
return this.filesTables[siteId].getOneByPrimaryKey({ fileId });
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue