Upgrade to V4
Sequelize v4 is the current release and it introduces some breaking changes. Majority of sequelize codebase has been refactored to use ES2015 features. The following guide lists some of the changes to upgrade from v3 to v4.
Changelog
Full Changelog for v4 release.
Breaking Changes
Node
To use new ES2015 features, Sequelize v4 requires at least Node v4 or above.
General
- Counter Cache plugin and consequently the counterCacheoption for associations has been removed.
- MariaDB dialect now removed. This was just a thin wrapper around MySQL. You can set dialect: 'mysql'an d Sequelize should be able to work with MariaDB server.
- Model.Instanceand- instance.Modelare removed. To access the Model from an instance, simply use- instance.constructor. The Instance class (- Model.Instance) is now the Model itself.
- Sequelize now uses an independent copy of bluebird library.
- Promises returned by sequelize are now instances of Sequelize.Promiseinstead of global bluebirdPromise.
- Pooling library was updated to v3, now you will need to callsequelize.close()to shutdown the pool.
Config / Options
- Removed support for old connection pooling configuration keys. Instead of - Old - pool: { maxIdleTime: 30000, minConnections: 20, maxConnections: 30 }- New - pool: { idle: 30000, min: 20, max: 30 }
- Removed support for pool: false. To use a single connection, setpool.maxto 1.
- Removed support for referencesKey, use a references objectreferences: { key: '', model: '' }
- Removed - classMethodsand- instanceMethodsoptions from- sequelize.define. Sequelize models are now ES6 classes. You can set class / instance level methods like this- Old - const Model = sequelize.define('Model', { ... }, { classMethods: { associate: function (model) {...} }, instanceMethods: { someMethod: function () { ...} } });- New - const Model = sequelize.define('Model', { ... }); // Class Method Model.associate = function (models) { ...associate the models }; // Instance Method Model.prototype.someMethod = function () {..}
- options.ordernow only accepts values with type of array or Sequelize method. Support for string values (ie- {order: 'name DESC'}) has been deprecated.
- With BelongsToManyrelationshipsadd/set/createsetters now set through attributes by passing them asoptions.through(previously second argument was used as through attributes, now it's considered options withthroughbeing a sub option)
- Raw options for where, order and group like - where: { $raw: '..', order: [{ raw: '..' }], group: [{ raw: '..' }] }have been removed to prevent SQL injection attacks.- Old - user.addProject(project, { status: 'started' });- New - user.addProject(project, { through: { status: 'started' } });
Data Types
- (MySQL/Postgres) BIGINTnow returned as string.
- (MySQL/Postgres) DECIMALandNEWDECIMALtypes now returned as string.
- (MSSQL) DataTypes.DATEnow usesDATETIMEOFFSETinstead ofDATETIME2sql datatype in case of MSSQL to record timezone. To migrate existingDATETIME2columns intoDATETIMEOFFSET, see #7201.
- DATEONLYnow returns string in- YYYY-MM-DDformat rather than- Datetype
Transactions / CLS
- Removed autocommit: truedefault, set this option explicitly to have transactions auto commit.
- Removed default REPEATABLE_READtransaction isolation. The isolation level now defaults to that of the database. Explicitly pass the required isolation level when initiating the transaction.
- The CLS patch does not affect global bluebird promise. Transaction will not automatically get passed to methods when used with - Promise.alland other bluebird methods. Explicitly patch your bluebird instance to get CLS to work with bluebird methods.- $ npm install --save cls-bluebird- const Sequelize = require('sequelize'); const Promise = require('bluebird'); const clsBluebird = require('cls-bluebird'); const cls = require('continuation-local-storage'); const ns = cls.createNamespace('transaction-namespace'); clsBluebird(ns, Promise); Sequelize.useCLS(ns);
Raw Queries
- Sequelize now supports bind parameters for all dialects. In v3 bindoption would fallback toreplacementsif dialect didn't supported binding. This could be a breaking change for MySQL / MSSQL where now queries will actually use bind parameters instead of replacements fallback.
Others
- Sequelize.Validatoris now an independent copy of- validatorlibrary.
- Model.validateinstance method now runs validation hooks by default. Previously you needed to pass- { hooks: true }. You can override this behavior by passing- { hooks: false }.
- The resulting promise from the Model.validateinstance method will be rejected when validation fails. It will fulfill when validation succeeds.
- Sequelize.Utilsis not longer part of the public API, use it at your own risk.
- Hooksshould return Promises now. Callbacks are deprecated.
- Getters wont run with instance.get({ raw: true }), useinstance.get({ plain: true })
- requiredinside include does not propagate up the include chain.- To get v3 compatible results you'll need to either set - requiredon the containing include.- Old - user.findOne({ include: { model: project, include: { model: task, required: true } } });- New - User.findOne({ include: { model: Project, required: true, include: { model: Task, required: true } } }); User.findOne({ include: { model: Project, required: true, include: { model: Task, where: { type: 'important' } //where cause required to default to true } } });- Optionally you can add a - beforeFindhook to get v3 compatible behavior -- function propagateRequired(modelDescriptor) { let include = modelDescriptor.include; if (!include) return false; if (!Array.isArray(include)) include = [include]; return include.reduce((isRequired, descriptor) => { const hasRequiredChild = propogateRequired(descriptor); if ((descriptor.where || hasRequiredChild) && descriptor.required === undefined) { descriptor.required = true; } return descriptor.required || isRequired; }, false); } const sequelize = new Sequelize(..., { ..., define: { hooks: { beforeFind: propagateRequired } } });
 Home
  
  Reference
  Source
  
  Repository
  Home
  
  Reference
  Source
  
  Repository