We will create a ready assertion to retrieve articles from a specific vary of dates. To try this, we have to create a ready assertion with parameters.
PREPARE get_articles_between_dates(timestamptz, timestamptz) AS SELECT * FROM articles WHERE created_at >= $1 AND created_at < $2; |
Within the above instance, we specify two parameters. One is the beginning date, and the opposite is the top date.
Now, we will present the parameters when executing the ready assertion. Whereas doing that, protecting the right order of parameters is essential.
— Fetch articles from the beginning of December to the top of December EXECUTE get_articles_between_dates(‘2024-12-01’, ‘2025-01-01’);
— Fetch articles from final two months EXECUTE get_articles_between_dates(current_date – interval ‘2 months’, current_date + interval ‘1 day’); |
Ready statements with the Drizzle ORM
First, let’s run our question in an everyday means with out creating the ready assertion.
articles-service.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import { Injectable } from ‘@nestjs/frequent’; import { DrizzleService } from ‘../database/drizzle.service’; import { databaseSchema } from ‘../database/database-schema’; import { and, gte, lt, sql } from ‘drizzle-orm’;
@Injectable() export class ArticlesService { constructor(non-public readonly drizzleService: DrizzleService) {}
getAllFromYesterday() { return this.drizzleService.db .choose() .from(databaseSchema.articles) .the place( and( gte( databaseSchema.articles.createdAt, sql`current_date – interval ‘1 day’`, ), lt(databaseSchema.articles.createdAt, sql`current_date`), ), ); }
// … } |
To create a ready assertion with the Drizzle ORM, we have to name the
put together() operate. After we try this, we will use it with the
execute() operate.
articles-service.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import { Injectable } from ‘@nestjs/frequent’; import { DrizzleService } from ‘../database/drizzle.service’; import { databaseSchema } from ‘../database/database-schema’; import { and, gte, lt, sql } from ‘drizzle-orm’;
@Injectable() export class ArticlesService { constructor(non-public readonly drizzleService: DrizzleService) {}
non-public getArticlesFromYesterdayStatement = this.drizzleService.db .choose() .from(databaseSchema.articles) .the place( and( gte( databaseSchema.articles.createdAt, sql`current_date – interval ‘1 day’`, ), lt(databaseSchema.articles.createdAt, sql`current_date`), ), ) .put together(‘get_articles_from_yesterday’);
getAllFromYesterday() { return this.getArticlesFromYesterdayStatement.execute(); }
// … } |
Ready statements with parameters
To create a ready assertion with parameters whereas utilizing Drizzle ORM, we have to use the
sql.placeholder operate each time we need to use a parameter.
articles-service.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import { Injectable } from ‘@nestjs/frequent’; import { DrizzleService } from ‘../database/drizzle.service’; import { databaseSchema } from ‘../database/database-schema’; import { and, gte, lt, sql } from ‘drizzle-orm’;
@Injectable() export class ArticlesService { constructor(non-public readonly drizzleService: DrizzleService) {}
non-public getArticlesBetweenDatesStatement = this.drizzleService.db .choose() .from(databaseSchema.articles) .the place( and( gte(databaseSchema.articles.createdAt, sql.placeholder(‘startingDate’)), lt(databaseSchema.articles.createdAt, sql.placeholder(‘endingDate’)), ), ) .put together(‘get_articles_between_dates’);
// … } |
Now, we have to use the
execute() operate and supply the parameters utilizing the identical names we offered when declaring the ready assertion.
articles-service.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import { Injectable } from ‘@nestjs/frequent’; import { DrizzleService } from ‘../database/drizzle.service’; import { databaseSchema } from ‘../database/database-schema’; import { and, gte, lt, sql } from ‘drizzle-orm’;
@Injectable() export class ArticlesService { constructor(non-public readonly drizzleService: DrizzleService) {}
non-public getArticlesBetweenDatesStatement = this.drizzleService.db .choose() .from(databaseSchema.articles) .the place( and( gte(databaseSchema.articles.createdAt, sql.placeholder(‘startingDate’)), lt(databaseSchema.articles.createdAt, sql.placeholder(‘endingDate’)), ), ) .put together(‘get_articles_between_dates’);
getArticlesBetweenDates(startingDate: Date, endingDate: Date) { return this.getArticlesBetweenDatesStatement.execute({ startingDate, endingDate, }); }
// … } |
With the above method, we will use ready statements with parameters when utilizing the Drizzle ORM.
Abstract
On this article, we realized what ready statements are and tips on how to create them with PostgreSQL. Along with utilizing uncooked SQL, we created examples utilizing the Drizzle ORM and NestJS.
Ready statements are a precious device for optimizing our queries, but it surely doesn’t imply we must always all the time use them. They will enhance efficiency when executing numerous related queries, particularly if these queries are advanced and would in any other case require lots of time to parse, analyze, and plan. For instance, queries involving a number of joins can profit from ready statements.
Nevertheless, it’s vital to notice that ready statements don’t velocity up question execution. Making ready statements gained’t enhance efficiency if a question is easy to parse and analyze however takes a very long time to execute. Due to that, we must always use ready statements sparingly. By realizing when to make use of them, we will enhance question efficiency with out including pointless complexity.