WebStorm Node.Js Sequelize模型类型提示

时间:2015-10-10 13:31:08

标签: javascript node.js webstorm sequelize.js type-hinting

我想知道如何在WebStorm中输入提示node.js sequelize模型,以便更好地完成代码。

至少我能够弄明白,如何获得模型属性的代码完成。但是我错过了sequelize中的模型函数。

这是我走了多远:

模型/ exampleModel.js

/**
 * @module ExampleModel
 * @typedef {Object}
 * @property {ExampleModel} ExampleModel
 */


 /**
 *
 * @param sequelize {sequelize}
 * @param DataTypes {DataTypes}
 * @returns {Model}
 */
module.exports = function (sequelize, DataTypes) {
    var ExampleModel = sequelize.define('ExampleModel', {
       id: {
          type: DataTypes.BIGINT.UNSIGNED,
          primaryKey: true
       },

       someProperty: {
          type: DataTypes.BOOLEAN,
          defaultValue: true
       }
    }, {
        classMethods: {
            associate: function (models) {
               ExampleModel.belongsTo(models.AnotherModel, {foreignKey: 'id'});
            }
        }
    });
    return ExampleModel;
};

模型/ index.js

'use strict';

/**
 * @module models
 * @typedef {Object} models
 * @property {ExampleModel} ExampleModel
 * @property {AnotherModel} AnotherModel
 */

var db        = {};

// code that automatically fills db with models from files
// resulting in something like { ExampleModel : ExampleModel, AnotherModel: AnotherModel}

module.exports = db;

现在我可以输入类似

的内容
var models = require(__base + 'models');
models.Ex // Webstorm suggets "ExampleModel"
models.ExampleModel. // WebStorm suggets "id", "someProperty", "classMethods" (the last one is weird, but doesn't matter)

并获取模型及其属性的代码完成。 现在我缺少像#34; upsert","创建",...

的续集方法

有人知道如何为这些人获得代码完成吗?

2 个答案:

答案 0 :(得分:1)

我通常添加假方法来改进IDE自动完成

db.sequelize = sequelize;
db.Sequelize = Sequelize;

// eslint-disable-next-line
function enableAutocomplete() {

  /**  @type {Model|Address|*} */
  db.Address = require('./address')();

  /**  @type {Model|Group|*} */
  db.Group = require('./group')();

  throw new Error('this function for IDE autocomplete');
}

答案 1 :(得分:0)

内部WebStorm 2019悬停在您的Sequelize上包括:

require('sequelize');

您应该看到黄色的灯泡。单击黄色灯泡,然后可以选择“安装TypeScript定义以获取更好的类型信息”

此功能还记录在WebStorm文档中,该示例适用于express.js https://www.jetbrains.com/help/webstorm/configuring-javascript-libraries.html