Woodshop
Search…
View soft-deleted records
Sequelize supports the concept of paranoid tables. A paranoid table is one that, when told to delete a record, it will not truly delete it. Instead, a special column called deletedAt will have its value set to the timestamp of that deletion request.
This means that paranoid tables perform a soft-deletion of records, instead of a hard-deletion. By default, Forest Admin hides soft-deleted records. This example will show you how to view soft-deleted records within your Forest Admin interface.
https://www.loom.com/share/30f68ffea7d649c5a9f896dddd5a85c8
www.loom.com

Define hooks on your model

Sequelize hooks are places we can write callbacks that get invoked at key points in time like afterCreate, afterDestroy, afterUpdate, and more. Here, we'll use beforeFind and beforeCount hooks ont our Companies model.
1
module.exports = (sequelize, DataTypes) => {
2
const { Sequelize } = sequelize;
3
4
const Companies = sequelize.define('companies', {
5
name: {
6
type: DataTypes.STRING,
7
allowNull: false,
8
},
9
score: {
10
type: DataTypes.INTEGER,
11
},
12
address: {
13
type: DataTypes.STRING,
14
},
15
}, {
16
tableName: 'companies',
17
schema: process.env.DATABASE_SCHEMA,
18
paranoid: true,
19
hooks: {
20
beforeFind(options) {
21
options.paranoid = false; // Hooks used to display soft-deleted records
22
},
23
beforeCount(options) {
24
options.paranoid = false; // Hooks used to count soft-deleted records
25
},
26
},
27
});
28
29
Companies.associate = (models) => {
30
};
31
32
return Companies;
33
};
34
Copied!

Use segments

For better visibility, you can create a segment to display your deleted records.
You can also use the option Access to records through Segments only to separate deleted records from the main table view 👇
Last modified 6mo ago