Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

This is how far I got:

models/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

'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", ...

Does anybody know how to get code completion for those also?

like image 817
Ralf Avatar asked Nov 09 '22 03:11

Ralf


1 Answers

I usually add fake method for improve IDE autocomplete

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');
}
like image 104
a chernyavenko Avatar answered Nov 15 '22 05:11

a chernyavenko