Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize.INTEGER vs DataTypes.INTEGER

In code from 2016 using sequelize ORM, I see model types defined with this pattern:

 module.exports = function(sequelize, DataTypes) {
     const Tasks = sequelize.define("Tasks", {  id: {
       type: DataTypes.INTEGER,
       [ ...etc.]

However in the current sequelize docs you see most prominently documented: Sequelize.INTEGER (or other type then integer). At the same time in the current docs I find also DataTypes still documented/used: here.

On same page the Sequelize.INTEGER is used..., is that only for deferrables or something?

I tried to find whether this altered over time or something but could not find it.

When Sequelize.INTEGER is 'current solution' could I just alter above code into:

module.exports = function(sequelize, Sequelize) {
  const Tasks = sequelize.define("Tasks", {  id: {
    type: Sequelize.INTEGER,
    [ ...etc.]

Or would using Sequelize as argument somehow make this fail?

like image 225
musicformellons Avatar asked Jul 21 '17 10:07

musicformellons


1 Answers

The second parameter in both of them is just the sequelize package itself You can use any of them which is on you what you want to use

const Sequelize = require('sequelize');

You'll notice in your index.js of models (if you set up as suggested) that you do something like the below, where you are passing in sequelize as the second argument.

const model = require(path.join(__dirname, file))(sequelize, Sequelize);

This exposes the data types. It doesn't matter what you call it. For example I am calling it abc in below code you can use any name

module.exports = (sequelize, abc) => {
  const Driver = sequelize.define('Driver', {
  firstName: {
       type: abc.STRING(),
       allowNull: false
  },
  last_name: {
       type: abc.TEXT,
       allowNull: true
  },
  email: {
      type: abc.TEXT,
      allowNull: false
  },
  password: {
      type: abc.TEXT,
      allowNull: true
  }

Same with migrations.

like image 124
Aryan Avatar answered Sep 28 '22 02:09

Aryan