简体   繁体   English

WebStorm Node.Js Sequelize模型类型提示

[英]WebStorm Node.Js Sequelize Model type hint

I'd like to know how to type-hint node.js sequelize models in WebStorm for better code completion. 我想知道如何在WebStorm中键入提示node.js sequelize模型以获得更好的代码完成。

At least I was able to figure out, how to get code completion for model properties. 至少我能弄明白,如何获得模型属性的代码完成。 But I'm missing the model functions from sequelize. 但是我错过了sequelize的模型功能。

This is how far I got: 这是我得到了多远:

models/exampleModel.js 车型/ 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;
};

models/index.js 车型/ 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;

Now I'm able to type something like 现在我可以打字了

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)

and get code completion for the models and their properties. 并获得模型及其属性的代码完成。 Now I'm missing sequelize methods like "upsert", "create", ... 现在我缺少像“upsert”,“create”这样的续集方法......

Does anybody know how to get code completion for those also? 有谁知道如何获得代码完成?

I usually add fake method for improve IDE autocomplete 我通常添加假方法来改进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');
}

Inside WebStorm 2019 hover over your Sequelize include: 在WebStorm 2019中,将鼠标悬停在Sequelize上,包括:

require('sequelize');

You should see the yellow lightbulb. 你应该看到黄色的灯泡。 Click the yellow lightbulb and you be able to select "Install TypeScript definitions for better type information" 单击黄色灯泡,您可以选择“安装TypeScript定义以获得更好的类型信息”

This feature is also documented inside the WebStorm docs, the example there is for express.js https://www.jetbrains.com/help/webstorm/configuring-javascript-libraries.html WebStorm文档中也记录了此功能,例如express.js https://www.jetbrains.com/help/webstorm/configuring-javascript-libraries.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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