SAILS-HOOK-MONGOOLIZE
This hook ensures that SEQUELIZE and MONGOOSE orms are initialized post sails lift and exposes them in global scope.
- Disable sails ORM hook in .sailsrc
{
"hooks": {
"orm": false
}
}
- Define DB Connection in local.js file or corresponding environment file(production.js/staging.js) in this format
mongoolize: {
mongoose : {
uri: 'mongodb://USERNAME:PASSWORD@IP:PORT_NO',
options:{ useMongoClient: true }
},
sequelize: {
username: USERNAME,
password: PASSWORD,
database: DATABASE,
host: HOST,
port: PORT,
options: {
define: {
freezeTableName: true,
timestamps: false,
},
dialect: 'mysql',
}
}
},
- By default sails models lies under api/models. Add two folders to it i.e mongo and sql for separation of concerns. EG:
-- api
-- models
-- mysql
-- mongo
- Exposes Sequelize and Mongoose objects globally with
sequelize
andmongoose
aliases. - Exposes all models under sql and mongo globally with their corresponding file names as aliases.
- npm install sails-hook-mongoolize
module.exports = {
attributes: {},
table_name: TABLENAME,
classMethods: [],
options: {},
associations: function() {}
}
module.exports = {
attributes: {},
document_name: DOCUMENTNAME,
classMethods: [],
instanceMethods: [],
options: {},
hooks:[
{hook: {type: 'pre', event: 'validate'}, fn: (next)=> console.log(`${this.attribute} pre validate value`)}
]
}
- Kindly raise an issue. Looking for feedbacks and enhancements :)