0

In this code I am writing the attributes and the options. At the end I am returning them in the method sequelize.define(). My problem is that I can not find a way to set relationship with other models using this method.

const { DataTypes } = require('sequelize');
 
module.exports = model;

function model(sequelize) {
  const attributes ={
    email: { type: DataTypes.STRING, allowNull: false },
    passwordHash: { type: DataTypes.STRING, allowNull: false },
    username: { type: DataTypes.STRING, allowNull: false },
    firstName: { type: DataTypes.STRING, allowNull: false },
    lastName: { type: DataTypes.STRING, allowNull: false },
    mobile:{type: DataTypes.INTEGER(11), allowNull: false},
    role: { type: DataTypes.STRING, allowNull: false }
  }

  const options = {
    defaultScope: {
      attributes: { exclude: ['passwordHash'] }      
    },
    scopes: {
      withHash: { attributes: {}, },      
    }
  };

  return sequelize.define('User', attributes, options);
}
Tobias S.
  • 5,247
  • 1
  • 12
  • 28
Aura
  • 1
  • Look at my answer here https://stackoverflow.com/a/61710568/1376618 to get an idea how to register models and associations – Anatoly Apr 17 '22 at 16:08

0 Answers0