controller-util
v1.0.32
Published
A utility to achieve controllers, controller organization
Downloads
4
Maintainers
Readme
ws: a node.js websocket library
sequelize-util
is a simple way to organice your sequelice models, connections...
Installing
npm install --save controller-util
Starting the controllers
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
Base Controller
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;
Your first controller
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;
Requiring the colection of controllers
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);
});