API with NestJS #158. Soft deletes with the Drizzle ORM

NestJS

This entry is part 158 of 177 in the API with NestJS

Deleting entities is a standard feature in most REST APIs. The most straightforward approach is to remove rows from the database permanently. However, we can use soft deletes to keep the deleted entities in our database. In this article, we learn how to do it using the Drizzle ORM and PostgreSQL.

Introducing soft deletes

To implement soft deletes, we can use a boolean flag to indicate which records are deleted.

In the code above, we use the  keyword so that the is_deleted flag is automatically set to false whenever a new entity is added to the database.

To perform a soft delete on the record above, we avoid using the keyword. Instead, we update the column to indicate that the record has been deleted without permanently removing it.


It’s crucial to notice that implementing soft deletes affects various queries. For instance, we need to account for it when fetching the list of all entities.

Advantages

One clear benefit of soft deletes is easily restoring deleted entities, offering a better user experience than using a backup. For example, an undo button can simply reset the flag to false. Additionally, we can still access deleted records from the database, which is helpful for generating comprehensive reports, for example.

Soft deletes are also helpful in managing relationships. For example, permanently deleting a record referenced in another table can cause a foreign key constraint violation. This issue is avoided with soft deletes since the records remain in the database.

If you want to know more about constraints, check out API with NestJS #152. SQL constraints with the Drizzle ORM

Disadvantages

A major drawback of soft deletes is the need to account for them in all related queries. Users might access information they shouldn’t if we forget to filter by the column when retrieving data. Unfortunately, this additional filtering can also affect performance.

Another factor to consider is the unique constraint. In the previous parts of this series, we created the  table, where each row contains a unique email.

database-schema.ts

In this scenario, each user must have a unique email. Hard deletes free up the email for reuse, but with soft deletes, the records remain in the database. Therefore, deleted users’ emails are not made available to others.

Soft deletes with the Drizzle ORM

A typical approach for soft deletes is to store the deletion date rather than just using a boolean flag.

database-schema.ts

Deleting entities

When deleting our categories, it’s important to correctly set the value of the column. Fortunately, we can use the function built into SQL.

categories.service.ts

What’s important is that we use the to prevent deleting a category that’s already deleted. In a case such as that, we need to throw the .

Fetching entities

We also need to use the function when fetching categories to filter out deleted ones.

categories.service.ts

Thanks to using the function, trying to fetch a category with a given ID that is deleted results in the 404 Not Found Error.

Updating entities

The soft deletes also affect how we update existing entities.

categories.service.ts

Restoring removed entities

We sometimes might want to restore an entity we removed. Thankfully, that’s straightforward and as simple as setting the column to null.

categories.service.ts

Summary

In this article, we implemented soft deletes and weighted their benefits and drawbacks. Soft deletes can improve the user experience by allowing the user to both delete and restore entities. However, they add complexity to our SQL queries. Despite this, soft deletes have applications and can be helpful in certain scenarios. Therefore, knowing how to implement them and when it makes sense is worth knowing.

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