Sequelize (TS) मॉडल को JSON में बदलें!

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

Sequelize (TS) मॉडल को JSON में बदलें!

इसे sequelize-typescript से लिखी @Table model classes दें और यह instance के toJSON() आउटपुट जैसे आकार का उदाहरण JSON बनाता है। इसमें वे id, createdAt, updatedAt और deletedAt attributes भी शामिल हैं जिन्हें Sequelize class में declare न होने पर भी जोड़ता है।

JS पेज के विपरीत, यहां values DataType argument से नहीं बल्कि हर property के TypeScript type से आती हैं। amount: string के रूप में declare किया @Column(DataType.DECIMAL) string बनाता है।

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

  1. @Table classes वाली फ़ाइल को Sequelize (TS) बॉक्स में पेस्ट करें। Nested values पाने के लिए (@HasMany वगैरह से) जुड़े models भी जोड़ें।
  2. अपने-आप जुड़े attributes वाले JSON के लिए परिणाम बॉक्स देखें।
  3. Date fields कैसे बदलते हैं यह तुरंत देखने के लिए timestamps या paranoid जैसे @Table options चालू-बंद करें।

उदाहरण

इनपुट (sequelize-typescript)
@Table({ tableName: "invoices", paranoid: true })
export class Invoice extends Model {
  @Column(DataType.STRING(20))
  number: string;

  @Column(DataType.DECIMAL(10, 2))
  amount: string;

  @Column(DataType.ENUM("draft", "sent", "paid"))
  status: "draft" | "sent" | "paid";

  @ForeignKey(() => Customer)
  @Column
  customerId: number;
}
आउटपुट (JSON)
{
  "id": 0,
  "number": "",
  "amount": "",
  "status": "draft",
  "customerId": 0,
  "createdAt": "1970-01-01T00:00:00.000Z",
  "updatedAt": "1970-01-01T00:00:00.000Z",
  "deletedAt": null
}

Class में कोई primary key नहीं है, इसलिए सबसे पहले id जुड़ता है, और paranoid: true deletedAt: null जोड़ता है। status अपने union का पहला literal "draft" लेता है, और amount खाली string है क्योंकि वह string के रूप में declared है।

समर्थित सुविधाएँ

  • @PrimaryKey, @Column({ primaryKey: true }) या id property के बिना, root और जुड़े models दोनों के लिए सबसे ऊपर id: 0 डाला जाता है।
  • जब तक @Table timestamps: false सेट न करे, createdAt और updatedAt जुड़ते हैं; createdAt: "created_on", updatedAt: false और paranoid: true (deletedAt: null) जैसे बदलाव हर model के हिसाब से माने जाते हैं।
  • @HasMany से Post[] array बनता है; @BelongsTo और @HasOne nested objects बनते हैं।
  • "draft" | "sent" जैसे literal unions पहली वैल्यू इस्तेमाल करते हैं, और extends Model से inherit हुई हर चीज़ अनदेखी होती है।
  • Object type वाला DataType.JSON column, जैसे { theme: string }, उसी आकार में भरा जाता है।

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

Express handlers का documentation

res.json(await Invoice.findByPk(id)) करने वाले endpoints के लिए आउटपुट ऐसा response example देता है जिसकी keys असलियत से मेल खाती हैं, अपने-आप जुड़े attributes समेत।

Soft delete चालू करने से पहले और बाद

paranoid चालू करने पर हर response में deletedAt जुड़ता है। API consumers को ठीक-ठीक बताने के लिए कि क्या बदलेगा, option चालू और बंद करके आउटपुट compare करें।

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

  • Root के अंदर nested जुड़े models को अपने @Table options से अपना id और timestamps मिलते हैं, लेकिन Sequelize जो foreign keys अप्रत्यक्ष रूप से जोड़ता है (जैसे @ForeignKey column के बिना @HasMany target पर invoiceId), वे गढ़े नहीं जाते।
  • allowNull और @Column({ field: ... }) से column renames अनदेखे होते हैं, क्योंकि JSON keys property नाम हैं। @Default("x") और @Column({...}) में literal defaultValue sample values के रूप में इस्तेमाल होते हैं।
  • underscored: true सिर्फ़ database column नाम बदलता है; toJSON() फिर भी attribute नाम इस्तेमाल करता है, इसलिए आउटपुट में createdAt सही है।
  • Values TypeScript types से आती हैं, इसलिए सिर्फ़ decorator से type पाने वाली property (बिना annotation के @Column(DataType.INTEGER) count;) null बनती है।

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

DECIMAL column का type number हो या string?

MySQL और PostgreSQL के साथ Sequelize default रूप से DECIMAL values strings के रूप में लौटाता है। इसे amount: string type देना type को ईमानदार रखता है, और यहां का उदाहरण भी string दिखाएगा।

क्या declare fields या CreationOptional इस्तेमाल कर सकते हैं?

हां। declare id: CreationOptional<number> number के रूप में पढ़ा जाता है, और यही खोलना NonAttribute<...> और ForeignKey<...> पर भी लागू होता है। इनके अलावा InferAttributes जैसे helper types नहीं फैलाए जाते।

क्या यह पेज JavaScript में लिखे sequelize.define models संभालता है?

नहीं। यह पेज classes और TypeScript types पढ़ता है। define और Model.init models के लिए Sequelize (JS) to JSON पेज इस्तेमाल करें, जो इसकी जगह DataTypes पढ़ता है।