SQL DDL to MikroORM TypeScript Entity Generator
Convert SQL CREATE TABLE definitions into MikroORM entity classes with @Entity, @PrimaryKey, @Property, @Index, and relationship decorators.
/**
* MikroORM TypeScript Entities
* Generated automatically by ToolsAA SQL to MikroORM Generator
* Dialect: POSTGRES | Entities: 7
*/
import {
Cascade,
Collection,
Entity,
Enum,
ManyToOne,
OneToMany,
OneToOne,
PrimaryKey,
Property,
Unique,
} from '@mikro-orm/core';
// ============================================================================
// Database Enums
// ============================================================================
UserStatusEnum {
ACTIVE = 'active',
PENDING = 'pending',
SUSPENDED = 'suspended',
ARCHIVED = 'archived',
}
OrderStatusEnum {
DRAFT = 'draft',
PENDING = 'pending',
PAID = 'paid',
SHIPPED = 'shipped',
CANCELLED = 'cancelled',
}
@Entity({ tableName: 'tenants' })
export class Tenant {
@PrimaryKey({ type: 'uuid', defaultRaw: 'gen_random_uuid()' })
id!: string;
@Property({ unique: true, length: 64 })
slug!: string;
@Property({ length: 120, fieldName: 'company_name' })
companyName!: string;
@Property({ type: 'boolean', fieldName: 'is_active', default: true })
isActive!: boolean;
@Property({ onCreate: () => new Date() })
createdAt: Date = new Date();
@Property({ onUpdate: () => new Date() })
updatedAt: Date = new Date();
@OneToMany(() => User, (user) => user.tenant, { cascade: [Cascade.ALL], orphanRemoval: true })
users = new Collection<User>(this);
@OneToMany(() => Category, (category) => category.tenant, { cascade: [Cascade.ALL], orphanRemoval: true })
categories = new Collection<Category>(this);
@OneToMany(() => Product, (product) => product.tenant, { cascade: [Cascade.ALL], orphanRemoval: true })
products = new Collection<Product>(this);
@OneToMany(() => Order, (order) => order.tenant, { cascade: [Cascade.ALL], orphanRemoval: true })
orders = new Collection<Order>(this);
}
@Entity({ tableName: 'users' })
@Unique({ properties: ['tenant', 'email'] })
export class User {
@PrimaryKey({ type: 'uuid', defaultRaw: 'gen_random_uuid()' })
id!: string;
@Property({ length: 255 })
email!: string;
@Property({ length: 255, fieldName: 'password_hash' })
passwordHash!: string;
@Property({ length: 100, fieldName: 'display_name' })
displayName!: string;
@Enum(() => UserStatusEnum)
status!: UserStatusEnum = UserStatusEnum.ACTIVE;
@Property({ onCreate: () => new Date() })
createdAt: Date = new Date();
@Property({ onUpdate: () => new Date() })
updatedAt: Date = new Date();
@ManyToOne(() => Tenant, { fieldName: 'tenant_id', deleteRule: 'cascade' })
tenant!: Tenant;
@OneToOne(() => UserProfile, (userProfile) => userProfile.user)
userProfile?: UserProfile;
@OneToMany(() => Order, (order) => order.user, { cascade: [Cascade.ALL], orphanRemoval: true })
orders = new Collection<Order>(this);
}
@Entity({ tableName: 'user_profiles' })
export class UserProfile {
@PrimaryKey({ type: 'uuid', defaultRaw: 'gen_random_uuid()' })
id!: string;
@Property({ length: 500, nullable: true, fieldName: 'avatar_url' })
avatarUrl?: string;
@Property({ type: 'text', nullable: true })
bio?: string;
@Property({ length: 32, nullable: true, fieldName: 'phone_number' })
phoneNumber?: string;
@Property({ type: 'json', nullable: true })
preferences?: Record<string, unknown>;
@Property({ onCreate: () => new Date() })
createdAt: Date = new Date();
@OneToOne(() => User, { fieldName: 'user_id', deleteRule: 'cascade', owner: true })
user!: User;
}
@Entity({ tableName: 'categories' })
@Unique({ properties: ['tenant', 'slug'] })
export class Category {
@PrimaryKey({ autoincrement: true })
id!: number;
@Property({ length: 100 })
name!: string;
@Property({ length: 120 })
slug!: string;
@Property({ fieldName: 'sort_order', default: 0 })
sortOrder!: number;
@ManyToOne(() => Tenant, { fieldName: 'tenant_id', deleteRule: 'cascade' })
tenant!: Tenant;
@ManyToOne(() => Category, { fieldName: 'parent_id', deleteRule: 'set null', nullable: true })
parent?: Category;
@OneToMany(() => Category, (category) => category.parent, { cascade: [Cascade.ALL], orphanRemoval: true })
children = new Collection<Category>(this);
@OneToMany(() => Product, (product) => product.category, { cascade: [Cascade.ALL], orphanRemoval: true })
products = new Collection<Product>(this);
}
@Entity({ tableName: 'products' })
@Unique({ properties: ['tenant', 'sku'] })
export class Product {
@PrimaryKey({ type: 'uuid', defaultRaw: 'gen_random_uuid()' })
id!: string;
@Property({ length: 64 })
sku!: string;
@Property({ length: 200 })
title!: string;
@Property({ type: 'decimal', precision: 12, scale: 2 })
price!: string;
@Property({ fieldName: 'inventory_count', default: 0 })
inventoryCount!: number;
@Property({ type: 'json', nullable: true })
attributes?: Record<string, unknown>;
@Property({ type: 'array', nullable: true })
tags?: string[];
@Property({ type: 'boolean', fieldName: 'is_published', default: false })
isPublished!: boolean;
@Property({ onCreate: () => new Date() })
createdAt: Date = new Date();
@Property({ onUpdate: () => new Date() })
updatedAt: Date = new Date();
@ManyToOne(() => Tenant, { fieldName: 'tenant_id', deleteRule: 'cascade' })
tenant!: Tenant;
@ManyToOne(() => Category, { fieldName: 'category_id', deleteRule: 'set null', nullable: true })
category?: Category;
@OneToMany(() => OrderItem, (orderItem) => orderItem.product, { cascade: [Cascade.ALL], orphanRemoval: true })
orderItems = new Collection<OrderItem>(this);
}
@Entity({ tableName: 'orders' })
export class Order {
@PrimaryKey({ type: 'uuid', defaultRaw: 'gen_random_uuid()' })
id!: string;
@Property({ unique: true, length: 32, fieldName: 'order_number' })
orderNumber!: string;
@Enum(() => OrderStatusEnum)
status!: OrderStatusEnum = OrderStatusEnum.DRAFT;
@Property({ type: 'decimal', precision: 12, scale: 2 })
subtotal!: string;
@Property({ type: 'decimal', precision: 12, scale: 2, fieldName: 'tax_amount', default: 0.00 })
taxAmount!: string;
@Property({ type: 'decimal', precision: 12, scale: 2, fieldName: 'shipping_cost', default: 0.00 })
shippingCost!: string;
@Property({ type: 'decimal', precision: 12, scale: 2, fieldName: 'total_amount' })
totalAmount!: string;
@Property({ type: 'text', nullable: true })
notes?: string;
@Property({ onCreate: () => new Date() })
createdAt: Date = new Date();
@Property({ onUpdate: () => new Date() })
updatedAt: Date = new Date();
@ManyToOne(() => Tenant, { fieldName: 'tenant_id', deleteRule: 'cascade' })
tenant!: Tenant;
@ManyToOne(() => User, { fieldName: 'user_id', deleteRule: 'restrict' })
user!: User;
@OneToMany(() => OrderItem, (orderItem) => orderItem.order, { cascade: [Cascade.ALL], orphanRemoval: true })
orderItems = new Collection<OrderItem>(this);
}
@Entity({ tableName: 'order_items' })
export class OrderItem {
@PrimaryKey({ autoincrement: true })
id!: string;
@Property()
quantity!: number;
@Property({ type: 'decimal', precision: 12, scale: 2, fieldName: 'unit_price' })
unitPrice!: string;
@Property({ type: 'decimal', precision: 12, scale: 2, fieldName: 'line_total' })
lineTotal!: string;
@Property({ onCreate: () => new Date() })
createdAt: Date = new Date();
@ManyToOne(() => Order, { fieldName: 'order_id', deleteRule: 'cascade' })
order!: Order;
@ManyToOne(() => Product, { fieldName: 'product_id', deleteRule: 'restrict' })
product!: Product;
}
100% Client-Side In-Memory Engine
Zero database connections, remote API calls, or server uploads. All SQL parsing, AST extraction, and MikroORM TypeScript entity synthesis execute entirely in your local browser sandbox.
Complete MikroORM v5 & v6 Decorator Ecosystem
Accurately maps @PrimaryKey (UUID, autoincrement, composite), @Property (types, length, nullable, defaultRaw), @Index, @Unique, @Enum, and automated onCreate/onUpdate timestamp hooks.
Type-Safe Relational Graph & Multi-Dialect Support
Automatically detects foreign key constraints to build @ManyToOne, @OneToMany Collection<T>, and @OneToOne relations with cascade options across PostgreSQL, MySQL, SQLite, and MSSQL.