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
Full Documentation
Usage (basic)
const CoreController = ;const core_controller = resources;const mongoose = ;const User = mongoose; //Querying the Database//1. using aliased methodscore_controller ;//2. using methods directlycore_controllerdbdefault ;//3. using the meta propertycore_controllermeta ; //Implementing Routes//1. using aliased methodslet initialized = core_controller;/* initialized -> { router: //express router new: //middleware show: //middleware edit: //middleware index: //middleware remove: //middleware search: //middleware create: //middleware update: //middleware load: //middleware load_with_count: //middleware load_with_limit: //middleware paginate: //middleware } *///2. using methods directlylet initialized = core_controllerprotocolapi;//3. using the meta propertylet initialized = core_controllermeta; //Rendering a View//1. using aliased methodscore_controller;//2. using the utility respondercore_controller_utility_responder ;//3. rendering a from a different template enginecore_controller_utility_responderengine = ;core_controller_utility_responder ;
Usage (advanced)
const CoreController = ;const mongoose = ;const User = mongoose; //Configurations for protocol, db, and responder can be passed at startupvar core_controller = resource db_configuration: model_name: 'user' model: 'User' docid: 'email' adapter: 'mongo' protocol_configuration: adapter: 'http' api: 'rest' ;//Or they can be initialized individuallycore_controller;core_controller; //Querying the Database//1. Using the initialized db adapter/* As you can see with initialized adapters there is no need to pass model, model_name or other generically required options */core_controllerdbuser ;//2. Using the meta propertycore_controllermeta ; //Implementing Routes//1. Using the initialized protocol adapter/* This will implement a RESTful API route strategy for all initialized db adapters. You can access the initialized router at core_controller.protocol.router; */core_controllerprotocol;//2. Using the API adapter/* This returns an express router for the user model */var router = core_controllerprotocolapi;/* Individual controller methods can also be access via aliased methods, the meta property and the api adapter: core_controller.protocol.api.implement.INDEX({ model_name: 'user' }); core_controller.meta.initialize_index({ model_name: 'user' }); core_controller.controller_index({ model_name: 'user' }); */
Default Adapters
/* The new CoreController uses abstractions called adapters which allows for standardization of method names with differing functionality. For example the periodicjs.core.data modules exposes adapters that have CRUD methods for various database types ie. MongoDB, SQL, PostgreSQL while periodicjs.core.protocols exposes adapters for request types ie. HTTP, Web Socket and for API strategies ie. REST, JSON RPC, XML RPC. As such CoreController itself does not have access to many methods without initialized adapters, but for convenience CoreController will initialize a set of default adapters even if configurations are not provided. */const CoreController = ;const core_controller = resources;/* Even without adapter configurations core_controller will have a .protocol, .responder and .db.default property. With these default adapters core_controller has exposed methods for implementing routing, middleware, CRUD methods etc. By default: - .protocol is a HTTP request adapter with a REST API adapter - .responder is a JSON adapter - .db.defualt is a MongoDB adapter with no specified default model - Additionally, there is a HTML adapter available at core_controller._utility_responder This default functionality can be overridden by passing options.skip_responder, options.skip_db and options.skip_protocol as true */
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
//Compatibility mode can be disabled at initializationconst CoreController = ; var core_controller = resources compatibility: false;//Even with compatibility mode off a sub-set of aliased methods are available (see aliased method documentation for a complete list) //You can also instantiate the v7.5.1 CoreController onlyvar compatible_core_controller = resources;
Magical .meta Property
const CoreController = ;const mongoose = ;const User = mongoose; //Configurations for protocol, db, and responder can be passed at startupvar core_controller = resource db_configuration: model_name: 'user' model: 'User' docid: 'email' adapter: 'mongo' ; /* The .meta property on core_controller is a proxy that allows for sudo-aliasing of methods for easier access to methods nested in child objects *///There are four main points of access when using the meta property://1. Accessing nested methodscore_controllermetarender //-> Child protocol adapters are searched for the .render method and if it is found a bound copy of the function is returned //2. Accessing controller implementation functionscore_controllermetainitialize_index //-> If the property follows the syntax "initialize" + "_" + keyword representing controller implementation ie. new, index, edit a bound copy of the api adapter utility method will be returnedcore_controllemetainitialize = {} //All API utility methods indexed by method name //3. Accessing CRUD methodscore_controllermetaload //-> searches the controller.db.default object for a .load method and returns itcore_controllermetaloadUser //-> If the "user" model adapter has been loaded sudo-aliased methods are available ie. loadUser, createUser, updateUser //4. Direct Accesscore_controllermetaprotocol = core_controllerprotocol //-> Any properties that exist on core_controller are also accessible from the meta property
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
- Check out https://github.com/typesettin/periodicjs for the full Periodic Documentation
Testing
$ npm i$ grunt test
Contributing
License
MIT