From ef91a5a8daf301a76c9d26b31e6b8e8d1b11db69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Fournier?= Date: Mon, 22 Apr 2024 09:31:37 +0200 Subject: [PATCH] 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 --- src/lib/db/crud/crud-store.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/lib/db/crud/crud-store.ts b/src/lib/db/crud/crud-store.ts index 2c21bb891a..ff3482c879 100644 --- a/src/lib/db/crud/crud-store.ts +++ b/src/lib/db/crud/crud-store.ts @@ -61,11 +61,13 @@ export abstract class CRUDStore< } async getAll(query?: Partial): Promise { + const endTimer = this.timer('getAll'); let allQuery = this.db(this.tableName); if (query) { allQuery = allQuery.where(this.toRow(query) as Record); } const items = await allQuery; + endTimer(); return items.map(this.fromRow) as OutputModel[]; } @@ -80,9 +82,11 @@ export abstract class CRUDStore< if (!items || items.length === 0) { return []; } + const endTimer = this.timer('bulkInsert'); const rows = await this.db(this.tableName) .insert(items.map(this.toRow)) .returning('*'); + endTimer(); return rows.map(this.fromRow) as OutputModel[]; } @@ -105,15 +109,18 @@ export abstract class CRUDStore< destroy(): void {} async exists(id: IdType): Promise { + const endTimer = this.timer('exists'); const result = await this.db.raw( `SELECT EXISTS(SELECT 1 FROM ${this.tableName} WHERE id = ?) AS present`, [id], ); const { present } = result.rows[0]; + endTimer(); return present; } async count(query?: Partial): Promise { + const endTimer = this.timer('count'); let countQuery = this.db(this.tableName).count('*'); if (query) { countQuery = countQuery.where( @@ -121,11 +128,14 @@ export abstract class CRUDStore< ); } const { count } = (await countQuery.first()) ?? { count: 0 }; + endTimer(); return Number(count); } async get(id: IdType): Promise { + const endTimer = this.timer('get'); const row = await this.db(this.tableName).where({ id }).first(); + endTimer(); if (!row) { throw new NotFoundError(`No item with id ${id}`); }