Hooks Sequelize

Asked

Viewed 23 times

2

For purposes of understanding: Node.js with database postgres running with Sequelize v4;

Some fields of my database need to be persisted in encrypted database, by business requirement.

Using the BeforeCreate, and beforeBulkUpdate I can encrypt in the bank, and this is working. I also use the afterFind hook to decrypt the data while retrieving it, and this is also OK.

My problem is that when I use afterBulkUpdate to decrypt the data that has been entered, it does not appear to the decrypted user. Follow a short example:

module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define(
    "User",
    {
      full_name: {
        type: DataTypes.STRING,
        allowNull: false,
        validate: {
          notNull: {
            msg: VALIDATIONS_MESSAGES.FULL_NAME,
          },
          notEmpty: {
            args: true,
            msg: VALIDATIONS_MESSAGES.FULL_NAME,
          },
        },
      });

  User.afterBulkUpdate(async (entity, options) => {
    entity.attributes = decryptPersonalData(entity.attributes);
    return entity;
  });

When returning, the data still appears encrypted in the API. Any idea what I can use?

  • If this data still appears encrypted, probably this Hook was not called. How is this call made to execute this Hook? Probably, methods that involve Bulk need some parameter like individualHooks: true to shoot that Hook.

  • Hook was called, I can do console.log on the hook call. The point is that the change made to the object inside the hook is not sent on the return in the service. The individual Hooks are ok.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.