SQL DDL to Sequelize TypeScript Model Generator
Convert SQL CREATE TABLE DDL into Sequelize TypeScript models, DataTypes definitions, attributes interfaces, and Model.init declarations.
import { DataTypes, Model, Optional, Sequelize } from 'sequelize';
// ============================================================================
// Sequelize TypeScript Models Generated by ToolsAA
// Dialect: POSTGRES | Syntax: Model.init
// ============================================================================
UserAttributes {
id?: string;
email: string;
fullName?: string | null;
role?: 'CUSTOMER' | 'ADMIN' | 'VENDOR';
isActive?: boolean;
metadata?: Record<string, any> | null;
createdAt?: Date;
updatedAt?: Date;
}
UserCreationAttributes extends Optional<UserAttributes, 'id' | 'fullName' | 'role' | 'isActive' | 'metadata' | 'createdAt' | 'updatedAt'> {}
export class User extends Model<UserAttributes, UserCreationAttributes> implements UserAttributes {
public id!: string;
public email!: string;
public fullName!: string | null;
public role!: 'CUSTOMER' | 'ADMIN' | 'VENDOR';
public isActive!: boolean;
public metadata!: Record<string, any> | null;
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
}
initUser(sequelize: Sequelize): typeof User {
User.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
allowNull: false,
defaultValue: DataTypes.UUIDV4,
},
email: {
type: DataTypes.STRING(255),
allowNull: false,
unique: true,
},
fullName: {
type: DataTypes.STRING(120),
allowNull: true,
field: 'full_name',
},
role: {
type: DataTypes.ENUM('CUSTOMER', 'ADMIN', 'VENDOR'),
allowNull: false,
defaultValue: 'CUSTOMER',
},
isActive: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true,
field: 'is_active',
},
metadata: {
type: DataTypes.JSONB,
allowNull: true,
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
field: 'created_at',
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
field: 'updated_at',
},
},
{
sequelize,
tableName: 'users',
modelName: 'User',
timestamps: true,
underscored: true,
}
);
return User;
}
UserProfileAttributes {
id?: string;
userId: string;
avatarUrl?: string | null;
phone?: string | null;
shippingAddress?: string | null;
taxIdentifier?: string | null;
}
UserProfileCreationAttributes extends Optional<UserProfileAttributes, 'id' | 'avatarUrl' | 'phone' | 'shippingAddress' | 'taxIdentifier'> {}
export class UserProfile extends Model<UserProfileAttributes, UserProfileCreationAttributes> implements UserProfileAttributes {
public id!: string;
public userId!: string;
public avatarUrl!: string | null;
public phone!: string | null;
public shippingAddress!: string | null;
public taxIdentifier!: string | null;
// Association Mixin Placeholders
// public getUser!: BelongsToGetAssociationMixin<User>;
}
initUserProfile(sequelize: Sequelize): typeof UserProfile {
UserProfile.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
allowNull: false,
defaultValue: DataTypes.UUIDV4,
},
userId: {
type: DataTypes.UUID,
allowNull: false,
unique: true,
field: 'user_id',
references: {
model: 'users',
key: 'id',
},
onDelete: 'CASCADE',
},
avatarUrl: {
type: DataTypes.STRING(500),
allowNull: true,
field: 'avatar_url',
},
phone: {
type: DataTypes.STRING(30),
allowNull: true,
},
shippingAddress: {
type: DataTypes.TEXT,
allowNull: true,
field: 'shipping_address',
},
taxIdentifier: {
type: DataTypes.STRING(50),
allowNull: true,
field: 'tax_identifier',
},
},
{
sequelize,
tableName: 'user_profiles',
modelName: 'UserProfile',
timestamps: true,
underscored: true,
}
);
return UserProfile;
}
ProductAttributes {
id?: number;
sku: string;
title: string;
description?: string | null;
price?: number;
inventory?: number;
isPublished?: boolean;
createdAt?: Date;
}
ProductCreationAttributes extends Optional<ProductAttributes, 'id' | 'description' | 'price' | 'inventory' | 'isPublished' | 'createdAt'> {}
export class Product extends Model<ProductAttributes, ProductCreationAttributes> implements ProductAttributes {
public id!: number;
public sku!: string;
public title!: string;
public description!: string | null;
public price!: number;
public inventory!: number;
public isPublished!: boolean;
public readonly createdAt!: Date;
}
initProduct(sequelize: Sequelize): typeof Product {
Product.init(
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false,
},
sku: {
type: DataTypes.STRING(64),
allowNull: false,
unique: true,
},
title: {
type: DataTypes.STRING(200),
allowNull: false,
},
description: {
type: DataTypes.TEXT,
allowNull: true,
},
price: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false,
defaultValue: 0.00,
},
inventory: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0,
},
isPublished: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true,
field: 'is_published',
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
field: 'created_at',
},
},
{
sequelize,
tableName: 'products',
modelName: 'Product',
timestamps: true,
underscored: true,
indexes: [
{
name: 'idx_products_sku',
fields: ['sku'],
},
],
}
);
return Product;
}
OrderAttributes {
id?: string;
customerId: string;
orderNumber: string;
status?: 'PENDING' | 'PAID' | 'PROCESSING' | 'SHIPPED' | 'DELIVERED' | 'CANCELLED';
totalAmount?: number;
notes?: string | null;
createdAt?: Date;
}
OrderCreationAttributes extends Optional<OrderAttributes, 'id' | 'status' | 'totalAmount' | 'notes' | 'createdAt'> {}
export class Order extends Model<OrderAttributes, OrderCreationAttributes> implements OrderAttributes {
public id!: string;
public customerId!: string;
public orderNumber!: string;
public status!: 'PENDING' | 'PAID' | 'PROCESSING' | 'SHIPPED' | 'DELIVERED' | 'CANCELLED';
public totalAmount!: number;
public notes!: string | null;
public readonly createdAt!: Date;
// Association Mixin Placeholders
// public getUser!: BelongsToGetAssociationMixin<User>;
}
initOrder(sequelize: Sequelize): typeof Order {
Order.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
allowNull: false,
defaultValue: DataTypes.UUIDV4,
},
customerId: {
type: DataTypes.UUID,
allowNull: false,
field: 'customer_id',
references: {
model: 'users',
key: 'id',
},
onDelete: 'RESTRICT',
},
orderNumber: {
type: DataTypes.STRING(64),
allowNull: false,
unique: true,
field: 'order_number',
},
status: {
type: DataTypes.ENUM('PENDING', 'PAID', 'PROCESSING', 'SHIPPED', 'DELIVERED', 'CANCELLED'),
allowNull: false,
defaultValue: 'PENDING',
},
totalAmount: {
type: DataTypes.DECIMAL(12, 2),
allowNull: false,
defaultValue: 0.00,
field: 'total_amount',
},
notes: {
type: DataTypes.TEXT,
allowNull: true,
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
field: 'created_at',
},
},
{
sequelize,
tableName: 'orders',
modelName: 'Order',
timestamps: true,
underscored: true,
indexes: [
{
name: 'idx_orders_customer',
fields: ['customer_id'],
},
],
}
);
return Order;
}
OrderItemAttributes {
id?: number;
orderId: string;
productId: number;
quantity?: number;
unitPrice: number;
}
OrderItemCreationAttributes extends Optional<OrderItemAttributes, 'id' | 'quantity'> {}
export class OrderItem extends Model<OrderItemAttributes, OrderItemCreationAttributes> implements OrderItemAttributes {
public id!: number;
public orderId!: string;
public productId!: number;
public quantity!: number;
public unitPrice!: number;
// Association Mixin Placeholders
// public getOrder!: BelongsToGetAssociationMixin<Order>;
// public getProduct!: BelongsToGetAssociationMixin<Product>;
}
initOrderItem(sequelize: Sequelize): typeof OrderItem {
OrderItem.init(
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false,
},
orderId: {
type: DataTypes.UUID,
allowNull: false,
field: 'order_id',
references: {
model: 'orders',
key: 'id',
},
onDelete: 'CASCADE',
},
productId: {
type: DataTypes.INTEGER,
allowNull: false,
field: 'product_id',
references: {
model: 'products',
key: 'id',
},
onDelete: 'RESTRICT',
},
quantity: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 1,
},
unitPrice: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false,
field: 'unit_price',
},
},
{
sequelize,
tableName: 'order_items',
modelName: 'OrderItem',
timestamps: true,
underscored: true,
}
);
return OrderItem;
}
// ============================================================================
// Unified Model Registry & Associations Initializer
// ============================================================================
DatabaseModels {
User: typeof User;
UserProfile: typeof UserProfile;
Product: typeof Product;
Order: typeof Order;
OrderItem: typeof OrderItem;
}
initModels(sequelize: Sequelize): DatabaseModels {
initUser(sequelize);
initUserProfile(sequelize);
initProduct(sequelize);
initOrder(sequelize);
initOrderItem(sequelize);
// Setup Relationships
UserProfile.belongsTo(User, { foreignKey: 'userId', as: 'user' });
User.hasMany(UserProfile, { foreignKey: 'userId', as: 'userProfiles' });
Order.belongsTo(User, { foreignKey: 'customerId', as: 'user' });
User.hasMany(Order, { foreignKey: 'customerId', as: 'orders' });
OrderItem.belongsTo(Order, { foreignKey: 'orderId', as: 'order' });
Order.hasMany(OrderItem, { foreignKey: 'orderId', as: 'orderItems' });
OrderItem.belongsTo(Product, { foreignKey: 'productId', as: 'product' });
Product.hasMany(OrderItem, { foreignKey: 'productId', as: 'orderItems' });
return {
User,
UserProfile,
Product,
Order,
OrderItem,
};
}
Detected Tables & Architecture (5)
Zero-Upload Local Privacy
All SQL DDL parsing and Sequelize model code generation execute 100% locally in your browser sandbox. Your database architecture and proprietary schemas are never sent to any external server.
Dual TypeScript Architecture
Generate either standard Sequelize v6/v7 Model.init declarations with complete Attributes interfaces or modern sequelize-typescript class decorators with strong compile-time type safety.
Associations & Migrations Suite
Automatically detects 1:1, 1:N, and N:M foreign key relationships to generate hasMany, belongsTo, and belongsToMany declarations alongside Sequelize CLI migration scripts.