Search code examples
sequelize.jshookfeathersjs

Hooks in FeathersJS or Sequelize


Given the scenario I want to insert a default value in a database column (date_introduced). I'm considering two alternatives:

  • use a hook in sequelize (hook: beforeCreate)
  • use a hook in feathersJS (hook: create)

What would be the specific benefits for each alternative? But of course there are other scenario's like checking on an input, etc... each with their own concerns.


Solution

  • Write a simple migration for your Sequelize DB.

    'use strict';
    
    module.exports = {
      up: function (queryInterface, Sequelize) {
        return queryInterface.changeColumn('tableName', 'columnName', {
          type: Sequelize.STRING,
          defaultValue: <defaultValue>
        });
      },
    
      down: function (queryInterface, Sequelize) {
        return queryInterface.changeColumn('tableName', 'columnName', {
          type: Sequelize.STRING,
          defaultValue: null
        });
      }
    };

    You can find more info in official documentation of feathers.