Improving Data Integrity with Soft Deletes and Status Workflows
In our ecommerce-fullstack project, we recently focused on improving the lifecycle management of our product catalog. Often, developers reach for a hard database delete when removing an item, but in an e-commerce context, this creates issues with historical data and order consistency.
Moving Beyond Hard Deletes
Hard-deleting a row via a cascade operation is risky. If a product is referenced in past transactions, removing it entirely breaks those historical records. We transitioned to a 'soft delete' pattern using Prisma to keep data intact while marking it as unavailable to the end user.
// Example of soft deletion logic
async function softDeleteProduct(productId: string) {
return await prisma.product.update({
where: { id: productId },
data: { isDeleted: true, updatedAt: new Date() },
});
}
By adding an isDeleted flag, we preserve the record while ensuring it no longer appears in customer-facing storefront queries. This simple architectural shift prevents orphaned records in our database.
Managing Product States
Beyond deletion, we needed more granular control over product availability. A simple boolean wasn't enough to distinguish between 'Draft', 'Published', or 'Archived' states. We implemented a status update utility to transition these states safely.
// Changing product status safely
async function updateProductStatus(id: string, newStatus: ProductStatus) {
return await prisma.product.update({
where: { id },
data: { status: newStatus },
});
}
By separating the status logic from the database record deletion, we gain the flexibility to pause sales without removing the product from our system entirely.
The Lesson
Technical decisions in your database layer have long-term consequences on data integrity. By implementing soft deletes and explicit status transitions, we've moved away from destructive operations toward a more resilient state-based architecture. Always consider how a delete operation impacts the rest of your application ecosystem before executing it.
Generated with Gitvlg.com