17

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 organized. In order to do that I need to require("sequelize') and var sequelize = new Sequelize('DB-Name', 'DB-User', 'DB-Password'); at the start of each file.

My question is, will that create a new connection to the database per model, or will it just keep re-using the same connection? Should I abandon the whole concept of "one model per file" and just create a master Models.js file?

I am very new to Node and am still getting used to its conventions. Thanks for the help!

KayoticSully
  • 1,400
  • 5
  • 15
  • 30

2 Answers2

19

Every model is defined as its own module, which you export:

module.exports = function(sequelize, DataTypes){
    return sequelize.define('Brand', {
        name: {
            type: DataTypes.STRING,
            unique: true,
            allowNull: false },
        description: {
            type: DataTypes.TEXT,
            allowNull: false },
        status: {
            type: DataTypes.INTEGER,
            unique: false,
            allowNull: true }
    })
};

Then simply import the module when you initialize Sequelize (and you can import many models here):

var Sequelize = require("sequelize");
var config = require("../../config/config.js");
var sequelize = new Sequelize(config.database, config.username, config.password,
    { dialect: config.dialect, host: config.host, port: config.port,
      omitNull: true, logging: false });
var Brand = require("./Brand").Brand;

You can read up more on modules at http://nodejs.org/api/modules.htm but the example above should get you started.

Dan Kohn
  • 33,811
  • 9
  • 84
  • 100
  • 2
    I had just found a good solution very similar to what you posted. The only difference is I made use of `sequelize.import('./File')` function. Either way works though! Thanks for another alternative. – KayoticSully Feb 15 '13 at 11:45
  • 1
    dankohn, I tried above code I had to pass the reference of sequelize and Sequelize like below, please confirm if I missed something that is causing not work with exact code as you have shared `var Brand = require("./dto/brand")(sequelize, Sequelize);` – Anuranjan Srivastav Oct 07 '18 at 07:05
  • My code is 5 years old, so please edit if necessary to make it work in the current version. – Dan Kohn Oct 07 '18 at 13:02
  • 1
    @DanKohn fyi - nodejs module link broken – CybeX Jul 04 '20 at 11:03
  • 1
    I got it working with: `const { Sequelize, Model, DataTypes, Op } = require('sequelize');` and then `const Brand= require('../path/to/brand.js')(sequelize, DataTypes)` – Papooch Oct 07 '20 at 15:22
2

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
  })
})
gsueagle2008
  • 4,583
  • 10
  • 36
  • 46
Narek Tootikian
  • 1,323
  • 2
  • 9
  • 15
  • fyi - sequelize.import is deprecated (see same link you posted) [https://sequelize.org/master/manual/models-definition.html#import] – CybeX Jul 04 '20 at 11:05