DataTypes in Sequelize ORM

  • Sequelize supports a wide range of data types that correspond to the data types available in different SQL databases like PostgreSQL, MySQL, SQLite, and Microsoft SQL Server. When you define a model and its attributes in Sequelize, you specify the data type of each attribute to ensure the data is stored correctly in the database.
Here is a list of some common Sequelize data types:

String/Binary Types

  • DataTypes.STRING: Variable length string. You can specify a length as an argument (e.g., STRING(255)).
  • DataTypes.TEXT: A large text block.
  • DataTypes.CHAR: Fixed-length string.
  • DataTypes.BINARY: Binary data.
  • DataTypes.BLOB: A large block of binary data.
Numeric Types
  • DataTypes.INTEGER: Integer.
  • DataTypes.BIGINT: Large integer.
  • DataTypes.FLOAT: Floating point number. Can specify precision as an argument.
  • DataTypes.REAL: Floating point number. Similar to `FLOAT` in some databases.
  • DataTypes.DOUBLE: Double precision floating point number.
  • DataTypes.DECIMAL: Decimal number. Can specify precision and scale as arguments.
Date/Time Types
  • DataTypes.DATE: Date and time without timezone.
  • DataTypes.DATEONLY: Date without time.
  • DataTypes.TIME: Time without date.
  • DataTypes.BOOLEAN: Boolean value.
Other Types
  • DataTypes.ENUM: Enumerated list of values.
  • DataTypes.ARRAY(DataType): Array of values of the specified data type. Supported by specific databases like PostgreSQL.
  • DataTypes.JSON: JSON object. Automatically parsed by Sequelize.
  • DataTypes.JSONB: Binary format JSON object. Supported by specific databases like PostgreSQL.
  • DataTypes.UUID: Universally unique identifier.
Example of Model Definition with DataTypes
  • Here's an example of how you might define a model with various data types:


    const { Sequelize, DataTypes } = require('sequelize');
    const sequelize = new Sequelize('sqlite::memory:'); // Example for SQLite

    const User = sequelize.define('User', {
        name: DataTypes.STRING,
        bio: DataTypes.TEXT,
        age: DataTypes.INTEGER,
        isAdmin: DataTypes.BOOLEAN,
        birthday: DataTypes.DATEONLY,
        appointment: DataTypes.DATE,
        email: {
            type: DataTypes.STRING,
            allowNull: false,
            unique: true
        },
        salary: DataTypes.FLOAT,
        uuid: DataTypes.UUID
    });

  • In this model, various Sequelize data types are used to define the properties of a `User`. These types ensure that Sequelize interacts with the database in the correct format for each attribute.
  • It's important to choose the appropriate data type for each model attribute to accurately reflect the data and ensure the integrity of your database. Sequelize's support for a wide range of SQL data types allows for precise data modeling and manipulation.

No comments:

Post a Comment

Primitive Types in TypeScript

In TypeScript, primitive types are the most basic data types, and they are the building blocks for handling data. They correspond to simple ...