-
Notifications
You must be signed in to change notification settings - Fork 2
Registry
Rig has a registry component that allows easy storage and retrieval of different resources you would want to be able to access from a middleware or a controller. You can access that component by calling rig.register() at startup time, and then later Rig.registry.register() or Rig.registry.get() at runtime. The register methods also make use of the Configuration library to configure the elements you give it to be registered. You can also have access to that configuration through the registry by calling Rig.registry.getConfig(). Registry is also used by the dispatcher middleware to configure and retrieve the controllers automatically.
# file system
myProject
|- middleware
| |- myMiddleware1.js
| ...
|- models
| |- myModel1.js
| ...
|- controllers
| ...
|- views
| ...
|- config.json
|- routes.json
|- server.js
|- package.json
...# config.json
{
"middleware": {
"myMiddleware1": {
"param1": "blah blah"
},
...
},
"controllers": {
...
},
"models": {
...
}
}// server.js
var Rig = require('rig'),
rig = new Rig({
config: 'config.json',
routes: 'routes.json'
});
rig.register('controllers');
rig.register('middleware');
rig.register('models');
rig.route();
rig.listen(3000);
console.log('front end app listening on port', 3000);The register method has two forms: it can take one or two parameters.
In its one-parameter form, the argument must be either an object literal or a string path. If it's a path, then the node package at that path will be registered with a name corresponding to the last segment of the path, or will recursively register all modules contained at that path, dot-prepended with the name of the containing directory.
Tip: this is useful when you want to register all your middleware at once, just store all your modules in a 'middleware' folder, create a 'middleware' property in your config, with each module name as a sub-property, and call
rig.register('middleware');. See the example above.
If it's an object, then register will be called in its two-argument form with each key-value pair as parameters.
###WARNING!!!!
For a resource to be considered an object,
instanceof Objecthas to betrue. Which means that if you assign a prototype to an object literal, theinstanceof Objectwill returntrueand all the enumerable properties of your instance will be registered as resources - which is probably undesirable. Instead of reassigning the prototype of a function to a new object, you should assign properties on the constructor individually. Read the doc of constructor and instanceof if you're a little rusty on those notions.
In it's two-argument form, register takes a string name and a resource argument.
- if the resource argument is a string path, then the function exported by the corresponding module that is registered at that name is exported with
register(name, resource)(see next bullet). - if the resource is a function then it is called with the configuration corresponding to the given name and the result is stored at that same name.
- if the resource is an object literal, then
registerwill be called in its two-argument form with each key-value pair, the first argument being the key appended to the given name separated by a dot:<name>.<key>. - else, the resource is simply stored at the given name.
Tip: In each of those versions, the name under which a resource is registered (whether implicitely the module name or explicitly given as the
nameargument) must correspond to a name given in the configuration (see here).
Pro Tip: To register a resource with a configuration that is generated at runtime instead of found in the config file, you can define the first argument of the configurable function as the runtime options (the second one coming from config.json) and give a bound method to the
registerfunction (see thebindfunction documentation here). Example:
// myController.js
module.exports = function (runtimeOptions, staticOptions) {
return function myController(req, res, next) {
// do something meaningful that can access both runtime and static option objects, thank you closure
}
}// server.js
var Rig = require('rig'),
rig = new Rig({
config: 'config.json',
routes: 'routes.json'
}),
crazyRuntimeOptions = {
foo: 'bar'
};
rig.register('controllers.myController', require('./controllers/myController.js').bind(null, crazyRuntimeOptions));
rig.route();
rig.listen(3000);Done! The configurable is now a one-argument function that register likes and will give the static config to.
The get method has two forms. Called without a parameter, it will return the object of all the resources indexed by their names. Called with a string name, it will return the resources stored at that name.
The getConfig method is just a shortcut to the get method of the config object: Without a parameter, it returns the config object as represented in the config file, with a string name parameter, it resolves the dot-separated path and returns the object at that path in the configuration as it is represented in the config file.