1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-19 00:15:43 +01:00

feat: crud time queries (#6895)

## About the changes
Add time metrics to relevant queries:
- get
- getAll
- bulkInsert
- count
- exists
- get

Ignored because might not be that relevant:
- insert
- delete
- deleteAll
- update
This commit is contained in:
Gastón Fournier 2024-04-22 09:31:37 +02:00 committed by GitHub
parent 126b78896e
commit ef91a5a8da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -61,11 +61,13 @@ export abstract class CRUDStore<
} }
async getAll(query?: Partial<InputModel>): Promise<OutputModel[]> { async getAll(query?: Partial<InputModel>): Promise<OutputModel[]> {
const endTimer = this.timer('getAll');
let allQuery = this.db(this.tableName); let allQuery = this.db(this.tableName);
if (query) { if (query) {
allQuery = allQuery.where(this.toRow(query) as Record<string, any>); allQuery = allQuery.where(this.toRow(query) as Record<string, any>);
} }
const items = await allQuery; const items = await allQuery;
endTimer();
return items.map(this.fromRow) as OutputModel[]; return items.map(this.fromRow) as OutputModel[];
} }
@ -80,9 +82,11 @@ export abstract class CRUDStore<
if (!items || items.length === 0) { if (!items || items.length === 0) {
return []; return [];
} }
const endTimer = this.timer('bulkInsert');
const rows = await this.db(this.tableName) const rows = await this.db(this.tableName)
.insert(items.map(this.toRow)) .insert(items.map(this.toRow))
.returning('*'); .returning('*');
endTimer();
return rows.map(this.fromRow) as OutputModel[]; return rows.map(this.fromRow) as OutputModel[];
} }
@ -105,15 +109,18 @@ export abstract class CRUDStore<
destroy(): void {} destroy(): void {}
async exists(id: IdType): Promise<boolean> { async exists(id: IdType): Promise<boolean> {
const endTimer = this.timer('exists');
const result = await this.db.raw( const result = await this.db.raw(
`SELECT EXISTS(SELECT 1 FROM ${this.tableName} WHERE id = ?) AS present`, `SELECT EXISTS(SELECT 1 FROM ${this.tableName} WHERE id = ?) AS present`,
[id], [id],
); );
const { present } = result.rows[0]; const { present } = result.rows[0];
endTimer();
return present; return present;
} }
async count(query?: Partial<InputModel>): Promise<number> { async count(query?: Partial<InputModel>): Promise<number> {
const endTimer = this.timer('count');
let countQuery = this.db(this.tableName).count('*'); let countQuery = this.db(this.tableName).count('*');
if (query) { if (query) {
countQuery = countQuery.where( countQuery = countQuery.where(
@ -121,11 +128,14 @@ export abstract class CRUDStore<
); );
} }
const { count } = (await countQuery.first()) ?? { count: 0 }; const { count } = (await countQuery.first()) ?? { count: 0 };
endTimer();
return Number(count); return Number(count);
} }
async get(id: IdType): Promise<OutputModel> { async get(id: IdType): Promise<OutputModel> {
const endTimer = this.timer('get');
const row = await this.db(this.tableName).where({ id }).first(); const row = await this.db(this.tableName).where({ id }).first();
endTimer();
if (!row) { if (!row) {
throw new NotFoundError(`No item with id ${id}`); throw new NotFoundError(`No item with id ${id}`);
} }