sequelize-util
is a simple way to organice your sequelice models, connections...
npm install --save controller-util
var ControllerUtil = require('controller-util');
/**
* CREATE INSTANCE OF CONTROLLER
* PRODUCTION & DEVELLOPER...
**/
var controllers = ControllerUtil({
controllers: "your path directory where file models are." (use the full path).
}, "example-prod");
/*
* INIT THE CONTROLLERS WITH NEEDED
* baseController.init()
*/
controllers.init('prod');
//baseController file is needed, to be executed
Create the baseController file and put it in folder with all controller files. filename: baseController.js
function baseController() {
var shareObject = {
value: 'One for all, all for one.'
};
function init(env = 'prod') {
this._ENV = env;
this.shareObject = shareObject;
}
this.init = init;
}
module.exports = baseController;
Lest create persons controller filename: persons.js
function persons() {
var shareObject = this.shareObject;
var store = [];
function create(name, lastname) {
var person = {
name,
lastname,
motto: shareObject.value
}
store.push(person);
return person;
}
/*
* SOME ASYNC WORK
*/
function savePerson(person) {
return new Promise((resolve, reject)=>{
//YOUR ASYNC TASK
resolve('Done :)', person);
});
}
this.create = create;
this.save = save;
}
module.exports = persons;
var controllers = require('controller-util').getSchema("example-dev");
controllers.persons.create('Juan','Perez').then(person=>{
console.log(person);
}).catch(err=>{
console.log(err);
});
controllers.persons.save(person).then(person=>{
console.log(person);
}).catch(err=>{
console.log(err);
});