Data Types
Database-agnostic data types with automatic SQL mapping per database
DataTypes Enum
import { DataTypes } from "stabilize-orm";
DataTypes.STRING // VARCHAR - short text with optional lengthDataTypes.TEXT // TEXT - long text contentDataTypes.INTEGER // INTEGER - whole numbersDataTypes.BIGINT // BIGINT - large whole numbersDataTypes.FLOAT // FLOAT - single-precision decimalDataTypes.DOUBLE // DOUBLE - double-precision decimalDataTypes.DECIMAL // DECIMAL - exact decimal (e.g. currency)DataTypes.BOOLEAN // BOOLEAN - true/false valuesDataTypes.DATE // DATE - date onlyDataTypes.DATETIME // DATETIME - date and timeDataTypes.JSON // JSON - JSON dataDataTypes.UUID // UUID - universally unique identifierDataTypes.BLOB // BLOB - binary dataDatabase Type Mappings
| DataType | PostgreSQL | MySQL | SQLite | SQL Server |
|---|---|---|---|---|
| DataTypes.STRING | TEXT | VARCHAR(255) | TEXT | NVARCHAR(255) |
| DataTypes.TEXT | TEXT | TEXT | TEXT | NVARCHAR(MAX) |
| DataTypes.INTEGER | INTEGER | INT | INTEGER | INT |
| DataTypes.BIGINT | BIGINT | BIGINT | INTEGER | BIGINT |
| DataTypes.FLOAT | REAL | FLOAT | REAL | REAL |
| DataTypes.DOUBLE | DOUBLE PRECISION | DOUBLE | REAL | FLOAT |
| DataTypes.DECIMAL | DECIMAL(10,2) | DECIMAL(10,2) | NUMERIC | DECIMAL(10,2) |
| DataTypes.BOOLEAN | BOOLEAN | TINYINT(1) | INTEGER | BIT |
| DataTypes.DATE | DATE | DATE | TEXT | DATE |
| DataTypes.DATETIME | TIMESTAMP | DATETIME | TEXT | DATETIME2 |
| DataTypes.JSON | JSONB | JSON | TEXT | NVARCHAR(MAX) |
| DataTypes.UUID | UUID | CHAR(36) | TEXT | UNIQUEIDENTIFIER |
| DataTypes.BLOB | BYTEA | BLOB | BLOB | VARBINARY(MAX) |
String Types
Variable-length string. Note that PostgreSQL maps this to TEXT rather than a length-capped VARCHAR — the length option does not produce a PostgreSQL length constraint, because Postgres treats TEXT and VARCHAR(n) as the same type. It is still enforced: the value is checked against the declared length on write.
{ type: DataTypes.STRING, length: 255 }Large text content with no length limit. Identical to STRING at the SQL level, but expresses intent.
{ type: DataTypes.TEXT }Numeric Types
Whole numbers.
{ type: DataTypes.INTEGER }Large whole numbers. SQLite has no distinct 64-bit type, so it falls back to INTEGER.
{ type: DataTypes.BIGINT }Single-precision floating-point numbers.
{ type: DataTypes.FLOAT }Double-precision floating-point numbers.
{ type: DataTypes.DOUBLE }Exact decimal numbers — use this for money. Unlike FLOAT/DOUBLE it stores the value you wrote rather than the nearest binary approximation.
{ type: DataTypes.DECIMAL, precision: 10, scale: 2 }Boolean Type
True/false values.
{ type: DataTypes.BOOLEAN, defaultValue: false }Date & Time Types
Date and time.
{ type: DataTypes.DATETIME }Date only, no time component.
{ type: DataTypes.DATE }There is no DataTypes.TIME. The enum stops at these two temporal types — store a time of day as a STRING or fold it into a DATETIME. SQLite stores both temporal types as TEXT, so a comparison there is a string comparison; an ISO-8601 value is the safe format to write.
Special Types
JSON data. PostgreSQL uses JSONB, which is stored decomposed and can be indexed.
{ type: DataTypes.JSON }Universally unique identifier. Only PostgreSQL has a native type; MySQL and SQLite store the string form.
import { generateUUID } from "stabilize-orm";
{ type: DataTypes.UUID, defaultValue: generateUUID() }Binary large object — files, images, raw bytes.
{ type: DataTypes.BLOB }Primary Keys
There is no primaryKey or autoIncrement column option. The column keyed id is always the primary key, and the DDL chosen for it depends only on its type:
columns: { // Integer-style id → the dialect's auto-increment primary key id: { type: DataTypes.INTEGER },
// STRING or UUID id → a client-supplied primary key, no auto-increment id: { type: DataTypes.UUID, required: true },}| id type | Generated DDL |
|---|---|
| INTEGER / BIGINT / other | SERIAL PRIMARY KEY (Postgres) · INT AUTO_INCREMENT PRIMARY KEY (MySQL) · INTEGER PRIMARY KEY AUTOINCREMENT (SQLite) · INT IDENTITY(1,1) PRIMARY KEY (SQL Server) |
| STRING | UUID PRIMARY KEY (Postgres) · VARCHAR(255) PRIMARY KEY (MySQL) · TEXT PRIMARY KEY (SQLite) · NVARCHAR(255) PRIMARY KEY (SQL Server) |
| UUID | UUID PRIMARY KEY (Postgres) · VARCHAR(255) PRIMARY KEY (MySQL) · TEXT PRIMARY KEY (SQLite) · UNIQUEIDENTIFIER PRIMARY KEY (SQL Server) |
Because the primary key is recognised by the column key id, a model that names it anything else gets no primary key at all.
Complete Example
import { defineModel, DataTypes } from "stabilize-orm";
const Product = defineModel({ tableName: "products", columns: { id: { type: DataTypes.STRING, required: true, unique: true }, name: { type: DataTypes.STRING, length: 255, required: true }, description: { type: DataTypes.TEXT }, price: { type: DataTypes.DECIMAL, required: true }, stock: { type: DataTypes.INTEGER, defaultValue: 0 }, isActive: { type: DataTypes.BOOLEAN, defaultValue: true }, metadata: { type: DataTypes.JSON }, releaseDate: { type: DataTypes.DATE }, createdAt: { type: DataTypes.DATETIME }, },});