ArticleZip > How To Include The Deleted Elements When Querying A Paranoid Table On Sequelize Js

How To Include The Deleted Elements When Querying A Paranoid Table On Sequelize Js

When working with databases, Sequelize.js is an excellent tool to interact with SQL databases using Node.js. However, handling deleted elements when querying a paranoid table in Sequelize.js requires a specific approach to ensure you retrieve all the necessary data. In this guide, we'll walk you through how to include the deleted elements when querying a paranoid table on Sequelize.js.

### Understanding Paranoid Tables
Before we dive into querying, let's quickly discuss what a paranoid table is. In Sequelize.js, a paranoid table refers to a table where records are not physically deleted from the database when the delete operation is performed. Instead, a special flag or column, often named `deletedAt`, is used to mark these records as deleted without actually removing them. This approach enables data recovery and auditing while maintaining data integrity.

### Including Deleted Elements in Queries
By default, Sequelize.js excludes soft-deleted records when querying a paranoid table to prevent them from appearing in regular query results. To include these deleted elements in your queries, you need to explicitly override this behavior by setting the `paranoid` option to `false`.

Here's an example of how you can include deleted elements when querying a paranoid table:

Javascript

const { Model } = require('sequelize');

class YourModel extends Model {}
YourModel.init({
  // Your model attributes here
}, {
  sequelize,
  paranoid: false, // Include deleted elements
  modelName: 'yourModel',
});

In the above code snippet, setting `paranoid: false` at the model level instructs Sequelize.js to return both active and deleted records when querying the `YourModel` table.

### Handling Soft-Deleted Records
When querying a paranoid table with deleted elements included, it's essential to differentiate between active and deleted records in your application logic. You can achieve this by checking the `deletedAt` attribute of each record. Records with a non-null `deletedAt` value are soft-deleted, while those with a null value are active.

Here's how you can filter out soft-deleted records in your queries:

Javascript

const activeRecords = await YourModel.findAll({
  where: {
    deletedAt: null, // Filter out soft-deleted records
  },
});

In the above code snippet, the `where: { deletedAt: null }` condition filters out soft-deleted records, ensuring that only active records are returned in the query result.

### Conclusion
In conclusion, querying a paranoid table in Sequelize.js with deleted elements included involves setting the `paranoid: false` option to retrieve both active and soft-deleted records. Remember to handle these deleted elements appropriately in your application logic by filtering out soft-deleted records based on the `deletedAt` attribute. By following these steps, you can effectively manage and retrieve data from paranoid tables in Sequelize.js.