简体   繁体   中英

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. 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

Is there a way to fix this (eg with JSDoc)?

If you are using pure Javascript you won't be able to do that.
Consider using Typescript, then VSCode (or any editor really) will be able to watch your Object and suggest what attribute you want from it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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