How to implement soft deletes in Prisma

The admin panel that you'll actually want to use. Try for free.

November 25, 2023

Soft deletes are a method for handling data removal in databases. Instead of permanently erasing a record, a soft delete marks it as deleted, often by setting a flag or a timestamp. This approach is useful for preserving data integrity and maintaining a record of deletions without losing the actual data.

Implementing Soft Deletes in Prisma

Prisma, an open-source database toolkit, supports soft deletes through its modeling and query capabilities. Implementing soft deletes in Prisma involves a few key steps: updating your data model, modifying queries, and handling the deleted data in your application logic.

Update the Prisma Schema

First, modify your Prisma schema to include a field that indicates whether a record is deleted. Commonly, a deletedAt field of type DateTime is used, which is null when the record is not deleted.

model YourModel { id Int @id @default(autoincrement()) name String deletedAt DateTime? // Nullable field to track soft deletes }

Modifying Queries for Soft Deletes

Inserting and Updating Records

When inserting or updating records, you don't need to change anything regarding the deletedAt field. It should remain null by default for new records.

Soft Deleting Records

To soft delete a record, update the deletedAt field with the current timestamp instead of using the delete operation.

const softDeleteRecord = async (id) => { return await prisma.yourModel.update({ where: { id }, data: { deletedAt: new Date() }, }); };

Querying Non-Deleted Records

When querying, you need to filter out soft-deleted records. This is done by adding a condition to check that the deletedAt field is null.

const getActiveRecords = async () => { return await prisma.yourModel.findMany({ where: { deletedAt: null }, }); };

Restoring Soft Deleted Records

To restore a soft-deleted record, simply set the deletedAt field back to null.

const restoreRecord = async (id) => { return await prisma.yourModel.update({ where: { id }, data: { deletedAt: null }, }); };

Handling Permanently Deleting Records

If you need to permanently delete a record, use Prisma's delete operation. This should be used cautiously, as it cannot be undone.

const permanentlyDeleteRecord = async (id) => { return await prisma.yourModel.delete({ where: { id }, }); };

You could ship faster.

Imagine the time you'd save if you never had to build another internal tool, write a SQL report, or manage another admin panel again. Basedash is built by internal tool builders, for internal tool builders. Our mission is to change the way developers work, so you can focus on building your product.

Advantages of Soft Deletes

  1. Data Recovery: Easily recover deleted data.
  2. Audit Trails: Maintain a history of deletions.
  3. Data Integrity: Preserve relationships with other data that might break with hard deletes.

Implementing soft deletes in Prisma enhances data management by providing flexibility and security in handling deletions. It’s a valuable technique for many applications, particularly those requiring robust data auditability and recovery mechanisms.

Manually Editing Records

If you’re using a database editor or admin panel tool like Basedash, you should make sure to handle soft deletes correctly. Instead of deleting the record, update the deletedAt field by setting it to the current datetime. Configurable tools like Basedash also let you create segments to filter your records, allowing you to segregate your soft-deleted records.

TOC

Implementing Soft Deletes in Prisma
Modifying Queries for Soft Deletes
Advantages of Soft Deletes
Manually Editing Records

November 25, 2023

Soft deletes are a method for handling data removal in databases. Instead of permanently erasing a record, a soft delete marks it as deleted, often by setting a flag or a timestamp. This approach is useful for preserving data integrity and maintaining a record of deletions without losing the actual data.

Implementing Soft Deletes in Prisma

Prisma, an open-source database toolkit, supports soft deletes through its modeling and query capabilities. Implementing soft deletes in Prisma involves a few key steps: updating your data model, modifying queries, and handling the deleted data in your application logic.

Update the Prisma Schema

First, modify your Prisma schema to include a field that indicates whether a record is deleted. Commonly, a deletedAt field of type DateTime is used, which is null when the record is not deleted.

model YourModel { id Int @id @default(autoincrement()) name String deletedAt DateTime? // Nullable field to track soft deletes }

Modifying Queries for Soft Deletes

Inserting and Updating Records

When inserting or updating records, you don't need to change anything regarding the deletedAt field. It should remain null by default for new records.

Soft Deleting Records

To soft delete a record, update the deletedAt field with the current timestamp instead of using the delete operation.

const softDeleteRecord = async (id) => { return await prisma.yourModel.update({ where: { id }, data: { deletedAt: new Date() }, }); };

Querying Non-Deleted Records

When querying, you need to filter out soft-deleted records. This is done by adding a condition to check that the deletedAt field is null.

const getActiveRecords = async () => { return await prisma.yourModel.findMany({ where: { deletedAt: null }, }); };

Restoring Soft Deleted Records

To restore a soft-deleted record, simply set the deletedAt field back to null.

const restoreRecord = async (id) => { return await prisma.yourModel.update({ where: { id }, data: { deletedAt: null }, }); };

Handling Permanently Deleting Records

If you need to permanently delete a record, use Prisma's delete operation. This should be used cautiously, as it cannot be undone.

const permanentlyDeleteRecord = async (id) => { return await prisma.yourModel.delete({ where: { id }, }); };

You could ship faster.

Imagine the time you'd save if you never had to build another internal tool, write a SQL report, or manage another admin panel again. Basedash is built by internal tool builders, for internal tool builders. Our mission is to change the way developers work, so you can focus on building your product.

Advantages of Soft Deletes

  1. Data Recovery: Easily recover deleted data.
  2. Audit Trails: Maintain a history of deletions.
  3. Data Integrity: Preserve relationships with other data that might break with hard deletes.

Implementing soft deletes in Prisma enhances data management by providing flexibility and security in handling deletions. It’s a valuable technique for many applications, particularly those requiring robust data auditability and recovery mechanisms.

Manually Editing Records

If you’re using a database editor or admin panel tool like Basedash, you should make sure to handle soft deletes correctly. Instead of deleting the record, update the deletedAt field by setting it to the current datetime. Configurable tools like Basedash also let you create segments to filter your records, allowing you to segregate your soft-deleted records.

November 25, 2023

Soft deletes are a method for handling data removal in databases. Instead of permanently erasing a record, a soft delete marks it as deleted, often by setting a flag or a timestamp. This approach is useful for preserving data integrity and maintaining a record of deletions without losing the actual data.

Implementing Soft Deletes in Prisma

Prisma, an open-source database toolkit, supports soft deletes through its modeling and query capabilities. Implementing soft deletes in Prisma involves a few key steps: updating your data model, modifying queries, and handling the deleted data in your application logic.

Update the Prisma Schema

First, modify your Prisma schema to include a field that indicates whether a record is deleted. Commonly, a deletedAt field of type DateTime is used, which is null when the record is not deleted.

model YourModel { id Int @id @default(autoincrement()) name String deletedAt DateTime? // Nullable field to track soft deletes }

Modifying Queries for Soft Deletes

Inserting and Updating Records

When inserting or updating records, you don't need to change anything regarding the deletedAt field. It should remain null by default for new records.

Soft Deleting Records

To soft delete a record, update the deletedAt field with the current timestamp instead of using the delete operation.

const softDeleteRecord = async (id) => { return await prisma.yourModel.update({ where: { id }, data: { deletedAt: new Date() }, }); };

Querying Non-Deleted Records

When querying, you need to filter out soft-deleted records. This is done by adding a condition to check that the deletedAt field is null.

const getActiveRecords = async () => { return await prisma.yourModel.findMany({ where: { deletedAt: null }, }); };

Restoring Soft Deleted Records

To restore a soft-deleted record, simply set the deletedAt field back to null.

const restoreRecord = async (id) => { return await prisma.yourModel.update({ where: { id }, data: { deletedAt: null }, }); };

Handling Permanently Deleting Records

If you need to permanently delete a record, use Prisma's delete operation. This should be used cautiously, as it cannot be undone.

const permanentlyDeleteRecord = async (id) => { return await prisma.yourModel.delete({ where: { id }, }); };

You could ship faster.

Imagine the time you'd save if you never had to build another internal tool, write a SQL report, or manage another admin panel again. Basedash is built by internal tool builders, for internal tool builders. Our mission is to change the way developers work, so you can focus on building your product.

Advantages of Soft Deletes

  1. Data Recovery: Easily recover deleted data.
  2. Audit Trails: Maintain a history of deletions.
  3. Data Integrity: Preserve relationships with other data that might break with hard deletes.

Implementing soft deletes in Prisma enhances data management by providing flexibility and security in handling deletions. It’s a valuable technique for many applications, particularly those requiring robust data auditability and recovery mechanisms.

Manually Editing Records

If you’re using a database editor or admin panel tool like Basedash, you should make sure to handle soft deletes correctly. Instead of deleting the record, update the deletedAt field by setting it to the current datetime. Configurable tools like Basedash also let you create segments to filter your records, allowing you to segregate your soft-deleted records.

What is Basedash?

What is Basedash?

What is Basedash?

Basedash 🤝 Prisma

Teams that build with Prisma love using Basedash to manage and administrate their databases. It fits in your stack and allows you to understand your data like never before.

Teams that build with Prisma love using Basedash to manage and administrate their databases. It fits in your stack and allows you to understand your data like never before.

Teams that build with Prisma love using Basedash to manage and administrate their databases. It fits in your stack and allows you to understand your data like never before.

Dashboards and charts

Edit data, create records, oversee how your product is running without the need to build or manage custom software.

USER CRM

ADMIN PANEL

SQL COMPOSER WITH AI

Screenshot of a users table in a database. The interface is very data-dense with information.