From 0255f95e0745a92e1ac7210aab32dee5ee7f3bad Mon Sep 17 00:00:00 2001 From: ingalls Date: Fri, 15 Mar 2024 14:56:10 -0600 Subject: [PATCH] Update CHANGELOG --- CHANGELOG.md | 4 ++++ generic.ts | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fd8da0..286302a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ ## Version History +$## v17.1.0 + +- :rocket: Add generic `.count()` function + ### v17.0.1 - :arrow_up: Update Core Deps diff --git a/generic.ts b/generic.ts index f03f932..dbcb94f 100644 --- a/generic.ts +++ b/generic.ts @@ -38,6 +38,11 @@ export type GenericListInput = { where?: SQL; } +export type GenericCountInput = { + order?: GenericListOrder; + where?: SQL; +} + export type GenericStreamInput = { where?: SQL; } @@ -136,6 +141,15 @@ export default class Drizzle { return generic; } + async count(query: GenericCountInput = {}): Promise { + const pgres = await this.pool.select({ + count: sql`count(*)`.as('count'), + }).from(this.generic) + .where(query.where) + + return pgres[0].count; + } + async list(query: GenericListInput = {}): Promise>> { const order = query.order && query.order === 'desc' ? desc : asc; const orderBy = order(query.sort ? this.key(query.sort) : this.requiredPrimaryKey());