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?
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');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With