Promise-based Node.js ORM tool for Postgres, MySQL, MariaDB, SQLite, Microsoft SQL Server, Amazon Redshift and Snowflake's Data Cloud with solid transaction support, relations, eager and lazy loading, read replication and more
—
Comprehensive data type system with database-specific mappings and validation rules for model attributes.
Data types for textual data with various length constraints.
/**
* Variable length string
* @param length - Maximum length (default varies by dialect)
*/
STRING(length?: number): DataType;
/**
* Fixed length string
* @param length - Fixed length
*/
CHAR(length?: number): DataType;
/**
* Large text field for long strings
*/
TEXT: DataType;
/**
* Case-insensitive text (PostgreSQL only)
*/
CITEXT: DataType;Usage Examples:
class User extends Model {}
User.init({
username: DataTypes.STRING(50), // VARCHAR(50)
code: DataTypes.CHAR(10), // CHAR(10)
description: DataTypes.TEXT, // TEXT
bio: DataTypes.CITEXT // CITEXT (PostgreSQL)
});Data types for numeric values with various precision and scale options.
/**
* Integer number
*/
INTEGER: DataType;
/**
* Big integer for large numbers
*/
BIGINT: DataType;
/**
* Floating point number
*/
FLOAT: DataType;
/**
* Real number
*/
REAL: DataType;
/**
* Double precision floating point
*/
DOUBLE: DataType;
/**
* Fixed-point decimal number
* @param precision - Total number of digits
* @param scale - Number of digits after decimal point
*/
DECIMAL(precision?: number, scale?: number): DataType;
/**
* Alias for DECIMAL
*/
NUMERIC(precision?: number, scale?: number): DataType;
/**
* Tiny integer (-128 to 127)
*/
TINYINT: DataType;
/**
* Small integer (-32768 to 32767)
*/
SMALLINT: DataType;
/**
* Medium integer (-8388608 to 8388607)
*/
MEDIUMINT: DataType;Usage Examples:
class Product extends Model {}
Product.init({
id: DataTypes.INTEGER,
quantity: DataTypes.SMALLINT,
price: DataTypes.DECIMAL(10, 2), // 10 digits, 2 after decimal
weight: DataTypes.FLOAT,
largeNumber: DataTypes.BIGINT,
rating: DataTypes.TINYINT
});
// With additional options
class User extends Model {}
User.init({
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
balance: {
type: DataTypes.DECIMAL(15, 2),
defaultValue: 0.00
}
});Data types with additional options and variants.
/**
* Unsigned integers (positive values only)
*/
INTEGER.UNSIGNED: DataType;
BIGINT.UNSIGNED: DataType;
TINYINT.UNSIGNED: DataType;
SMALLINT.UNSIGNED: DataType;
MEDIUMINT.UNSIGNED: DataType;
/**
* Zero-filled integers (pad with leading zeros)
*/
INTEGER.ZEROFILL: DataType;
BIGINT.ZEROFILL: DataType;
/**
* Binary strings (affects collation)
*/
STRING.BINARY: DataType;
CHAR.BINARY: DataType;
/**
* Number type (general purpose numeric)
*/
NUMBER: DataType;Usage Examples:
class Statistics extends Model {}
Statistics.init({
// Unsigned integers for counts/IDs
userId: {
type: DataTypes.INTEGER.UNSIGNED,
allowNull: false
},
// Zero-filled display
orderNumber: {
type: DataTypes.INTEGER.ZEROFILL,
// Displays as 0000001234
},
// Binary string comparison
token: {
type: DataTypes.STRING.BINARY,
// Case-sensitive comparison
},
// General numeric
score: DataTypes.NUMBER
});Data types for temporal data.
/**
* Date and time with timezone
*/
DATE: DataType;
/**
* Date only (no time component)
*/
DATEONLY: DataType;
/**
* Time only (no date component)
*/
TIME: DataType;
/**
* Current timestamp default value
*/
NOW: DataType;Usage Examples:
class Event extends Model {}
Event.init({
startDate: DataTypes.DATE,
endDate: DataTypes.DATE,
eventDay: DataTypes.DATEONLY,
startTime: DataTypes.TIME,
createdAt: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
}
});Data type for true/false values.
/**
* Boolean true/false value
*/
BOOLEAN: DataType;Usage Example:
class User extends Model {}
User.init({
isActive: {
type: DataTypes.BOOLEAN,
defaultValue: true
},
isVerified: DataTypes.BOOLEAN
});Data types for binary data.
/**
* Binary large object
* @param length - Optional length specification
*/
BLOB(length?: 'tiny' | 'medium' | 'long'): DataType;Usage Example:
class File extends Model {}
File.init({
data: DataTypes.BLOB,
thumbnail: DataTypes.BLOB('tiny'),
content: DataTypes.BLOB('medium')
});Data types for universally unique identifiers.
/**
* UUID string format
*/
UUID: DataType;
/**
* UUID v1 generator (default value)
*/
UUIDV1: DataType;
/**
* UUID v4 generator (default value)
*/
UUIDV4: DataType;Usage Examples:
class User extends Model {}
User.init({
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
sessionId: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV1
}
});Data types for JSON data storage.
/**
* JSON data type
*/
JSON: DataType;
/**
* Binary JSON (PostgreSQL only)
*/
JSONB: DataType;Usage Examples:
class User extends Model {}
User.init({
preferences: DataTypes.JSON,
metadata: DataTypes.JSONB, // PostgreSQL only
profile: {
type: DataTypes.JSON,
defaultValue: {}
}
});
// Usage
const user = await User.create({
preferences: {
theme: 'dark',
notifications: true
}
});Data types for array storage (PostgreSQL only).
/**
* Array of specified type
* @param type - Element data type
*/
ARRAY(type: DataType): DataType;Usage Examples:
class User extends Model {}
User.init({
tags: DataTypes.ARRAY(DataTypes.STRING),
scores: DataTypes.ARRAY(DataTypes.INTEGER),
coordinates: DataTypes.ARRAY(DataTypes.FLOAT)
});
// Usage
const user = await User.create({
tags: ['developer', 'javascript', 'node.js'],
scores: [95, 87, 92],
coordinates: [40.7128, -74.0060]
});Data types for enumerated values.
/**
* Enumeration with predefined values
* @param values - Allowed values
*/
ENUM(...values: string[]): DataType;Usage Example:
class User extends Model {}
User.init({
role: DataTypes.ENUM('admin', 'user', 'moderator'),
status: {
type: DataTypes.ENUM('active', 'inactive', 'pending'),
defaultValue: 'pending'
}
});Data types for geometric data (PostgreSQL only).
/**
* Geometric data (points, lines, polygons)
*/
GEOMETRY: DataType;
/**
* Geographic data with Earth model
*/
GEOGRAPHY: DataType;Usage Example:
class Location extends Model {}
Location.init({
coordinates: DataTypes.GEOMETRY,
area: DataTypes.GEOGRAPHY
});Data types for network addresses (PostgreSQL only).
/**
* IP address (IPv4 or IPv6)
*/
INET: DataType;
/**
* Network address with subnet mask
*/
CIDR: DataType;
/**
* MAC address
*/
MACADDR: DataType;Usage Example:
class NetworkDevice extends Model {}
NetworkDevice.init({
ipAddress: DataTypes.INET,
subnet: DataTypes.CIDR,
macAddress: DataTypes.MACADDR
});Additional specialized data types.
/**
* Key-value store (PostgreSQL only)
*/
HSTORE: DataType;
/**
* Range type (PostgreSQL only)
* @param type - Range element type
*/
RANGE(type: DataType): DataType;
/**
* Virtual computed field (not stored in database)
*/
VIRTUAL: DataType;
/**
* Text search vector (PostgreSQL only)
*/
TSVECTOR: DataType;Usage Examples:
class Product extends Model {}
Product.init({
// Key-value pairs
attributes: DataTypes.HSTORE,
// Date range
availablePeriod: DataTypes.RANGE(DataTypes.DATE),
// Virtual computed field
fullName: {
type: DataTypes.VIRTUAL,
get() {
return `${this.firstName} ${this.lastName}`;
}
},
// Text search
searchVector: DataTypes.TSVECTOR
});Additional options available for all data types.
interface DataTypeOptions {
/** Allow null values */
allowNull?: boolean;
/** Default value */
defaultValue?: any;
/** Primary key */
primaryKey?: boolean;
/** Auto increment (numbers only) */
autoIncrement?: boolean;
/** Unique constraint */
unique?: boolean | string;
/** Column comment */
comment?: string;
/** Custom column name in database */
field?: string;
// String-specific options
/** Binary string (affects collation) */
binary?: boolean;
/** Character set */
charset?: string;
/** Collation */
collate?: string;
// Numeric-specific options
/** Unsigned integer */
unsigned?: boolean;
/** Zero fill */
zerofill?: boolean;
}Usage Example with Options:
class User extends Model {}
User.init({
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
unsigned: true
},
username: {
type: DataTypes.STRING(50),
allowNull: false,
unique: true,
comment: 'User login name'
},
email: {
type: DataTypes.STRING,
field: 'email_address', // Different column name in DB
defaultValue: null
},
balance: {
type: DataTypes.DECIMAL(10, 2),
unsigned: true,
defaultValue: 0.00
}
});Install with Tessl CLI
npx tessl i tessl/npm-sequelize