Estus Flask
Estus Flask

Reputation: 222344

Using Sequelize and DataTypes for data types

I've previously used a recipe for model definitions very similar to the one from Sequelize documentation, where model definitions are wrapped with factory functions to be fed to sequelize.import:

module.exports = (sequelize, DataTypes) => {
  return sequelize.define('SomeModel', {
    field: DataTypes.STRING(100)
  });
};

This structure works for me but it is not always convenient to export wrapper function from the module (e.g. when it exports a class for base model that uses DataTypes but shouldn't be imported with sequelize.import).

Is Sequelize fully interchangeable with DataTypes for data types (DataTypes.STRING vs Sequelize.STRING), so factory function could be omitted?

Can DataTypes be dependent on the dialect that was chosen in sequelize instance, and thus preferred?

Upvotes: 2

Views: 3033

Answers (1)

Keval
Keval

Reputation: 3326

Yes Sequelize fully interchangeable with DataTypes for data types.

https://github.com/sequelize/sequelize/blob/3e5b8772ef75169685fc96024366bca9958fee63/lib/sequelize.js

Also datatypes are mapped according to dialect but they are added to Sequelize class so you can use either

https://github.com/sequelize/sequelize/blob/3e5b8772ef75169685fc96024366bca9958fee63/lib/data-types.js#L57

Upvotes: 3

Related Questions