Is it possible to pass an object into model.init in Sequelize?

34 views Asked by At

I'm wondering basically what the title says, since for my nodejs application I need to create multiple tables with the same set of columns, so I'm wondering if I can do something like this:

Let's say I have an object:

const defaultValues = {
      name: {
        type: DataTypes.STRING(50),
        defaultValue: null
      },
      email: {
        type: DataTypes.STRING(50),
        defaultValue: null
      },
      username: {
        type: DataTypes.STRING(50),
        defaultValue: null
      },
}

Is there any way to accomplish something like this in my model?:

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class users extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  };
  users.init({
      defaultValues
  }, {
    sequelize,
    tableName: 'users',
    modelName: 'users',
    timestamps: false
  });
  return users;
};

If this isn't possible, is there any other way to accomplish what is basically inheritance using the sequelize-cli generated models or would I have to completely redo everything and manually create the models?

0

There are 0 answers