简体   繁体   English

Sequelize – 方法和属性的自动完成

[英]Sequelize – autocompletion for methods and attributes

This is my current setup:这是我当前的设置:

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);

I now want to ensure that all User attributes, as well as its model methods, are showing up in VSCode autocomplete.我现在想确保所有User属性及其 model 方法都显示在 VSCode 自动完成中。 How can I make this happen?我怎样才能做到这一点?

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

When I change the code to class User extends Model , then I can see the ahoy() being autocompleted, yet this does not apply to .email当我将代码更改为class User extends Model时,我可以看到 ahoy ahoy()正在自动完成,但这不适用于.email

Is there a way to fix this (eg with JSDoc)?有没有办法解决这个问题(例如使用 JSDoc)?

If you are using pure Javascript you won't be able to do that.如果您使用的是纯 Javascript,您将无法做到这一点。
Consider using Typescript, then VSCode (or any editor really) will be able to watch your Object and suggest what attribute you want from it.考虑使用 Typescript,然后 VSCode(或任何真正的编辑器)将能够观察您的 Object 并建议您想要从中获得什么属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM