-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
85 lines (71 loc) · 1.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Module dependencies
*/
var util = require('./lib/util');
var parsers = require('./lib/parsers');
var proto = require('./lib/proto');
/**
* Keep a list of registered databases
*/
var adapters = exports._adapters = {};
/**
* Save the default adapter
*/
var defaultAdapter = null;
/**
* Expose the adapters
*
* @param {String} name
* @return {SimpleDbAdapter}
*/
module.exports = exports = function(name) {
name = name || defaultAdapter;
if (!adapters[name]) throw new Error('simple-db adapter "' + name + '" not initialized');
return adapters[name];
};
/**
* Register `adapter` by `name`
*
* @param {String} name
* @param {SimpleDbAdapter} adapter
* @return {simpleDb}
*/
exports.use = function(name, adapter) {
adapters[name] = createDb(name, adapter);
if (!defaultAdapter) defaultAdapter = name;
return this;
};
/**
* Set the default adapter
*
* var simpleDb = require('simple-db')
* , riak = require('simple-db-riak');
*
* simpleDb.use('riak', riak(...));
* simpleDb.default('riak');
*
* var db = simpleDb(); // defaults to 'riak' adapter
*
* @param {String} name
* @return {simpleDb}
*/
exports.default = function(name) {
defaultAdapter = name;
return this;
};
/**
* Database interface
*
* @param {String} name
* @param {Object} adapter
* @api private
*/
function createDb(name, adapter) {
function db(method, bucket, key, fn) { return db.request(method, bucket, key, fn); };
util.merge(db, proto);
db._adapter = adapter;
db._name = name;
return db;
};
// expose the parsers
exports.parsers = parsers;