簡體   English   中英

WebStorm Node.Js Sequelize模型類型提示

[英]WebStorm Node.Js Sequelize Model type hint

我想知道如何在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)

並獲得模型及其屬性的代碼完成。 現在我缺少像“upsert”,“create”這樣的續集方法......

有誰知道如何獲得代碼完成?

我通常添加假方法來改進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');
}

在WebStorm 2019中,將鼠標懸停在Sequelize上,包括:

require('sequelize');

你應該看到黃色的燈泡。 單擊黃色燈泡,您可以選擇“安裝TypeScript定義以獲得更好的類型信息”

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