From cdf5c50cf3a95590a09ff5f50a308a6fdede2cc0 Mon Sep 17 00:00:00 2001 From: janniks Date: Fri, 19 May 2023 16:52:09 +0200 Subject: [PATCH] refactor: rewrite query without defaults --- src/pg/pg-store.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/pg/pg-store.ts b/src/pg/pg-store.ts index 612ef42b..c11d1806 100644 --- a/src/pg/pg-store.ts +++ b/src/pg/pg-store.ts @@ -485,12 +485,23 @@ export class PgStore extends BasePgStore { async getInscriptionCountPerBlock( filters?: DbInscriptionCountPerBlockFilters ): Promise { - // 9223372036854775807 is the max value for a bigint + const fromCondition = filters?.from_block_height + ? this.sql`block_height >= ${filters?.from_block_height}` + : this.sql``; + + const toCondition = filters?.to_block_height + ? this.sql`block_height <= ${filters?.to_block_height}` + : this.sql``; + + const where = + filters?.from_block_height && filters?.to_block_height + ? this.sql`WHERE ${fromCondition} AND ${toCondition}` + : this.sql`WHERE ${fromCondition}${toCondition}`; + return await this.sql` SELECT * FROM inscriptions_per_block - WHERE block_height >= ${filters?.from_block_height ?? '0'} - AND block_height <= ${filters?.to_block_height ?? '9223372036854775807'} + ${filters?.from_block_height || filters?.to_block_height ? where : this.sql``} ORDER BY block_height DESC LIMIT 10000 `; // roughly 70 days of blocks, assuming 10 minute block times on a full database