API with NestJS #160. Using views with the Drizzle ORM and PostgreSQL

JavaScript NestJS SQL

This entry is part 160 of 180 in the API with NestJS

Our SQL queries can become more complex as the application we develop grows. To deal with that, PostgreSQL implements views that act as virtual tables with rows and columns from which we can select. Additionally, we can create a cached version of our data using materialized views that store our virtual tables in the database. Unlike regular views, materialized views need to be refreshed to reflect changes in the underlying data.

In this article, we explore the idea of views using Drizzle ORM, PostgreSQL, and NestJS.

Views with the Drizzle ORM

Previously, in this series, we created a schema for storing the articles.

database-schema.ts

If you want to learn more about managing dates with the Drizzle ORM, check out API with NestJS #159. Date and time with PostgreSQL and the Drizzle ORM

Selecting articles scheduled today

To get all of the articles scheduled for today with PostgreSQL, we can use the variable.

We need to use raw SQL code to implement it with the Drizzle ORM.

articles.service.ts

Creating a view

Instead of the above, we can create a view using the function.

database-schema.ts

Unfortunately, the Drizzle Kit does not yet support views. Because of that, we need to add the flag when generating the migration and write it ourselves.

0009_create-articles-scheduled-for-today-view.sql

To run the above SQL code, we need to execute our migrations.

Thanks to that, we can use the view to simplify our code.

articles.service.ts

Materialized views

It’s crucial to notice that views resemble tables, but they are not stored in our database by default. We can demonstrate this using the command, which shows the execution plan.

When we select data from the view, the database queries all articles and applies the filters to find matching results. We can modify this behavior by using the function to create a materialized view instead.

database-schema.ts

We also need to take it into account when creating a migration.

0009_create-articles-scheduled-for-today-view.sql

Now, PostgreSQL will store a table with articles scheduled for today in the database thanks to being a materialized view. Therefore, the database does not need to filter all articles every time we fetch them.

A key point to remember is that materialized views don’t update automatically, which means they can contain stale data. To keep it up to date, we need to refresh it manually.

Besides using raw SQL to achieve this, we can use the Drizzle ORM.

Materialized views can come in handy when caching the results of complex queries. For example, we could refresh the view once a day at midnight to avoid running it often. To do that, we could set up a cron job, for example.

If you want to know more about cron, check out API with NestJS #25. Sending scheduled emails with cron and Nodemailer

Summary

Views are useful for simplifying complex queries and making them easier to manage. They can be particularly helpful in scenarios like transitioning from old tables to new ones. For example, if you’re phasing out an old table, a view can temporarily replace it. Additionally, views can be used to grant users access to specific data without giving them direct access to the underlying tables. Materialized views, on the other hand, are valuable for caching data, which is especially beneficial when working with large datasets that are accessed frequently. Thanks to all of the above, views and materialized views can make managing your database easier and more efficient.

Series Navigation<< API with NestJS #159. Date and time with PostgreSQL and the Drizzle ORMAPI with NestJS #161. Generated columns with the Drizzle ORM and PostgreSQL >>
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments