JSON data को TypeORM entity class में बदलता है: primary key वाली @Entity() class, और बाकी हर key TypeScript type, nullability और class-validator decorator के साथ @Column() property के रूप में।
इसे NestJS + TypeORM app में TypeOrmModule.forFeature([...]) के लिए पहली entity बनाने में इस्तेमाल करें, या जब Express project में TypeORM DataSource जोड़ रहे हों और आपके पास पहले से मौजूद data से मेल खाती entities चाहिए।
मॉडल का नाम में entity class का नाम डालें (default MyModel); support ticket SupportTicket बनता है।support-ticket.entity.ts के रूप में सेव करें और अपने DataSource के entities array में, या NestJS module में TypeOrmModule.forFeature([SupportTicket]) में जोड़ें।[
{ "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;
}दूसरे ticket में assignee नहीं है, इसलिए वह column string | null type के साथ @Column({ nullable: true }) है। opened_at Date type वाली openedAt property बना, और name: 'opened_at' मूल column नाम बनाए रखता है।
id key न हो या integer id हो → @PrimaryGeneratedColumn() id: number; 32-bit range से बड़ी id → @PrimaryGeneratedColumn({ type: 'bigint' }) id: string; UUID जैसी string id → @PrimaryColumn() id: string।@Column() इस्तेमाल करते हैं, decimals को type: 'double precision' मिलता है ताकि भिन्न न कटें, bigint और सिर्फ़ तारीख वाले date columns का type string है (TypeORM उन्हें टेक्स्ट के रूप में लौटाता है), date-times Date हैं।Record<string, unknown> या string[] जैसे typed array वाले simple-json columns बनते हैं।"007" जैसे quoted अंक string columns रहते हैं।@Column({ name: 'opened_at' }) जुड़ता है।null या न मौजूद रहे fields को nullable: true, | null और @IsOptional() मिलता है; बाकी को @IsNotEmpty()।nest g resource tickets से बनी खाली ticket.entity.ts को इस आउटपुट से बदलें; तब entity उस mock data से मेल खाती है जो frontend पहले से इस्तेमाल कर रहा था, और Repository<SupportTicket> इस्तेमाल के लिए तैयार है।
Entity बनाने के बाद typeorm migration:generate उसे database से compare करता है और column नामों व nullability समेत CREATE TABLE migration लिखता है।
simple-json JSON को टेक्स्ट के रूप में store करता है, इसलिए उसके अंदर की values पर filter नहीं कर सकते। अगर उन पर query करनी है तो PostgreSQL पर type: 'jsonb', MySQL पर type: 'json' पर जाएं।double precision floating-point type है; कीमतों के लिए @Column('decimal', { precision: 10, scale: 2 }) इस्तेमाल करें, जिसे TypeORM string के रूप में लौटाता है।@ManyToOne या @OneToMany relations बनना चाहिए।@Entity() में कोई नाम नहीं है, इसलिए TypeORM की default naming class नाम को snake_case में बदलती है: support_ticket। मौजूदा table के लिए नाम दें, जैसे @Entity('tickets')।
emitDecoratorMetadata बंद हो तो?बिना option वाला @Column() अपना type reflection metadata से पढ़ता है, इसलिए TypeORM data type error के साथ विफल होता है। tsconfig में इसे चालू करें या हर type बताएं, जैसे @Column('varchar')।
Date property को कौन सा database type मिलता है?MySQL पर datetime और PostgreSQL पर timestamp without time zone। अगर PostgreSQL पर time zones मायने रखते हैं, तो @Column('timestamptz') पर जाएं।