db.js
* @author Hermann Mayer <hermann.mayer92@gmail.com>
var Connection = require('../db/connection');
var DBStore = function(config)
* Configure all selected backends and/or connections.
* @param {Object} select - Selection of backends and connections
* @param {Function} callback - Function to call on finish
DBStore.prototype.configure = function(select, callback)
return callback && callback();
var backendsSelected = Object.keys(select);
// If we got no selection - use all backends with all connections
if (0 === backendsSelected.length) {
Object.keys(this.config).forEach(function(backend) {
if (!map.hasOwnProperty(backend)) {
// Map all connections for the current backend
Object.keys(self.config[backend]).forEach(function(connection) {
self.config[backend][connection]
// Walk throuh all selected backends
backendsSelected.forEach(function(backend) {
// We got backends selected, proof their config existence
if (!self.config.hasOwnProperty(backend)) {
throw new Error('Requested backend "' + backend + '" is not configured.');
if (!map.hasOwnProperty(backend)) {
var backendConnections = Object.keys(select[backend]).length;
// Map all connections for the current backend
Object.keys(self.config[backend]).forEach(function(connection) {
self.config[backend][connection]
// We got no specific connections selection - use all
if (0 === backendConnections) {
// Iterate over all selected connections of the current backend
select[backend].forEach(function(connection) {
if (!self.config[backend].hasOwnProperty(connection)) {
'Requested backend "' + backend +
'" has no configured "' + connection + '" connection'
self.config[backend][connection]
// Prepare connections for async configure
Object.keys(map).forEach(function(backend) {
connections = connections.concat(map[backend]);
if (0 === connections.length) {
// Configure all connections for all backends parallel
async.each(connections, function(connection, callback) {
// Write the connection to the DB Store
if (!self.store[connection.backend]) {
self.store[connection.backend] = {};
self.store[connection.backend][connection.name] = connection.connection;
connection.connection.configure(callback);
logger.info('All ' + 'backend connections'.yellow + ' established');
* Close all connections for this store.
* @param {Function} callback - Function to call on finish
DBStore.prototype.close = function(callback)
Object.keys(this.store).forEach(function(backend) {
Object.keys(self.store[backend]).forEach(function(connection) {
connections.push(self.store[backend][connection]);
async.each(connections, function(connection, callback) {
* Get a connection by namepath from the store.
* @param {String} namepath - The namepath to search the backend/connection for
DBStore.prototype.get = function(namepath)
throw new Error('Namepath was not given');
namepath = namepath.split('.');
var backend = namepath.shift();
var connection = namepath.join('.');
if (!backend || !connection) {
throw new Error('Namepath "' + namepath + '" is faulty');
if (!this.store.hasOwnProperty(backend)) {
throw new Error('Backend "' + backend + '" is not registered');
if (!this.store[backend].hasOwnProperty(connection)) {
'Backend "' + backend + '" got no connection "' +