इसे 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 बनाता है।
@Table classes वाली फ़ाइल को Sequelize (TS) बॉक्स में पेस्ट करें। Nested values पाने के लिए (@HasMany वगैरह से) जुड़े models भी जोड़ें।परिणाम बॉक्स देखें।timestamps या paranoid जैसे @Table options चालू-बंद करें।@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;
}{
"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 हुई हर चीज़ अनदेखी होती है।DataType.JSON column, जैसे { theme: string }, उसी आकार में भरा जाता है।res.json(await Invoice.findByPk(id)) करने वाले endpoints के लिए आउटपुट ऐसा response example देता है जिसकी keys असलियत से मेल खाती हैं, अपने-आप जुड़े attributes समेत।
paranoid चालू करने पर हर response में deletedAt जुड़ता है। API consumers को ठीक-ठीक बताने के लिए कि क्या बदलेगा, option चालू और बंद करके आउटपुट compare करें।
@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 सही है।@Column(DataType.INTEGER) count;) null बनती है।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 नहीं फैलाए जाते।
sequelize.define models संभालता है?नहीं। यह पेज classes और TypeScript types पढ़ता है। define और Model.init models के लिए Sequelize (JS) to JSON पेज इस्तेमाल करें, जो इसकी जगह DataTypes पढ़ता है।