periodicjs.core.controller
Description
Periodic's Core Controller module, provides helpful controller methods for querying documents and rendering responses.
Extensions are node modules that are loaded when an instance of periodic is instantiated.
Installation
$ npm i periodicjs.core.controller
Usage (basic)
const CoreController = require('periodicjs.core.controller');
const core_controller = new CoreController(resources);
const mongoose = require('mongoose');
const User = mongoose.model('User');
core_controller.loadModel({
docid: 'email',
model: User,
query: 'somerandomemail@email.com'
})
.then(result => {
});
core_controller.db.default.load({
docid: 'email',
model: User,
query: 'somerandomemail@email.com'
})
.then(result => {
});
core_controller.meta.load({
docid: 'email',
model: User,
query: 'somerandomemail@email.com'
})
.then(result => {
});
let initialized = core_controller.controller_routes({
model_name: 'user'
});
let initialized = core_controller.protocol.api.implement({
model_name: 'user'
});
let initialized = core_controller.meta.initialize_routes({
model_name: 'user'
});
core_controller.renderView(req, res, 'examples/helloworld', {
name: 'user'
});
core_controller._utility_responder.render({
name: 'user'
}, {
viewname: 'examples/helloworld'
})
.then(result => {
core_controller.protocol.respond(req, res, { responder_override: result });
});
core_controller._utility_responder.engine = require('pug');
core_controller._utility_responder.render({
name: 'user'
}, {
viewname: 'examples/helloworld',
extname: '.pug',
})
.then(result => {
core_controller.protocol.respond(req, res, { responder_override: result });
});
Usage (advanced)
const CoreController = require('periodicjs.core.controller');
const mongoose = require('mongoose');
const User = mongoose.model('User');
var core_controller = new CoreController(resource, {
db_configuration: [{
model_name: 'user',
model: 'User',
docid: 'email',
adapter: 'mongo'
}],
protocol_configuration: {
adapter: 'http',
api: 'rest'
}
});
core_controller.initialize_db({
model_name: 'user',
model: 'User',
docid: 'email',
adapter: 'mongo'
});
core_controller.initialize_responder({ adapter: 'json' });
core_controller.db.user.load({
query: 'somerandomemail@email.com'
})
.then(result => {
});
core_controller.meta.loadUser({
query: 'somerandomemail@email.com'
})
.then(result => {
});
core_controller.protocol.implement();
var router = core_controller.protocol.api.implement({ model_name: 'user' });
Default Adapters
const CoreController = require('periodicjs.core.controller');
const core_controller = new CoreController(resources);
Compatibility Mode
By default CoreController will instantiate a version of v7.5.1 and make all prototype methods indexed by their original name. This is to ensure that older versions of periodicjs and its extensions and core modules are compatible with the newest version of periodicjs.core.controller
const CoreController = require('periodicjs.core.controller');
var core_controller = new CoreController(resources, {
compatibility: false
});
var compatible_core_controller = new CoreController.compatibility(resources);
Magical .meta Property
const CoreController = require('periodicjs.core.controller');
const mongoose = require('mongoose');
const User = mongoose.model('User');
var core_controller = new CoreController(resource, {
db_configuration: [{
model_name: 'user',
model: 'User',
docid: 'email',
adapter: 'mongo'
}]
});
core_controller.meta.render
core_controller.meta.initialize_index
core_controlle.meta.initialize = {}
core_controller.meta.load
core_controller.meta.loadUser
core_controller.meta.protocol = core_controller.protocol
Development
Make sure you have grunt installed
$ npm i -g grunt-cli jsdoc-to-markdown
For generating documentation
$ grunt doc
$ jsdoc2md lib/**/*.js utility/**/*.js index.js > doc/api.md
Notes
Testing
$ npm i
$ grunt test
Contributing
License
MIT