首页 > 解决方案 > Sequelize 模型的自动完成属性

问题描述

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

问题

标签: node.jssequelize.js

解决方案


推荐阅读