Node.js ORM written in TypeScript for type lovers.

View the Project on GitHub twinlogix/typetta

Soft-Delete

In many systems, the deletion of data, as a requirement, must not be physical but logical. This means that each entity must have a field that identifies its deleted/undeleted status and that the delete operation acts on this field rather than deleting the record from the data source. As a result, each read operation must filter all deleted records automatically.

The mechanism described above is commonly called soft-delete and is used to:

Middleware

Typetta offers the developer middleware that makes implementing the soft-delete mechanism much easier. All you have to do is specify a function that returns an object with two fields:

Take, for example, the following data model:

type User {
  id: ID!
  firstName: String
  lastName: String
  live: Boolean!
  deletedOn: Date
}

You can implement soft-delete behaviour for the User entity using this middleware as follows:

const entityManager = new EntityManager({
  overrides: {
    user: {
      middlewares: [
        softDelete(() => ({
          changes: { live: false, deletedOne: new Date() },
          filter: { live: true }
        })),
      ]
    }
  }
)