2011-05-14 05:47:26 +00:00
|
|
|
/*
|
|
|
|
* provider.js: Abstraction providing an interface into pluggable configuration storage.
|
|
|
|
*
|
2011-11-24 05:33:08 +00:00
|
|
|
* (C) 2011, Nodejitsu Inc.
|
2011-05-14 05:47:26 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-08-28 12:50:26 +00:00
|
|
|
var async = require('async'),
|
2011-11-23 02:22:09 +00:00
|
|
|
common = require('./common');
|
2011-09-16 10:49:47 +00:00
|
|
|
|
2011-06-05 05:35:54 +00:00
|
|
|
//
|
|
|
|
// ### function Provider (options)
|
|
|
|
// #### @options {Object} Options for this instance.
|
|
|
|
// Constructor function for the Provider object responsible
|
|
|
|
// for exposing the pluggable storage features of `nconf`.
|
|
|
|
//
|
2011-05-14 05:47:26 +00:00
|
|
|
var Provider = exports.Provider = function (options) {
|
2011-09-16 10:49:47 +00:00
|
|
|
//
|
|
|
|
// Setup default options for working with `stores`,
|
|
|
|
// `overrides`, `process.env` and `process.argv`.
|
|
|
|
//
|
2011-11-22 16:58:36 +00:00
|
|
|
options = options || {};
|
2011-11-23 02:22:09 +00:00
|
|
|
this.stores = {};
|
|
|
|
this.sources = [];
|
|
|
|
this.init(options);
|
2011-05-14 05:47:26 +00:00
|
|
|
};
|
|
|
|
|
2011-11-20 21:21:09 +00:00
|
|
|
//
|
2012-04-14 19:28:55 +00:00
|
|
|
// Define wrapper functions for using basic stores
|
2011-11-20 21:21:09 +00:00
|
|
|
// in this instance
|
|
|
|
//
|
2012-03-02 18:59:06 +00:00
|
|
|
['argv', 'env'].forEach(function (type) {
|
2011-11-20 21:21:09 +00:00
|
|
|
Provider.prototype[type] = function (options) {
|
|
|
|
return this.add(type, options);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
//
|
2012-07-10 05:50:18 +00:00
|
|
|
// ### function file (key, options)
|
2012-07-10 05:27:28 +00:00
|
|
|
// #### @key {string|Object} Fully qualified options, name of file store, or path.
|
2012-07-10 05:50:18 +00:00
|
|
|
// #### @path {string|Object} **Optional** Full qualified options, or path.
|
2012-07-10 05:27:28 +00:00
|
|
|
// Adds a new `File` store to this instance. Accepts the following options
|
2012-03-02 18:59:06 +00:00
|
|
|
//
|
2012-07-10 05:50:18 +00:00
|
|
|
// nconf.file({ file: '.jitsuconf', dir: process.env.HOME, search: true });
|
|
|
|
// nconf.file('path/to/config/file');
|
|
|
|
// nconf.file('userconfig', 'path/to/config/file');
|
|
|
|
// nconf.file('userconfig', { file: '.jitsuconf', search: true });
|
2012-07-10 05:27:28 +00:00
|
|
|
//
|
2012-07-10 05:50:18 +00:00
|
|
|
Provider.prototype.file = function (key, options) {
|
2012-07-10 05:27:28 +00:00
|
|
|
if (arguments.length == 1) {
|
2012-07-10 05:50:18 +00:00
|
|
|
options = typeof key === 'string' ? { file: key } : key;
|
2012-07-10 05:27:28 +00:00
|
|
|
key = 'file';
|
|
|
|
}
|
|
|
|
else {
|
2012-07-10 05:50:18 +00:00
|
|
|
options = typeof options === 'string'
|
|
|
|
? { file: options }
|
|
|
|
: options;
|
2012-03-02 18:59:06 +00:00
|
|
|
}
|
2012-07-10 05:27:28 +00:00
|
|
|
|
2012-07-10 05:50:18 +00:00
|
|
|
options.type = 'file';
|
2012-07-10 05:27:28 +00:00
|
|
|
return this.add(key, options);
|
2012-03-02 18:59:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Define wrapper functions for using
|
2011-11-20 21:21:09 +00:00
|
|
|
// overrides and defaults
|
|
|
|
//
|
|
|
|
['defaults', 'overrides'].forEach(function (type) {
|
|
|
|
Provider.prototype[type] = function (options) {
|
2011-12-19 22:37:16 +00:00
|
|
|
options = options || {};
|
2012-01-02 22:14:03 +00:00
|
|
|
if (!options.type) {
|
2011-12-19 22:37:16 +00:00
|
|
|
options.type = 'literal';
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-12-19 22:37:16 +00:00
|
|
|
return this.add(type, options);
|
2011-11-21 01:00:04 +00:00
|
|
|
};
|
2011-11-20 21:21:09 +00:00
|
|
|
});
|
|
|
|
|
2011-05-14 05:47:26 +00:00
|
|
|
//
|
2011-09-16 10:49:47 +00:00
|
|
|
// ### function use (name, options)
|
2011-05-14 05:47:26 +00:00
|
|
|
// #### @type {string} Type of the nconf store to use.
|
|
|
|
// #### @options {Object} Options for the store instance.
|
2012-04-14 19:28:55 +00:00
|
|
|
// Adds (or replaces) a new store with the specified `name`
|
|
|
|
// and `options`. If `options.type` is not set, then `name`
|
2011-09-16 10:49:47 +00:00
|
|
|
// will be used instead:
|
2011-05-14 05:47:26 +00:00
|
|
|
//
|
2011-09-16 10:49:47 +00:00
|
|
|
// provider.use('file');
|
|
|
|
// provider.use('file', { type: 'file', filename: '/path/to/userconf' })
|
|
|
|
//
|
|
|
|
Provider.prototype.use = function (name, options) {
|
2011-09-19 01:37:01 +00:00
|
|
|
options = options || {};
|
|
|
|
var type = options.type || name;
|
2011-06-25 04:34:07 +00:00
|
|
|
|
2011-09-16 10:49:47 +00:00
|
|
|
function sameOptions (store) {
|
2011-06-25 04:34:07 +00:00
|
|
|
return Object.keys(options).every(function (key) {
|
2011-09-16 10:49:47 +00:00
|
|
|
return options[key] === store[key];
|
2011-06-25 04:34:07 +00:00
|
|
|
});
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-11-23 02:22:09 +00:00
|
|
|
var store = this.stores[name],
|
2011-09-16 10:49:47 +00:00
|
|
|
update = store && !sameOptions(store);
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-09-16 10:49:47 +00:00
|
|
|
if (!store || update) {
|
|
|
|
if (update) {
|
|
|
|
this.remove(name);
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-09-16 10:49:47 +00:00
|
|
|
this.add(name, options);
|
2011-05-14 05:47:26 +00:00
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-06-25 04:34:07 +00:00
|
|
|
return this;
|
2011-05-14 05:47:26 +00:00
|
|
|
};
|
|
|
|
|
2011-09-16 10:49:47 +00:00
|
|
|
//
|
|
|
|
// ### function add (name, options)
|
|
|
|
// #### @name {string} Name of the store to add to this instance
|
|
|
|
// #### @options {Object} Options for the store to create
|
|
|
|
// Adds a new store with the specified `name` and `options`. If `options.type`
|
|
|
|
// is not set, then `name` will be used instead:
|
|
|
|
//
|
|
|
|
// provider.add('memory');
|
|
|
|
// provider.add('userconf', { type: 'file', filename: '/path/to/userconf' })
|
|
|
|
//
|
|
|
|
Provider.prototype.add = function (name, options) {
|
2011-09-19 01:37:01 +00:00
|
|
|
options = options || {};
|
|
|
|
var type = options.type || name;
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-11-23 02:22:09 +00:00
|
|
|
if (!require('../nconf')[common.capitalize(type)]) {
|
2011-09-19 01:37:01 +00:00
|
|
|
throw new Error('Cannot add store with unknown type: ' + type);
|
2011-09-16 10:49:47 +00:00
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-11-23 02:22:09 +00:00
|
|
|
this.stores[name] = this.create(type, options);
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-11-23 02:22:09 +00:00
|
|
|
if (this.stores[name].loadSync) {
|
|
|
|
this.stores[name].loadSync();
|
2011-09-19 01:37:01 +00:00
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-09-25 04:01:44 +00:00
|
|
|
return this;
|
2011-09-16 10:49:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
2012-04-14 19:28:55 +00:00
|
|
|
// ### function remove (name)
|
2011-09-16 10:49:47 +00:00
|
|
|
// #### @name {string} Name of the store to remove from this instance
|
|
|
|
// Removes a store with the specified `name` from this instance. Users
|
|
|
|
// are allowed to pass in a type argument (e.g. `memory`) as name if
|
|
|
|
// this was used in the call to `.add()`.
|
|
|
|
//
|
|
|
|
Provider.prototype.remove = function (name) {
|
2011-11-23 02:22:09 +00:00
|
|
|
delete this.stores[name];
|
2011-09-25 04:01:44 +00:00
|
|
|
return this;
|
2011-09-16 10:49:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// ### function create (type, options)
|
|
|
|
// #### @type {string} Type of the nconf store to use.
|
|
|
|
// #### @options {Object} Options for the store instance.
|
2012-04-14 19:28:55 +00:00
|
|
|
// Creates a store of the specified `type` using the
|
2011-09-16 10:49:47 +00:00
|
|
|
// specified `options`.
|
|
|
|
//
|
|
|
|
Provider.prototype.create = function (type, options) {
|
2011-11-23 02:22:09 +00:00
|
|
|
return new (require('../nconf')[common.capitalize(type.toLowerCase())])(options);
|
|
|
|
};
|
|
|
|
|
2011-11-23 21:41:07 +00:00
|
|
|
//
|
|
|
|
// ### function init (options)
|
|
|
|
// #### @options {Object} Options to initialize this instance with.
|
|
|
|
// Initializes this instance with additional `stores` or `sources` in the
|
2012-04-14 19:28:55 +00:00
|
|
|
// `options` supplied.
|
2011-11-23 21:41:07 +00:00
|
|
|
//
|
2011-11-23 02:22:09 +00:00
|
|
|
Provider.prototype.init = function (options) {
|
2011-11-23 21:41:07 +00:00
|
|
|
var self = this;
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-11-23 02:22:09 +00:00
|
|
|
//
|
|
|
|
// Add any stores passed in through the options
|
|
|
|
// to this instance.
|
|
|
|
//
|
|
|
|
if (options.type) {
|
|
|
|
this.add(options.type, options);
|
|
|
|
}
|
|
|
|
else if (options.store) {
|
|
|
|
this.add(options.store.name || options.store.type, options.store);
|
|
|
|
}
|
|
|
|
else if (options.stores) {
|
|
|
|
Object.keys(options.stores).forEach(function (name) {
|
|
|
|
var store = options.stores[name];
|
|
|
|
self.add(store.name || name || store.type, store);
|
|
|
|
});
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-11-23 02:22:09 +00:00
|
|
|
//
|
|
|
|
// Add any read-only sources to this instance
|
|
|
|
//
|
|
|
|
if (options.source) {
|
|
|
|
this.sources.push(this.create(options.source.type || options.source.name, options.source));
|
|
|
|
}
|
|
|
|
else if (options.sources) {
|
|
|
|
Object.keys(options.sources).forEach(function (name) {
|
|
|
|
var source = options.sources[name];
|
|
|
|
self.sources.push(self.create(source.type || source.name || name, source));
|
|
|
|
});
|
|
|
|
}
|
2011-09-16 10:49:47 +00:00
|
|
|
};
|
|
|
|
|
2011-05-14 05:47:26 +00:00
|
|
|
//
|
|
|
|
// ### function get (key, callback)
|
|
|
|
// #### @key {string} Key to retrieve for this instance.
|
|
|
|
// #### @callback {function} **Optional** Continuation to respond to when complete.
|
|
|
|
// Retrieves the value for the specified key (if any).
|
|
|
|
//
|
|
|
|
Provider.prototype.get = function (key, callback) {
|
2011-09-19 01:37:01 +00:00
|
|
|
//
|
|
|
|
// If there is no callback we can short-circuit into the default
|
|
|
|
// logic for traversing stores.
|
|
|
|
//
|
|
|
|
if (!callback) {
|
|
|
|
return this._execute('get', 1, key, callback);
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-09-19 01:37:01 +00:00
|
|
|
//
|
2012-04-14 19:28:55 +00:00
|
|
|
// Otherwise the asynchronous, hierarchical `get` is
|
2011-09-19 01:37:01 +00:00
|
|
|
// slightly more complicated because we do not need to traverse
|
|
|
|
// the entire set of stores, but up until there is a defined value.
|
|
|
|
//
|
|
|
|
var current = 0,
|
2014-02-11 18:40:41 +00:00
|
|
|
names = Object.keys(this.stores).reverse(),
|
2011-09-19 01:37:01 +00:00
|
|
|
self = this,
|
2012-06-13 07:14:56 +00:00
|
|
|
response,
|
|
|
|
mergeObjs = [];
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-09-19 01:37:01 +00:00
|
|
|
async.whilst(function () {
|
2011-11-20 21:21:09 +00:00
|
|
|
return typeof response === 'undefined' && current < names.length;
|
2011-09-19 01:37:01 +00:00
|
|
|
}, function (next) {
|
2011-11-23 02:22:09 +00:00
|
|
|
var store = self.stores[names[current]];
|
2011-09-19 01:37:01 +00:00
|
|
|
current++;
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-09-19 01:37:01 +00:00
|
|
|
if (store.get.length >= 2) {
|
|
|
|
return store.get(key, function (err, value) {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-09-19 01:37:01 +00:00
|
|
|
response = value;
|
2012-06-13 07:14:56 +00:00
|
|
|
|
|
|
|
// Merge objects if necessary
|
|
|
|
if (typeof response === 'object' && !Array.isArray(response)) {
|
|
|
|
mergeObjs.push(response);
|
|
|
|
response = undefined;
|
|
|
|
}
|
|
|
|
|
2011-09-19 01:37:01 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-09-19 01:37:01 +00:00
|
|
|
response = store.get(key);
|
2012-06-13 07:14:56 +00:00
|
|
|
|
|
|
|
// Merge objects if necessary
|
|
|
|
if (typeof response === 'object' && !Array.isArray(response)) {
|
|
|
|
mergeObjs.push(response);
|
|
|
|
response = undefined;
|
|
|
|
}
|
|
|
|
|
2011-09-19 01:37:01 +00:00
|
|
|
next();
|
|
|
|
}, function (err) {
|
2012-06-13 07:14:56 +00:00
|
|
|
if (!err && mergeObjs.length) {
|
|
|
|
response = common.merge(mergeObjs.reverse());
|
|
|
|
}
|
2011-09-19 01:37:01 +00:00
|
|
|
return err ? callback(err) : callback(null, response);
|
|
|
|
});
|
2011-05-14 05:47:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// ### function set (key, value, callback)
|
|
|
|
// #### @key {string} Key to set in this instance
|
|
|
|
// #### @value {literal|Object} Value for the specified key
|
|
|
|
// #### @callback {function} **Optional** Continuation to respond to when complete.
|
|
|
|
// Sets the `value` for the specified `key` in this instance.
|
|
|
|
//
|
|
|
|
Provider.prototype.set = function (key, value, callback) {
|
2011-08-28 12:50:26 +00:00
|
|
|
return this._execute('set', 2, key, value, callback);
|
2011-05-14 05:47:26 +00:00
|
|
|
};
|
|
|
|
|
2011-09-16 10:49:47 +00:00
|
|
|
//
|
|
|
|
// ### function reset (callback)
|
|
|
|
// #### @callback {function} **Optional** Continuation to respond to when complete.
|
|
|
|
// Clears all keys associated with this instance.
|
|
|
|
//
|
|
|
|
Provider.prototype.reset = function (callback) {
|
2012-04-14 19:28:55 +00:00
|
|
|
return this._execute('reset', 0, callback);
|
2011-09-16 10:49:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// ### function clear (key, callback)
|
|
|
|
// #### @key {string} Key to remove from this instance
|
|
|
|
// #### @callback {function} **Optional** Continuation to respond to when complete.
|
|
|
|
// Removes the value for the specified `key` from this instance.
|
|
|
|
//
|
|
|
|
Provider.prototype.clear = function (key, callback) {
|
|
|
|
return this._execute('clear', 1, key, callback);
|
|
|
|
};
|
|
|
|
|
2011-06-05 05:35:54 +00:00
|
|
|
//
|
2011-08-28 12:50:26 +00:00
|
|
|
// ### function merge ([key,] value [, callback])
|
2011-06-05 05:35:54 +00:00
|
|
|
// #### @key {string} Key to merge the value into
|
|
|
|
// #### @value {literal|Object} Value to merge into the key
|
2011-08-28 12:50:26 +00:00
|
|
|
// #### @callback {function} **Optional** Continuation to respond to when complete.
|
2012-04-14 19:28:55 +00:00
|
|
|
// Merges the properties in `value` into the existing object value at `key`.
|
2011-08-28 12:50:26 +00:00
|
|
|
//
|
|
|
|
// 1. If the existing value `key` is not an Object, it will be completely overwritten.
|
|
|
|
// 2. If `key` is not supplied, then the `value` will be merged into the root.
|
|
|
|
//
|
|
|
|
Provider.prototype.merge = function () {
|
|
|
|
var self = this,
|
|
|
|
args = Array.prototype.slice.call(arguments),
|
|
|
|
callback = typeof args[args.length - 1] === 'function' && args.pop(),
|
|
|
|
value = args.pop(),
|
|
|
|
key = args.pop();
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-08-28 12:50:26 +00:00
|
|
|
function mergeProperty (prop, next) {
|
|
|
|
return self._execute('merge', 2, prop, value[prop], next);
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-08-28 12:50:26 +00:00
|
|
|
if (!key) {
|
|
|
|
if (Array.isArray(value) || typeof value !== 'object') {
|
|
|
|
return onError(new Error('Cannot merge non-Object into top-level.'), callback);
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-08-28 12:50:26 +00:00
|
|
|
return async.forEach(Object.keys(value), mergeProperty, callback || function () { })
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-08-28 12:50:26 +00:00
|
|
|
return this._execute('merge', 2, key, value, callback);
|
|
|
|
};
|
|
|
|
|
2011-05-14 05:47:26 +00:00
|
|
|
//
|
|
|
|
// ### function load (callback)
|
|
|
|
// #### @callback {function} Continuation to respond to when complete.
|
|
|
|
// Responds with an Object representing all keys associated in this instance.
|
|
|
|
//
|
|
|
|
Provider.prototype.load = function (callback) {
|
2011-11-23 21:41:07 +00:00
|
|
|
var self = this;
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-11-23 21:41:07 +00:00
|
|
|
function getStores () {
|
2011-12-19 23:26:48 +00:00
|
|
|
var stores = Object.keys(self.stores);
|
|
|
|
stores.reverse();
|
2012-04-14 19:28:55 +00:00
|
|
|
return stores.map(function (name) {
|
2011-11-23 21:41:07 +00:00
|
|
|
return self.stores[name];
|
|
|
|
});
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-09-25 04:01:44 +00:00
|
|
|
function loadStoreSync(store) {
|
2011-09-16 10:49:47 +00:00
|
|
|
if (!store.loadSync) {
|
|
|
|
throw new Error('nconf store ' + store.type + ' has no loadSync() method');
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-09-16 10:49:47 +00:00
|
|
|
return store.loadSync();
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-09-25 04:01:44 +00:00
|
|
|
function loadStore(store, next) {
|
2011-09-16 10:49:47 +00:00
|
|
|
if (!store.load && !store.loadSync) {
|
|
|
|
return next(new Error('nconf store ' + store.type + ' has no load() method'));
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-09-16 10:49:47 +00:00
|
|
|
return store.loadSync
|
|
|
|
? next(null, store.loadSync())
|
|
|
|
: store.load(next);
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-09-25 04:46:28 +00:00
|
|
|
function loadBatch (targets, done) {
|
|
|
|
if (!done) {
|
|
|
|
return common.merge(targets.map(loadStoreSync));
|
|
|
|
}
|
|
|
|
|
|
|
|
async.map(targets, loadStore, function (err, objs) {
|
|
|
|
return err ? done(err) : done(null, common.merge(objs));
|
|
|
|
});
|
2011-05-14 05:47:26 +00:00
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-09-25 04:46:28 +00:00
|
|
|
function mergeSources (data) {
|
|
|
|
//
|
2012-04-14 19:28:55 +00:00
|
|
|
// If `data` was returned then merge it into
|
2011-09-25 04:46:28 +00:00
|
|
|
// the system store.
|
|
|
|
//
|
|
|
|
if (data && typeof data === 'object') {
|
2011-11-23 21:41:07 +00:00
|
|
|
self.use('sources', {
|
2012-04-14 19:28:55 +00:00
|
|
|
type: 'literal',
|
2011-11-23 21:41:07 +00:00
|
|
|
store: data
|
2011-09-25 04:46:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-09-25 04:46:28 +00:00
|
|
|
function loadSources () {
|
2011-12-20 01:34:39 +00:00
|
|
|
var sourceHierarchy = self.sources.splice(0);
|
|
|
|
sourceHierarchy.reverse();
|
|
|
|
|
2011-09-25 04:46:28 +00:00
|
|
|
//
|
2012-04-14 19:28:55 +00:00
|
|
|
// If we don't have a callback and the current
|
2011-09-25 04:46:28 +00:00
|
|
|
// store is capable of loading synchronously
|
|
|
|
// then do so.
|
|
|
|
//
|
|
|
|
if (!callback) {
|
2011-12-20 01:34:39 +00:00
|
|
|
mergeSources(loadBatch(sourceHierarchy));
|
2011-11-23 21:41:07 +00:00
|
|
|
return loadBatch(getStores());
|
2011-09-25 04:46:28 +00:00
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-12-20 01:34:39 +00:00
|
|
|
loadBatch(sourceHierarchy, function (err, data) {
|
2011-09-25 04:46:28 +00:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-09-25 04:46:28 +00:00
|
|
|
mergeSources(data);
|
2011-11-23 21:41:07 +00:00
|
|
|
return loadBatch(getStores(), callback);
|
2011-09-25 04:46:28 +00:00
|
|
|
});
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
|
|
|
return self.sources.length
|
2011-09-25 04:46:28 +00:00
|
|
|
? loadSources()
|
2011-11-23 21:41:07 +00:00
|
|
|
: loadBatch(getStores(), callback);
|
2011-05-14 05:47:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
2012-05-02 02:31:53 +00:00
|
|
|
// ### function save (callback)
|
|
|
|
// #### @callback {function} **optional** Continuation to respond to when
|
|
|
|
// complete.
|
|
|
|
// Instructs each provider to save. If a callback is provided, we will attempt
|
|
|
|
// asynchronous saves on the providers, falling back to synchronous saves if
|
|
|
|
// this isn't possible. If a provider does not know how to save, it will be
|
|
|
|
// ignored. Returns an object consisting of all of the data which was
|
|
|
|
// actually saved.
|
2011-05-14 05:47:26 +00:00
|
|
|
//
|
|
|
|
Provider.prototype.save = function (value, callback) {
|
2011-09-16 10:49:47 +00:00
|
|
|
if (!callback && typeof value === 'function') {
|
2011-05-14 05:47:26 +00:00
|
|
|
callback = value;
|
|
|
|
value = null;
|
2011-09-16 10:49:47 +00:00
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-11-20 21:21:09 +00:00
|
|
|
var self = this,
|
2011-11-23 02:22:09 +00:00
|
|
|
names = Object.keys(this.stores);
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2012-05-02 02:31:53 +00:00
|
|
|
function saveStoreSync(memo, name) {
|
2011-11-23 02:22:09 +00:00
|
|
|
var store = self.stores[name];
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-11-23 21:41:07 +00:00
|
|
|
//
|
2012-04-14 19:28:55 +00:00
|
|
|
// If the `store` doesn't have a `saveSync` method,
|
|
|
|
// just ignore it and continue.
|
2011-11-23 21:41:07 +00:00
|
|
|
//
|
2012-05-02 02:31:53 +00:00
|
|
|
if (store.saveSync) {
|
|
|
|
var ret = store.saveSync();
|
|
|
|
if (typeof ret == 'object' && ret !== null) {
|
|
|
|
memo.push(ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return memo;
|
2011-05-14 05:47:26 +00:00
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2012-05-02 02:31:53 +00:00
|
|
|
function saveStore(memo, name, next) {
|
2011-11-23 02:22:09 +00:00
|
|
|
var store = self.stores[name];
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-11-23 21:41:07 +00:00
|
|
|
//
|
2012-04-14 19:28:55 +00:00
|
|
|
// If the `store` doesn't have a `save` or saveSync`
|
|
|
|
// method(s), just ignore it and continue.
|
2011-11-23 21:41:07 +00:00
|
|
|
//
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2012-05-02 02:31:53 +00:00
|
|
|
if (store.save) {
|
2012-07-10 07:16:54 +00:00
|
|
|
return store.save(function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
2012-05-02 02:31:53 +00:00
|
|
|
if (typeof data == 'object' && data !== null) {
|
|
|
|
memo.push(data);
|
|
|
|
}
|
2012-07-10 07:16:54 +00:00
|
|
|
|
|
|
|
next(null, memo);
|
2012-05-02 02:31:53 +00:00
|
|
|
});
|
2012-07-10 07:16:54 +00:00
|
|
|
}
|
|
|
|
else if (store.saveSync) {
|
2012-05-02 02:31:53 +00:00
|
|
|
memo.push(store.saveSync());
|
|
|
|
}
|
2012-07-10 07:16:54 +00:00
|
|
|
|
2012-05-02 02:31:53 +00:00
|
|
|
next(null, memo);
|
2011-09-16 10:49:47 +00:00
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-09-16 10:49:47 +00:00
|
|
|
//
|
2012-04-14 19:28:55 +00:00
|
|
|
// If we don't have a callback and the current
|
2011-09-16 10:49:47 +00:00
|
|
|
// store is capable of saving synchronously
|
|
|
|
// then do so.
|
|
|
|
//
|
|
|
|
if (!callback) {
|
2012-05-02 02:31:53 +00:00
|
|
|
return common.merge(names.reduce(saveStoreSync, []));
|
2011-09-16 10:49:47 +00:00
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2012-05-02 02:31:53 +00:00
|
|
|
async.reduce(names, [], saveStore, function (err, objs) {
|
|
|
|
return err ? callback(err) : callback(null, common.merge(objs));
|
2012-04-14 19:28:55 +00:00
|
|
|
});
|
2011-08-23 10:38:51 +00:00
|
|
|
};
|
|
|
|
|
2011-08-28 12:50:26 +00:00
|
|
|
//
|
|
|
|
// ### @private function _execute (action, syncLength, [arguments])
|
|
|
|
// #### @action {string} Action to execute on `this.store`.
|
|
|
|
// #### @syncLength {number} Function length of the sync version.
|
|
|
|
// #### @arguments {Array} Arguments array to apply to the action
|
2011-09-16 10:49:47 +00:00
|
|
|
// Executes the specified `action` on all stores for this instance, ensuring a callback supplied
|
2011-08-28 12:50:26 +00:00
|
|
|
// to a synchronous store function is still invoked.
|
|
|
|
//
|
|
|
|
Provider.prototype._execute = function (action, syncLength /* [arguments] */) {
|
|
|
|
var args = Array.prototype.slice.call(arguments, 2),
|
2011-09-16 10:49:47 +00:00
|
|
|
callback = typeof args[args.length - 1] === 'function' && args.pop(),
|
2012-06-13 07:41:28 +00:00
|
|
|
destructive = ['set', 'clear', 'merge', 'reset'].indexOf(action) !== -1,
|
2011-09-16 10:49:47 +00:00
|
|
|
self = this,
|
2012-06-13 07:14:56 +00:00
|
|
|
response,
|
2014-02-12 18:31:55 +00:00
|
|
|
mergeObjs = [],
|
|
|
|
keys = Object.keys(this.stores);
|
|
|
|
|
|
|
|
if (action === 'get') {
|
|
|
|
keys = keys.reverse();
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-09-16 10:49:47 +00:00
|
|
|
function runAction (name, next) {
|
2011-11-23 02:22:09 +00:00
|
|
|
var store = self.stores[name];
|
2011-11-20 21:21:09 +00:00
|
|
|
|
2011-11-21 01:00:04 +00:00
|
|
|
if (destructive && store.readOnly) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2011-09-16 10:49:47 +00:00
|
|
|
return store[action].length > syncLength
|
|
|
|
? store[action].apply(store, args.concat(next))
|
|
|
|
: next(null, store[action].apply(store, args));
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-08-28 12:50:26 +00:00
|
|
|
if (callback) {
|
2014-02-12 18:31:55 +00:00
|
|
|
return async.forEach(keys, runAction, function (err) {
|
2011-09-16 10:49:47 +00:00
|
|
|
return err ? callback(err) : callback();
|
|
|
|
});
|
2011-08-28 12:50:26 +00:00
|
|
|
}
|
2011-09-16 10:49:47 +00:00
|
|
|
|
2014-02-12 18:31:55 +00:00
|
|
|
keys.forEach(function (name) {
|
2011-11-20 21:21:09 +00:00
|
|
|
if (typeof response === 'undefined') {
|
2011-11-23 02:22:09 +00:00
|
|
|
var store = self.stores[name];
|
2011-11-21 01:00:04 +00:00
|
|
|
|
|
|
|
if (destructive && store.readOnly) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-20 21:21:09 +00:00
|
|
|
response = store[action].apply(store, args);
|
2012-06-13 07:14:56 +00:00
|
|
|
|
|
|
|
// Merge objects if necessary
|
2012-09-09 01:09:03 +00:00
|
|
|
if (response && action === 'get' && typeof response === 'object' && !Array.isArray(response)) {
|
2012-06-13 07:14:56 +00:00
|
|
|
mergeObjs.push(response);
|
|
|
|
response = undefined;
|
|
|
|
}
|
2011-11-20 21:21:09 +00:00
|
|
|
}
|
2011-09-16 10:49:47 +00:00
|
|
|
});
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2012-06-13 07:14:56 +00:00
|
|
|
if (mergeObjs.length) {
|
|
|
|
response = common.merge(mergeObjs.reverse());
|
|
|
|
}
|
|
|
|
|
2011-08-28 12:50:26 +00:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Throw the `err` if a callback is not supplied
|
|
|
|
//
|
|
|
|
function onError(err, callback) {
|
|
|
|
if (callback) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-08-28 12:50:26 +00:00
|
|
|
throw err;
|
2012-05-02 02:31:53 +00:00
|
|
|
}
|