Nodejs with Sequelizejs using separate files per model

前端 未结 2 2036
情书的邮戳
情书的邮戳 2021-02-05 16:48

This is an embarrassingly beginner question, but I just want to settle my worries about Sequelizejs. I want to split out each model into its own file to keep my source organize

2条回答
  •  星月不相逢
    2021-02-05 16:59

    In case if one wants to use EcmaScript 6 approach there is great example with explanation in Sequelize documentation here.

    // in your server file - e.g. app.js
    const Project = sequelize.import(__dirname + "/path/to/models/project")
    
    // The model definition is done in /path/to/models/project.js
    // As you might notice, the DataTypes are the very same as explained above
    module.exports = (sequelize, DataTypes) => {
      return sequelize.define("project", {
        name: DataTypes.STRING,
        description: DataTypes.TEXT
      })
    }
    

    The import method can also accept a callback as an argument.

    sequelize.import('project', (sequelize, DataTypes) => {
      return sequelize.define("project", {
        name: DataTypes.STRING,
        description: DataTypes.TEXT
      })
    })
    

提交回复
热议问题