Skip to main content

Command Palette

Search for a command to run...

Sequelize Data Types

Published
View as Markdown
  1. Introduction
    In Sequelize, a popular Node.js ORM (Object-Relational Mapper) for working with relational databases like MySQL, PostgreSQL, SQLite, and others, data types are used to define the structure of the fields in a database table. These data types correspond to the types of data you can store in the database, such as strings, integers, dates, etc.
    i) String Types -

    STRING: A general-purpose variable-length string. By default, it creates a VARCHAR(255) column. You can specify the length if needed, e.g., Sequelize.STRING(100) for VARCHAR(100).

    TEXT: Used for longer text strings. The length is not limited like STRING. Typically maps to TEXT or CLOB in databases.

    CHAR: A fixed-length string, which will pad the value with spaces if the length is shorter than specified. E.g., Sequelize.CHAR(10).
    ii) Numeric Types -

    INTEGER: A standard integer type. You can specify options like UNSIGNED and ZEROFILL.

    BIGINT: A larger integer type for storing big integers. Typically used for very large numbers that exceed the size of a standard INTEGER.

    FLOAT: For floating-point numbers, which are decimal numbers that can have fractions.

    DOUBLE: A double-precision floating-point number, offering more precision than FLOAT.

    DECIMAL: A fixed-point number with a specified precision and scale, e.g., DECIMAL(10, 2).
    iii) Boolean Type -

    BOOLEAN: Used to store true or false values.
    iv) Date and Time Types -

    DATE: Stores date and time information. This maps to a DATETIME column in most databases.

    DATEONLY: Stores only the date without the time. Typically maps to a DATE column.

    TIME: Stores time information, e.g., 08:30:00.

    NOW: A special type that represents the current date and time.
    v) Binary Types -

    BLOB: Used for storing binary data like images or files. Comes in different sizes, e.g., BLOB('tiny'), BLOB('medium'), etc.

    UUID: A type for storing Universally Unique Identifiers (UUIDs). Useful for primary keys when you want to use UUIDs instead of auto-incrementing integers.
    vi) JSON Types -

    JSON: Stores JSON-formatted data. Supported natively by some databases like PostgreSQL.

    JSONB: Similar to JSON, but stores data in a binary format, which allows for faster query performance in databases that support it.
    vii) Enumerated Types -

    ENUM: Allows you to define a list of possible values for a column. For example, Sequelize.ENUM('value1', 'value2', 'value3') restricts the column to only these values.
    viii) Geometric Types -

    GEOMETRY: Stores geometric data, such as points, lines, or polygons. Useful for spatial databases.

    GEOGRAPHY: Similar to GEOMETRY but specifically for storing Earth-based coordinates.
    ix) Other Types -

    ARRAY: Allows you to store an array of items of a specific type. For example, Sequelize.ARRAY(Sequelize.STRING) will create an array of strings.

    RANGE: Represents a range of values, such as [1, 10] or [DATE1, DATE2]. Useful in PostgreSQL for range types.

    CIDR, INET, MACADDR: Used for storing network-related data like IP addresses.

  2. Example

     const { Sequelize, DataTypes } = require('sequelize');
     const sequelize = new Sequelize('database', 'username', 'password', {
         host: 'localhost',
         dialect: 'mysql',
     });
    
     const User = sequelize.define('User', {
         id: {
             type: DataTypes.INTEGER,
             autoIncrement: true,
             primaryKey: true,
         },
         username: {
             type: DataTypes.STRING,
             allowNull: false,
         },
         email: {
             type: DataTypes.STRING,
             unique: true,
         },
         password: {
             type: DataTypes.STRING,
             allowNull: false,
         },
         birthdate: {
             type: DataTypes.DATEONLY,
         },
         isActive: {
             type: DataTypes.BOOLEAN,
             defaultValue: true,
         },
         preferences: {
             type: DataTypes.JSON,
         }
     });
    
     sequelize.sync();