reagis
v1.0.4
Published
Simple key-value registry to store global values
Downloads
9
Maintainers
Readme
reagis
Need a place to store values that persist through out multiple modules? Wonder where to put your knex instance, your mongoose connection? Struggling to deal with circular dependencies when declaring relationships in ORMs?
reagis is just what you need. A simple key-value store designed to house your global singletons.
Installation
This is a Node.js module available on the npm registry.
Before proceeding, make sure you have Node.js and npm installed.
Installation is done using the
npm install
command:
npm install --save reagis
Documentation
- Website and Documentation
- Checkout API.md for detailed references
Quick start
Just require reagis and start setting/getting your values
// config/knex.js
const registry = require('reagis');
registry.set('knex', knex({ ... }));
registry.set('bookshelf', registry.get('knex'));
// models/user.js
const registry = require('reagis');
const User = registry.get('bookshelf').Model.extends({
tableName : 'users',
stories : function() {
return this.hasMany(registry.get('models.story'));
}
});
registry.set('models.user', User);
module.exports = User;
Bulk load
When your values are loaded from files or need to be loaded all at once, or you're just too lazy to do call registry.set many times.
const registry = require('reagis');
const knex = require('knex')({ ... });
const bookshelf = require('bookshelf')(knex);
registry.load({
knex : knex,
bookshelf : bookshelf,
});
Custom registry
In case you want to extend or instantiate your own registry, just import the exposed Registry class.
const { Registry } = require('reagis');
// Extending base Registry
class ModelRegistry extends Registry {
model(key, value) {
if (this.has(key)) return this.get(key);
this.set(key, value);
return value;
}
}
// Instantiate
const registry = new ModelRegistry();