Converts JSON data into a TypeORM entity class: an @Entity() class with a primary key, and every other key as an @Column() property with a TypeScript type, nullability and a class-validator decorator.
Use it to create the first entity for TypeOrmModule.forFeature([...]) in a NestJS + TypeORM app, or when adding a TypeORM DataSource to an Express project and you need entities that match data you already have.
Model name (default MyModel); support ticket becomes SupportTicket.support-ticket.entity.ts and add it to your DataSource entities array or to TypeOrmModule.forFeature([SupportTicket]) in a NestJS module.[
{ "subject": "Login fails on Safari", "priority": 2, "assignee": "jdoe", "closed": false, "opened_at": "2024-06-03T08:12:00Z" },
{ "subject": "Export CSV is empty", "priority": 1, "closed": true, "opened_at": "2024-06-04T14:40:00Z" }
]import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
import { IsNotEmpty, IsOptional } from 'class-validator';
@Entity()
export class SupportTicket {
@PrimaryGeneratedColumn()
id: number;
@Column()
@IsNotEmpty()
subject: string;
@Column()
@IsNotEmpty()
priority: number;
@Column({ nullable: true })
@IsOptional()
assignee: string | null;
@Column()
@IsNotEmpty()
closed: boolean;
@Column({ name: 'opened_at' })
@IsNotEmpty()
openedAt: Date;
}The second ticket has no assignee, so that column is @Column({ nullable: true }) with the type string | null. opened_at became the openedAt property typed Date, with name: 'opened_at' keeping the original column name.
id key or an integer id → @PrimaryGeneratedColumn() id: number; an id above the 32-bit range → @PrimaryGeneratedColumn({ type: 'bigint' }) id: string; a string id such as a UUID → @PrimaryColumn() id: string.@Column(), decimals get type: 'double precision' so fractions are not truncated, bigint and date-only date columns are typed string (TypeORM returns them as text), date-times are Date.simple-json columns typed Record<string, unknown> or a typed array like string[]."007" remain string columns.@Column({ name: 'opened_at' }) is added whenever the JSON key differs.null or missing get nullable: true, | null and @IsOptional(); the rest get @IsNotEmpty().Replace the empty ticket.entity.ts created by nest g resource tickets with this output; the entity then matches the mock data the frontend was already using, and Repository<SupportTicket> is ready to use.
After creating the entity, typeorm migration:generate compares it with the database and writes the CREATE TABLE migration, column names and nullability included.
simple-json stores JSON as text, so you cannot filter on values inside it. On PostgreSQL switch to type: 'jsonb', on MySQL to type: 'json', if you need to query them.double precision is a floating-point type; for prices use @Column('decimal', { precision: 10, scale: 2 }), which TypeORM returns as a string.@ManyToOne or @OneToMany relations.@Entity() has no name, so TypeORM's default naming turns the class name into snake_case: support_ticket. For an existing table, pass the name, e.g. @Entity('tickets').
emitDecoratorMetadata is off?An option-less @Column() reads its type from reflection metadata, so TypeORM fails with a data type error. Enable it in tsconfig or state each type, e.g. @Column('varchar').
Date property get?datetime on MySQL and timestamp without time zone on PostgreSQL. If time zones matter on PostgreSQL, switch to @Column('timestamptz').