【问题标题】:WebStorm Node.Js Sequelize Model type hintWebStorm Node.Js Sequelize Model 类型提示
【发布时间】:2016-01-08 08:43:53
【问题描述】:

我想知道如何在 WebStorm 中键入提示 node.js 对模型进行续集以更好地完成代码。

至少我能够弄清楚如何获得模型属性的代码完成。但我缺少 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)

并获得模型及其属性的代码完成。 现在我缺少像“upsert”、“create”、...这样的sequelize方法。

有没有人知道如何为那些也完成代码?

【问题讨论】:

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


    【解决方案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');
    }
    

    【讨论】:

      【解决方案2】:

      在 WebStorm 2019 中将鼠标悬停在您的 Sequelize 上包括:

      require('sequelize');
      

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

      WebStorm 文档中也记录了此功能,例如 express.js https://www.jetbrains.com/help/webstorm/configuring-javascript-libraries.html

      【讨论】:

        猜你喜欢
        • 2020-08-26
        • 1970-01-01
        • 2018-01-23
        • 2019-10-02
        • 2017-07-17
        • 2018-09-29
        • 1970-01-01
        • 1970-01-01
        • 2019-07-19
        相关资源
        最近更新 更多