I am trying to convert js code to ts code (and understanding why something have been done).
This my JS code
use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.js')[env];
const db = {};
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
Here, there are multiple sequalise
db.sequelize = sequelize;
db.Sequelize = Sequelize;
Can someone please help me in understand meaning, purpose and why both of them exist? and where we would use each of them.
I am able to make sense for sequelize but not for Sequelize. like we already have used new Sequelize( so why would we add it in db object?
Sequelize refers to the library itself while sequelize refers to an instance of Sequelize, which represents a connection to one database. This is the recommended convention and it is followed throughout the official documentation of sequelize also.
So for example a db.Sequelize.fn() is utilizing a specific function from sequelize module and db.sequelize.model.findAll() is utilizing an instance from a connection to a specific database.