API with NestJS #174. Multiple PostgreSQL schemas with Drizzle ORM

NestJS SQL

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

PostgreSQL uses schemas as namespaces within the database to hold tables and other structures, such as indexes. In this article, we explain how to use them with the Drizzle ORM and how they can be beneficial.

The public schema

Out of the box, PostgreSQL creates a schema called for each new database.

database-schema.ts

Let’s investigate a migration that Drizzle ORM creates for the above table.

If you want to know more about migrations with the Drizzle ORM, check out API with NestJS #149. Introduction to the Drizzle ORM with PostgreSQL

000_create-articles-table.sql

We can see that the above migration does not explicitly mention the schema. By default, when we don’t specify the schema, PostgreSQL assumes that we want to use the schema.

What’s interesting, the Drizzle ORM holds the information about our migrations in a separate schema called .

When we make a SQL query and don’t specify the schema, PostgreSQL also assumes that we want to use the schema.

How PostgreSQL chooses the default schema

PostgreSQL controls the default schema through the built-in variable called . It determines the order of schemas PostgreSQL looks at when making a query that does not specify the schema explicitly.

By default, contains . The first part refers to the name of the current user, which we can verify through the current_user variable.

Therefore, PostgreSQL first tries to look for the table in the schema. Since it does not exist, it tries the schema.

To switch the default schema, we need to change the  variable.

We can go back to the default value in a straightforward way.

We can prepend the table’s name with the intended schema if we want to be explicit.

Using other schemas

To create new schemas with the Drizzle ORM, we need the function.

database-schema.ts

Now, we need to use its output with our new tables.

database-schema.ts

What’s interesting is that we can create relationships between tables in different schemas. For example, we can add the column to our articles.

database-schema.ts

Thanks to defining the schema above, the Drizzle ORM will attach its name to SQL queries when necessary.

Let’s create a migration and inspect the output.

Now, we can see that the Drizzle ORM appends the schema’s name when creating our tables.

0001_create-users-data.sql

For example, we can use the above table for authentication.

users.service.ts

When we turn on the logger, we can see that the Drizzle ORM appends the name of the schema when making various SQL queries as well.

If you want to know more about logging with Drizzle ORM, take a look at API with NestJS #166. Logging with the Drizzle ORM

Advantages of multiple schemas

Using multiple schemas in PostgreSQL gives us several advantages. If we have multiple tables, organizing them into schemas can make it easier to navigate our database. Also, we can use schemas to manage access permissions in our database in order to restrict some users from using a specific schema. It can come in handy if we have multiple users interacting with our database. On top of that, we can have backups and other routine maintenance tasks target particular schemas without affecting the whole database.

Additionally, schemas can help us resolve naming conflicts that could happen if different teams work separately on the database. As long as they use dedicated schemas, they can use tables or indexes with the same names.

Summary

With schemas, we can manage our data in a more secure and readable way. It can come in handy, especially in complex environments with databases that have multiple users.

To learn how to work with schemas, we first wrote raw SQL queries to see how PostgreSQL works when we don’t specify the schema explicitly. Then, we used the Drizzle ORM to create more schemas with various tables. Learning how to do that can make our database more straightforward to manage, especially if our database is big and multiple users interact with it.

 

Series Navigation<< API with NestJS #173. Storing money with Drizzle ORM and PostgreSQLAPI with NestJS #175. PUT and PATCH requests with PostgreSQL and Drizzle ORM >>
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments