Sequelize 模型的自动完成属性

dna*_*ion 5 node.js sequelize.js

从sequelize 文档中获取这个简单的示例。

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

const sequelize = new Sequelize('sqlite::memory');

class User extends Model {}

User.init(
  {
    // Model attributes are defined here
    firstName: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    lastName: {
      type: DataTypes.STRING,
      // allowNull defaults to true
    },
  },
  {
    // Other model options go here
    sequelize, // We need to pass the connection instance
    modelName: 'User', // We need to choose the model name
  },
);

async function main() {
  await User.sync();

  await User.create({
    firstName: 'Ruby',
    lastName: 'Gem',
  });

  const ruby = await User.findOne({
    where: {
      firstName: 'Ruby', // how to get auto complete for Model attributes? example: firstName
    },
  });

  const rubyFirstName = ruby.firstName; // how to get auto complete for the Model instance?

  console.log(rubyFirstName);
  console.log(ruby);
}

main();
Run Code Online (Sandbox Code Playgroud)

问题

  • 有没有办法让 VSCode 智能自动完成where对象中的模型属性?
  • 另外,如何让自动完成功能适用于模型实例的属性?(请阅读上面代码片段中对 main 函数的注释)