Skip to content

Commit

Permalink
refactor: rewrite query without defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed May 19, 2023
1 parent 7ffff2f commit 9af6988
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/pg/pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,12 +485,23 @@ export class PgStore extends BasePgStore {
async getInscriptionCountPerBlock(
filters?: DbInscriptionCountPerBlockFilters
): Promise<DbInscriptionCountPerBlock[]> {
// 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<DbInscriptionCountPerBlock[]>`
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
Expand Down

0 comments on commit 9af6988

Please sign in to comment.