API with NestJS #180. Organizing Drizzle ORM schema with PostgreSQL

NestJS

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

As our application grows, it gets increasingly important to create a file structure that’s easy to maintain. Also, if we care about it from the start, it is easier to achieve.

In this article, we learn how to organize the database schema when working with the Drizzle ORM and NestJS.

Handling column names

When working with PostgreSQL, we use the function to specify the schema of a particular table.

database-schema.ts

Above, we specify a bunch of properties, such as and . When we generate a migration, Drizzle specifies the column names in our Database based on the property names we chose.

If you want to know more about migrations with the Drizzle ORM, check out API with NestJS #176. Database migrations with the Drizzle ORM

0000_add-users-table.sql

Typically, columns in SQL databases use the convention. However, since TypeScript and JavaScript commonly use the convention, Drizzle uses it for column names. To handle that, we can explicitly provide the name of our columns.

database-schema.ts

When we do that, Drizzle uses the explicitly provided column names when generating a migration.

0000_add-users-table.sql

Converting the name conventions automatically

Alternatively, we can tell the Drizzle ORM to map the to alternatively. To do that, we need to provide the option when initializing the Drizzle ORM.

drizzle.service.ts

If you want to know how to create the , take a look at API with NestJS #149. Introduction to the Drizzle ORM with PostgreSQL

We also have to provide the same option when configuring the Drizzle Kit.

drizzle.config.ts

Once we add the parameter, Drizzle ORM automatically translates properties such as to .

Organizing schema files

A straightforward way of organizing our database schema is to put it all in one TypeScript file.

database-schema.ts

It’s crucial to export the , which includes all our schemas together with the relations such as the . We then use it in our .

If you want to know more about the relationships with the Drizzle ORM, check out the following articles:

drizzle.service.ts

If we don’t provide the relations objects, such as the , the relational Query API built into Drizzle ORM won’t work. It will cause the following error:

Cannot read properties of undefined (reading ‘referencedTable’)

Splitting the schema into multiple files

As our database grows, storing the entire database schema in one file becomes unmaintainable. To deal with that, we can create separate TypeScript files for every table, each ending with .

users.schema.ts

articles.schema.ts

Now, we need to configure the Drizzle ORM to look for the schema in all files ending with .

drizzle.config.ts

Finally, we still need to provide the Drizzle ORM with an object that contains all schemas and relationships.

database-schema.ts

We can use it in our to provide all the necessary information.

Summary

In this article, we split our schema into multiple files to make it easier to maintain. While doing that, we ensured that we were still providing the Drizzle ORM with all of the necessary information, such as the relations. Besides that, we learned how to avoid manually converting the convention to . All of that makes our schemas less complicated, shorter, and easier to understand.

Series Navigation<< API with NestJS #179. Pattern matching search with Drizzle ORM and PostgreSQLAPI with NestJS #181. Prepared statements in PostgreSQL with Drizzle ORM >>
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments