TypeORM entity classes पेस्ट करें और placeholder values से भरा वह JSON आकार पाएं जो ये entities API से लौटाने पर बनाती हैं। यह उन NestJS या Express projects के लिए है जो entities को सीधे responses में वापस भेजते हैं और जिन्हें Swagger examples या frontend के साथ तय करने के लिए request/response samples चाहिए।
Values @Column options से नहीं बल्कि हर property के TypeScript type से आती हैं। इसलिए price: number के साथ @Column({ type: "decimal" }) declare किया column संख्या 0 के रूप में निकलता है।
TypeORM बॉक्स में पेस्ट करें। जुड़ी entities और enums शामिल करें, ताकि nested objects भर सकें।परिणाम बॉक्स अपडेट होता है। जिस entity को कोई दूसरी entity reference नहीं करती वह top-level JSON object बनती है; अगर कई असंबंधित entities हैं, तो हर एक अपने class नाम के तहत दिखती है।enum Genre { Novel = "novel", Essay = "essay" }
@Entity()
export class Author {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
}
@Entity()
export class Book {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column({ type: "enum", enum: Genre })
genre: Genre;
@Column({ type: "decimal", nullable: true })
price: number | null;
@ManyToOne(() => Author)
author: Author;
@Exclude()
@Column()
internalCode: string;
}{
"id": "",
"genre": "novel",
"price": 0,
"author": {
"id": 0,
"name": ""
}
}Book Author को reference करता है, इसलिए Book root है और author उसके अंदर nested है। @Exclude() की वजह से internalCode हट जाता है, और number | null type वाला price पहले non-null type का पालन करके 0 बनता है। uuid primary key का type string है, इसलिए वह खाली string है।
@ManyToOne और @OneToOne relations nested objects बनते हैं; @OneToMany या @ManyToMany से Post[] एक element वाला array बनता है।Promise<Post[]>) और ESM Relation<User> wrapper खुलकर अंदर का type बनते हैं।@Exclude() लगे fields, static fields, methods और getters छोड़ दिए जाते हैं।Date properties (@CreateDateColumn वगैरह से) 1970-01-01T00:00:00.000Z जैसी ISO strings बनती हैं।Record<string, number>, {"key": 0} के रूप में दिखते हैं।null या खाली array से काट दिया जाता है, ताकि nesting सीमित रहे।जो controllers सीधे entities लौटाते हैं उन्हें भी @ApiResponse के लिए example payload चाहिए। Entity फ़ाइल पेस्ट करें और परिणाम की placeholder values को असली जैसी values से बदलें।
जब आप relations: ["author"] के साथ query करते हैं, तो frontend जानना चाहता है कि response कितना गहरा nested है। आउटपुट साफ़ दिखाता है कि कौन से relation fields objects हैं और कौन से arrays।
आउटपुट को supertest POST calls के request body के ढांचे के रूप में इस्तेमाल करें। id और timestamps जैसे बने हुए fields हटाएं, फिर बाकी भरें।
nullable, default या length जैसे @Column options नहीं पढ़े जाते; वैल्यू property type या literal initializer (status = "draft") से आती है। अगर कोई column null हो सकता है तो उस वैल्यू को खुद null करें।@Entity classes सभी बदली जाती हैं, class नाम से keyed ({ "Book": {...}, "Author": {...} })। एक-दूसरे को reference करने वाली entities फिर भी एक nested root बनाती हैं।null के रूप में दिखती हैं।ClassSerializerInterceptor के साथ इस्तेमाल होने वाले @Expose, serialization groups और @Transform लागू नहीं होते।@PrimaryGeneratedColumn("uuid") खाली string क्यों है?सिर्फ़ property type वैल्यू तय करता है। id: string "" देता है; decorator का "uuid" argument नहीं पढ़ा जाता। Documentation के लिए बाद में UUID जैसी वैल्यू डाल दें।
यह पूरी तरह load हुई entity के JSON.stringify से मेल खाता है। जो relations आपने relations से load नहीं किए वे असली response में बस नहीं होते, इसलिए आउटपुट के relation fields को load हुई स्थिति मानें।
PostgreSQL और MySQL drivers precision खोने से बचने के लिए decimal values को strings के रूप में लौटाते हैं। Entity में property को price: string declare करें और आउटपुट भी, आपके असली data की तरह, string होगा।