I have seen the use of both classes for defining data types, including in the official documentation, both apparently serve the same purpose.
On a tutorial, I saw the application was using DataTypes for the Model and Sequelize for Migrations, you can exchange between them and they continue to work. Example codes:
Model using DataTypes:
module.exports = (sequelize, DataTypes) => {
const Driver = sequelize.define('Driver', {
firstName: {
type: DataTypes.STRING(50),
allowNull: false
},
Migration using Sequelize:
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Drivers', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
Sequelize is an Object Relational Mapper (ORM), which simplifies interacting with relational databases by abstracting away the need to use SQL directly. This lesson introduces defining models with Sequelize in Node.
Sequelize is a Node. js-based Object Relational Mapper that makes it easy to work with MySQL, MariaDB, SQLite, PostgreSQL databases, and more. An Object Relational Mapper performs functions like handling database records by representing the data as objects.
With timestamps: false , the generated model will omit the createdAt and updatedAt attributes. You can also opt to include only the timestamp attribute you need as shown below: const User = sequelize. define( "User", { firstName: Sequelize.
If you give the datatype as TEXT in Sequelize It will automatically convert that field to NVARCHAR(MAX ) for SQL Server database.
The second parameter in both of them is just the sequelize package itself
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
module.exports = (sequelize, asd) => {
const Driver = sequelize.define('Driver', {
firstName: {
type: asd.STRING(50),
allowNull: false
},
Same with migrations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With