In my usecase, i maintain my database config/connections using sequelize
in a separate file models/index.js
. I export
the database connection object(called db
) so that other modules can connect to the database. But when I require
the object in some other file, no auto-completions are shown for that object nor its corresponding properties.
This is how my part of my models/index.js
file looks like, which i have done like the official sequelize example :-
const { Sequelize } = require('sequelize');
const sequelize = new Sequelize(config);
let db = {};
// for each of the files `file` in a dir...dynamically do(refer above linked example for details):-
let model = require(path.join(__dirname, file))(sequelize, Sequelize);
db[model.name] = model; // type Sequelize.Model
// dynamic config over
// statically add
db.sequelize = sequelize; // type Sequelize
module.exports = db
This is how i have imported in another module:-
const db = require("./models/index.js");
db.seq -------------------------------> where i try for auto-completion
When i require
my database connection object db
in another file, it should provide auto-completion for the properties i statically assigned, like db.sequelize
should be a completion and further db.sequelize.xxx
, where xxx
is the corresponding sequelize methods and properties should also be auto-completed.
Typing db.
doesn't show any valid auto-completions pertaining to the object db
. Also db.sequelize
has a type any
for the sequelize
part(so no further auto-completions), whereas I expected it to be Sequelize
.
jsdoc
, thinking that's the only way to make the auto-completions work, but to no use:-/**
* @typedef {import('sequelize').Sequelize} Sequelize
*/
const { Sequelize } = require('sequelize');
/**
* @type {Sequelize}
*/
const sequelize = new Sequelize(config);
/**
* key : string -> Name of the Sequelize.Model | 'sequelize'
* value : Either one of the Models or Sequelize object
* @type {Object.<string,(Sequelize|Sequelize.Model)>}
* @property {Sequelize} sequelize
* @example
* db = { User: db.User , seqeuelize : sequelize}
*/
let db = {};
// rest of the dynamic processing and adding Sequelize.Model to db
db.sequelize = sequelize;
module.exports = db
jsconfig.json
with following content, but that was also futile:-{
// NOTE THAT, this file resides within my src/server/ directory where all the js files exists
// the models/index.js resides within src/server
// I maintain another jsconfig for src/client
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"allowSyntheticDefaultImports": true,
"moduleResolution": "node",
"experimentalDecorators":true,
"baseUrl": ".",
"paths": {
"*": ["*"],
},
},
"include": ["./**/*"],
}
If you change your object let db = {};
to let db = {sequelize: null};
, you will get intellisense to work as intended.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.