Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize: Require vs Import

In the documentation for sequlize they use the import function like so

// in your server file - e.g. app.js
var 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 = function(sequelize, DataTypes) {
  return sequelize.define("Project", {
    name: DataTypes.STRING,
    description: DataTypes.TEXT
  })
}

However, what would be so wrong with this?

// in your server file - e.g. app.js
var Project = require(__dirname + "/path/to/models/project")

// The model definition is done in /path/to/models/project.js
  var Project = sequelize.define("Project", {
    name: Sequelize.STRING,
    description: Sequelize.TEXT
  });
module.exports = Project
like image 308
Ouwen Huang Avatar asked Oct 17 '14 20:10

Ouwen Huang


People also ask

What is required true in Sequelize?

This is the case because, when the where option is used inside an include , Sequelize automatically sets the required option to true . This means that, instead of an OUTER JOIN , an INNER JOIN is done, returning only the parent models with at least one matching children.

How do I require a Sequelized model?

The sequelize instance needs to be passed into the user. js model file when you call the require() function. Notice the sequelize variable passed inside the second parentheses below: const User = require(`${__dirname}/models/user`)(sequelize);

What is required false in Sequelize?

sequelize fails to load a model if a nested include has a where clause that returns no models.. to ensure it doesnt fail completely, you can set a required: false clause along with the where clause.. this makes sequelize return a blank array instead of failing the load completely.. Save this answer.


1 Answers

Well, as you can see your model definition needs two things:

  1. Sequelize or DataTypes
  2. sequelize

In your first example when using sequelize.import('something'); it is similar to use require('something')(this, Sequelize); (this being the sequelize instance)

Both are necessary to initialize your model, but the important thing to understand is: One of these is a classtype so it's global, the other one is an instance and has to be created with your connection parameters.

So if you do this:

 var Project = sequelize.define("Project", {
    name: Sequelize.STRING,
    description: Sequelize.TEXT
  });
module.exports = Project

Where does sequelize come from? It has to be instantiated and passed somehow.

Here is an example with require instead of import:

// /path/to/app.js
var Sequelize = require('sequelize');
var sequelize = new Sequelize(/* ... */);
var Project = require('/path/to/models/project')(sequelize, Sequelize);

// /path/to/models/project.js
module.exports = function (sequelize, DataTypes) {
    sequelize.define("Project", {
       name: DataTypes.STRING,
       description: DataTypes.TEXT
    });
};

module.exports = Project

You could even change it so you wouldn't have to pass Sequelize by requiring it in the model itself, but you would still need to create a sequelize instance prior to define the model.

like image 120
Ervadac Avatar answered Sep 24 '22 08:09

Ervadac