title: Sequelize - Calling Triggers functions using hooks
body: <p>I want to implement soft_delete. So I have created a hook in my model class <code>beforeBulkDelete</code> that calls a stored procedure that put some date in <code>deleted_at</code> column and returns <code>TRIGGER</code>. </p>
  
  <p>I want to return <code>NULL</code> as a trigger so actual delete won't happen. </p>
  
  <p>I tried creating triggers using <code>CREATE TRIGGER</code> command instead of using Sequelize hooks and it works fine. But I would like to do it using hooks.</p>
  
  <p>Here's my model class - </p>
  
  <pre><code>```export default function defineAttachment(
    sequelize: Sequelize.Sequelize,
    DataTypes: any
  ) {
    const XerceCaseAttachment = sequelize.define(
      'XerceCaseAttachment',
      {
        id: {
          type: DataTypes.INTEGER,
          primaryKey: true,
          autoIncrement: true,
        },
        .
        .
        .
        deletedAt: {
          type: DataTypes.DATE,
          field: 'deleted_at',
        },
      },
      {
        tableName: 'attachments',
        hooks: {
          beforeBulkDestroy: (options: any) =&gt; {
            sequelize.query(`SELECT soft_delete();`, {
              type: sequelize.QueryTypes.DELETE,
              transaction: options.transaction,
            });
          },
        },
      }
    );```
  </code></pre>
  
  <p>Here's my <code>soft_delete</code> stored procedure - </p>
  
  <pre><code>```CREATE OR REPLACE FUNCTION soft_delete()
   RETURNS TRIGGER AS
  $$
     BEGIN
       RAISE NOTICE 'test triggers';
       EXECUTE 'UPDATE attachments SET deleted_at = current_timestamp WHERE id = $1';
       RETURN NULL;
     END;
  $$ LANGUAGE PLPGSQL;```
  </code></pre>
  
  <p>I am getting following error - </p>
  
  <pre><code>```Unhandled rejection SequelizeDatabaseError: trigger functions can only be called as triggers
      at Query.formatError (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/sequelize/lib/dialects/postgres/query.js:363:16)
      at query.catch.err (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/sequelize/lib/dialects/postgres/query.js:86:18)
      at bound (domain.js:395:14)
      at runBound (domain.js:408:12)
      at tryCatcher (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/bluebird/js/release/util.js:16:23)
      at Promise._settlePromiseFromHandler (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/bluebird/js/release/promise.js:512:31)
      at Promise._settlePromise (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/bluebird/js/release/promise.js:569:18)
      at Promise._settlePromise0 (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/bluebird/js/release/promise.js:614:10)
      at Promise._settlePromises (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/bluebird/js/release/promise.js:690:18)
      at _drainQueueStep (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/bluebird/js/release/async.js:138:12)
      at _drainQueue (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/bluebird/js/release/async.js:131:9)
      at Async._drainQueues (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/bluebird/js/release/async.js:147:5)
      at Immediate.Async.drainQueues [as _onImmediate] (/home/ishankhanduja/Cyberdyne/cyberdyne/server/node_modules/bluebird/js/release/async.js:17:14)
      at runCallback (timers.js:705:18)
      at tryOnImmediate (timers.js:676:5)
      at processImmediate (timers.js:658:5)
      at process.topLevelDomainCallback (domain.js:120:23)```
  </code></pre>
  
  <p>Expectation - </p>
  
  <p>It should not delete the row instead some date get stored in deleted_at</p>