Tulakarn Rungsimawong
Tulakarn Rungsimawong

Reputation: 61

How to make sequelize model seperate file but same sequelize connection

everyone. I'm very new for sequelize ORM with nodejs. I've just created couple files for models seperately and I also seperate sequelize.js to connect to database.

The problem is when I make association between model(file)(Self Associate work well) and I've got an error **** "hasMany called with something that's not a subclass of Sequelize.Model"

I tried to solved this but doesn't work until I put every model in the same file. So, I realized that every model must use common sequelize connection.

Is there anyways to solve this problem without sequelize-cli (I don't want to use sequelize-cli) My code belows

Many thanks,

sequelize.js

const config = require("config");
const { Sequelize } = require("sequelize");

const sequelize = new Sequelize(
  config.get("database"),
  config.get("user"),
  config.get("cipher"),
  {
    dialect: "mariadb",
    timezone: "Asia/Bangkok",
  }
);

module.exports = sequelize;

user.js

const { DataTypes, Model } = require("sequelize");
const sequelize = require("./sequelize");
const Position = require("./position");

class User extends Model {}

User.init(
  {
    uuid: {
      type: DataTypes.UUID,
      allowNull: false,
      defaultValue: DataTypes.UUIDV4,
    },
    title: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    firstname: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    lastname: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    phone: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    email: {
      type: DataTypes.STRING,
      allowNull: true,
      validate: {
        isEmail: true,
      },
    },
    imgurl: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    login: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    passphase: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    status: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 1,
    },
  },
  {
    sequelize,
    modelName: "User",
    tableName: "users",
    timestamps: true,
  }
);

User.belongsToMany(User, {
  as: "CreatedUser",
  foreignKey: "user_id",
  through: "UserCreator",
});
User.belongsToMany(User, {
  as: "Creator",
  foreignKey: "creator_id",
  through: "UserCreator",
});

User.belongsToMany(User, {
  as: "ModifiedUser",
  foreignKey: "user_id",
  through: "UserModifier",
});
User.belongsToMany(User, {
  as: "Modifier",
  foreignKey: "modifier_id",
  through: "UserModifier",
});

User.belongsTo(Position);

module.exports = User;

position.js

const { DataTypes, Model } = require("sequelize");
const sequelize = require("./sequelize");
const User = require("./user");

class Position extends Model {}

Position.init(
  {
    uuid: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    status: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 1,
    },
  },
  {
    sequelize,
    modelName: "Position",
    tableName: "postions",
    timestamps: true,
  }
);

Position.hasMany(User, {
  foreignKey: {
    type: DataTypes.UUIDV4,
    allowNull: false,
  },
});

module.exports = Position;

Upvotes: 1

Views: 1088

Answers (1)

mreza
mreza

Reputation: 21

Sequelize has a .define() method to create all your schemas in JSON. If you want to keep your connection to the database separate from your models, I suggest doing something like this

./models/schemas/User.js


const userSchema = {
  attribute1: {
    type: ...,
    option1: ...,
    option2: ...,
    .
    .
  },
  .
  .
}

export default userSchema

Do the same with Position.js

Then:

./models/models.js

import {sequelize} from 'path/to/sequelize.js'
import positionSchema from './schemas/Position.js'
import userSchema from './schemas/User.js'


const User = sequelize.define("user", userSchema, { freezeTableName: true });
const Position = sequelize.define("position", positionSchema, { freezeTableName: true });


// add associations

export {
  User,
  Position,
  .
  .
}



Upvotes: 2

Related Questions