Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize: difference of DataTypes and Sequelize

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
      },
like image 681
Deeh Avatar asked Nov 29 '17 22:11

Deeh


People also ask

What does Sequelize mean?

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.

What is Sequelize used for?

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.

How do you prevent createdAt and updatedAt in Sequelize?

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.

How do you define varchar in Sequelize?

If you give the datatype as TEXT in Sequelize It will automatically convert that field to NVARCHAR(MAX ) for SQL Server database.


1 Answers

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.

like image 187
Michael McCabe Avatar answered Sep 22 '22 01:09

Michael McCabe