TypeORM एंटिटी को JSON में बदलें!

TypeORM एंटिटी डालें और उदाहरण JSON बनाएँ! TypeORM @Entity, @Column समर्थित। API डॉक्यूमेंटेशन और टेस्ट request body (sample JSON) के लिए उपयोगी।
TypeORM
परिणाम
 

TypeORM एंटिटी को JSON में बदलें!

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 के रूप में निकलता है।

उपयोग कैसे करें

  1. Entity फ़ाइल को TypeORM बॉक्स में पेस्ट करें। जुड़ी entities और enums शामिल करें, ताकि nested objects भर सकें।
  2. टाइप करते ही परिणाम बॉक्स अपडेट होता है। जिस entity को कोई दूसरी entity reference नहीं करती वह top-level JSON object बनती है; अगर कई असंबंधित entities हैं, तो हर एक अपने class नाम के तहत दिखती है।
  3. परिणाम के ऊपर दाईं ओर के copy बटन से JSON लें, या Ctrl/⌘+Enter दबाएं।

उदाहरण

इनपुट (TypeORM)
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;
}
आउटपुट (JSON)
{
  "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 बनता है।
  • Lazy relations (Promise<Post[]>) और ESM Relation<User> wrapper खुलकर अंदर का type बनते हैं।
  • TypeScript enums पहले member की वैल्यू इस्तेमाल करते हैं: string enums के लिए string, numeric के लिए संख्या।
  • class-transformer के @Exclude() लगे fields, static fields, methods और getters छोड़ दिए जाते हैं।
  • Date properties (@CreateDateColumn वगैरह से) 1970-01-01T00:00:00.000Z जैसी ISO strings बनती हैं।
  • jsonb columns पर आम types, जैसे Record<string, number>, {"key": 0} के रूप में दिखते हैं।
  • जब entities एक-दूसरे को reference करती हैं, तो दूसरी बार पहुंचने पर null या खाली array से काट दिया जाता है, ताकि nesting सीमित रहे।

उपयोग के मामले

NestJS controllers में Swagger examples

जो controllers सीधे entities लौटाते हैं उन्हें भी @ApiResponse के लिए example payload चाहिए। Entity फ़ाइल पेस्ट करें और परिणाम की placeholder values को असली जैसी values से बदलें।

Load हुए relations का preview

जब आप relations: ["author"] के साथ query करते हैं, तो frontend जानना चाहता है कि response कितना गहरा nested है। आउटपुट साफ़ दिखाता है कि कौन से relation fields objects हैं और कौन से arrays।

e2e tests के लिए fixtures

आउटपुट को supertest POST calls के request body के ढांचे के रूप में इस्तेमाल करें। id और timestamps जैसे बने हुए fields हटाएं, फिर बाकी भरें।

नोट्स और सीमाएँ

  • nullable, default या length जैसे @Column options नहीं पढ़े जाते; वैल्यू property type या literal initializer (status = "draft") से आती है। अगर कोई column null हो सकता है तो उस वैल्यू को खुद null करें।
  • एक-दूसरे को reference न करने वाली कई @Entity classes सभी बदली जाती हैं, class नाम से keyed ({ "Book": {...}, "Author": {...} })। एक-दूसरे को reference करने वाली entities फिर भी एक nested root बनाती हैं।
  • जो classes पेस्ट नहीं बल्कि import की गई हैं, वे टूल को पता नहीं होतीं और null के रूप में दिखती हैं।
  • ClassSerializerInterceptor के साथ इस्तेमाल होने वाले @Expose, serialization groups और @Transform लागू नहीं होते।

अक्सर पूछे जाने वाले प्रश्न

मेरा @PrimaryGeneratedColumn("uuid") खाली string क्यों है?

सिर्फ़ property type वैल्यू तय करता है। id: string "" देता है; decorator का "uuid" argument नहीं पढ़ा जाता। Documentation के लिए बाद में UUID जैसी वैल्यू डाल दें।

क्या entity भेजने पर मेरा API सच में यही लौटाता है?

यह पूरी तरह load हुई entity के JSON.stringify से मेल खाता है। जो relations आपने relations से load नहीं किए वे असली response में बस नहीं होते, इसलिए आउटपुट के relation fields को load हुई स्थिति मानें।

Decimal column संख्या दिखाता है, लेकिन मेरा API string लौटाता है। क्यों?

PostgreSQL और MySQL drivers precision खोने से बचने के लिए decimal values को strings के रूप में लौटाते हैं। Entity में property को price: string declare करें और आउटपुट भी, आपके असली data की तरह, string होगा।