Sequelize Data Types
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 likeSTRING. Typically maps toTEXTorCLOBin 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 likeUNSIGNEDandZEROFILL.BIGINT: A larger integer type for storing big integers. Typically used for very large numbers that exceed the size of a standardINTEGER.FLOAT: For floating-point numbers, which are decimal numbers that can have fractions.DOUBLE: A double-precision floating-point number, offering more precision thanFLOAT.DECIMAL: A fixed-point number with a specified precision and scale, e.g.,DECIMAL(10, 2).
iii) Boolean Type -BOOLEAN: Used to storetrueorfalsevalues.
iv) Date and Time Types -DATE: Stores date and time information. This maps to aDATETIMEcolumn in most databases.DATEONLY: Stores only the date without the time. Typically maps to aDATEcolumn.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 toJSON, 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 toGEOMETRYbut 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.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();