Search code examples
node.jspostgresqlexpresssequelize.jssequelize-cli

Issues with implementing SequelizeJS soft deletion


I'm trying to implement 'soft deletion' in SequelizeJS. So, I've put 'paranoid: true' in my model and 'deletedAt' column in migration. I tried to use the answer from the other question, but it didn't work because of different versions. Also, I'm not sure if I wrote my controllers correctly. There is not that much information online, so I'm not sure how to check if I'm doing it correctly. I'm using Sequelize 5.3.0. Here, is my model:

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Properties = sequelize.define('Properties', {
    name: {
      allowNull: false,
      type: DataTypes.STRING
    }
  }, {
    timestamps: true,
    paranoid: true
  });
  Properties.associate = function(models) {
    // associations can be defined here
    Properties.hasMany(models.Deals, {
      foreignKey: 'id',
      onDelete: 'CASCADE'
    })
  };
  return Properties;
};

Here is my migration:

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Properties', {
      id: {
        allowNull: false,
        primaryKey: true,
        type: Sequelize.INTEGER,
        autoIncrement: true
      },
      name: {
        allowNull: false,
        type: Sequelize.STRING
      }
      deletedAt: {
        type: Sequelize.DATE
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Properties');
  }
};

I've found this solution from official docs, but it doesn't make sense to me:

User.findAll({
    include: [{
        model: Tool,
        where: { name: { [Op.like]: '%ooth%' } },
        paranoid: false // query and loads the soft deleted records
    }]
});

My getAllProperties controller:

getAllProperties: (req, res, next) => {
    return Properties
    .all()
    .then(properties => res.status(200).json({ status: 'Retrieved all properties', properties }))
    .catch(error => console.log(error));
  }

My destroyProperty controller:

destroyProperty: (req, res, next) => {
  const { id } = req.params;
  return Properties
  .findById(id)
  .then(property => {
    if (!property) {
      return res.status(404).send({ message: 'Property not found' })
    }
    return property
    .destroy()
    .then(() => res.status(200).json({ status: 'Deleted one property', property }))
    .catch(error => console.log(error));
  })
}

Solution

  • I figured out, that my models and migrations were good, the thing is that I was doing sequelize db:migrate:undo:all and sequelize db:migrate, but the db schema stayed the same. So, I did sequelize db:drop and sequelize db:create and then it started creating this field. Also, I changed my getAllProperties controller:

    getAllProperties: (req, res, next) => {
      return Properties
      .findAll({paranoid: false})
      .then(properties => res.status(200).json({ status: 'Retrieved all properties', properties }))
      .catch(error => console.log(error));
    }
    

    After I changed all that, it started working.