How to make a delete on cascade on a paraloid in Sequelize?

Viewed 103

First of all,

I would like to know if a 'soft-delete' with cascade is even possible? I've seen quite a few similar posts at best, but none really have a conclusive answer. (I even tried with Hooks, nothing helped).

Here is the scenario I want to implement:

  • A brand has one or more products.
  • A product belongs to one and only one brand.
  • When deleting a product, it is archived (soft-delete).
  • When deleting a brand, it is archived AND its associated products.

Here are my two models - Brand and Product :

// marqueModel.js
module.exports = (sequelize, Sequelize) =>
    sequelize.define('marque', {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            autoIncrement: true,
            field: 'MAR_ID'
        },
        nom: {
            type: Sequelize.STRING,
            allowNull: false,
            unique: true,
            field: 'MAR_NOM'
        },
        logourl: {
            type: Sequelize.STRING,
            allowNull: false,
            field: 'MAR_URL'
        },
    }, {
        tableName: 'MARQUE',
        paranoid: true,
        timestamps: true,
        deletedAt: 'MAR_DATE_ARCHIVE',
    });
    
// produitModel.js
module.exports = (sequelize, Sequelize) =>
    sequelize.define('produit', {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            autoIncrement: true,
            field: 'PRO_ID'
        },
        nom: {
            type: Sequelize.STRING,
            allowNull: false,
            unique: false,
            field: 'PRO_NOM'
        },
        prix: {
            type: Sequelize.STRING,
            allowNull: false,
            field: 'PRO_PRIX'
        },
        description: {
            type: Sequelize.TEXT,
            allowNull: false,
            field: 'PRO_DESCRIPTION'
        },
        imageurl: {
            type: Sequelize.STRING,
            allowNull: false,
            field: 'PRO_URL'
        },
        qtestock: {
            type: Sequelize.INTEGER,
            allowNull: false,
            field: 'PRO_QTESTOCK'
        }
    }, {
        tableName: 'PRODUIT',
        paranoid: true,
        timestamps: true,
        deletedAt: 'PRO_DATE_ARCHIVE',
    });

I placed my associations in a different file, to avoid having problems :

const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;

// Models
db.users = require('../model/userModel')(sequelize, Sequelize);
db.produits = require('../model/produitModel')(sequelize, Sequelize);
db.marques = require('../model/marqueModel')(sequelize, Sequelize);
db.commandes = require('../model/commandeModel')(sequelize, Sequelize);

// Relations
// un produits appartiennent à une marque
db.produits.belongsTo(db.marques, {
    foreignKey: {
        name: 'marqueId',
        field: 'FK_MAR_PRO_ID'
    },
    onDelete: 'cascade',
    hooks: true
});

Am I forgetting something ? I've read the doc of Sequelize multiple time already... Or do I have to do the product cascade myself?

0 Answers
Related