María Guillermina María Guillermina - 3 months ago 13
Node.js Question

When defining a Model, should I use Sequelize or DataType?

I'm using Sequelize to handle a MySQL database in a Node-Express app. I'm trying to define a Teacher model following the docs, but I've found two possible options to do so. The first one means doing

var Teacher = sequelize.define('Teacher', {
firstname: Sequelize.STRING,
lastname: Sequelize.STRING})


The other option was

export default function(sequelize, DataTypes) {
var Teacher = sequelize.define('Teacher', {
firstname: DataTypes.STRING,
lastname: DataTypes.STRING})


I don't see if there's actually a difference between those two (Sequelize and DataTypes), or why should I export the second one.

Answer

There is no difference. In the library source code, all datatype objects reference the same set of objects from DataTypes:

const DataTypes = require('./data-types');

…

for (const dataType in DataTypes) {
   Sequelize[dataType] = DataTypes[dataType];
}

…

module.exports = Promise.Sequelize = Sequelize;

Personally, I would suggest using an option with DataTypes. For me, it seems more semantically correct.

Comments