簡體   English   中英

Sequelize – 方法和屬性的自動完成

[英]Sequelize – autocompletion for methods and attributes

這是我當前的設置:

const { Model, DataTypes } = require('sequelize');

class CustomModel extends Model {
  static init(attributes, config) {
    return super.init(attributes, {
      ...config,
      timestamps: true,
      underscored: true,
      createdAt: 'created_at',
      updatedAt: 'updated_at',
    });
  }
}

class User extends CustomModel {
  ahoy() {
    const { email } = this.get();
    console.log(`Ahoy, ${email}`);
  }
}

User.init({
  id: {
    type: DataTypes.UUID,
    primaryKey: true,
    allowNull: false,
    defaultValue: DataTypes.UUIDV4,
  },
  email: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true
  },
}, myConfigHere);

我現在想確保所有User屬性及其 model 方法都顯示在 VSCode 自動完成中。 我怎樣才能做到這一點?

const user = await User.findOne({ where: { email: 'john@example.com' } });
user.email; // No autocomplete while typing
user.ahoy() // No autocomplete while typing

當我將代碼更改為class User extends Model時,我可以看到 ahoy ahoy()正在自動完成,但這不適用於.email

有沒有辦法解決這個問題(例如使用 JSDoc)?

如果您使用的是純 Javascript,您將無法做到這一點。
考慮使用 Typescript,然后 VSCode(或任何真正的編輯器)將能夠觀察您的 Object 並建議您想要從中獲得什么屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM