-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
152 lines (118 loc) · 2.65 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Dependencies.
*/
var promise = require('bluff')
var delegate = require('./lib/adapter')
var emitter = require('emitter-component')
/**
* Expose `datastore`
*/
module.exports = datastore
/**
* Create a datastore.
*
* @param {Object?} data
* @param {Function?} adapter
* @api public
*/
function datastore(data, adapter) {
return datastore.factory({}, data, adapter)
}
/**
* Mixin an object with a datastore.
*
* @api private
*/
datastore.factory = function(obj, data, adapter) {
data = data || {}
/**
* A datastore is an event emitter.
*/
var store = emitter(obj)
/**
* Delegate operations to the datastore adapter.
* (Fallback in memory)
*/
var proxy = delegate(store, data, adapter)
/**
* Returns the value associated to the key, or undefined if there is none.
* (synchronous version)
*
* @param {Any} key
* @return value associated to the key, or undefined if there is none
* @api public
*/
store.get = function(key) {
return data[key]
}
/**
* Returns the value associated to the key, or undefined if there is none.
* (asynchronous version)
*
* @param {Any} key
* @return value associated to the key, or undefined if there is none
* @return {Promise}
* @api public
*/
store.pull = function(key) {
return promise(function(resolve) {
proxy('pull', resolve, key)
})
}
/**
* Sets the value for the key in the datastore.
*
* @param {Any} key
* @param {Any} value
* @return {Promise}
* @api public
*/
store.set = function(key, value) {
var cb = function(val) {
return promise(function(resolve) {
proxy('set', function(key, entry) {
resolve(entry)
}, key, val)
})
}
return (value == null && typeof key != 'object') ? cb : cb(value)
}
/**
* Removes any value associated to the key.
*
* @param {Any} key\
* @return {Promise}
* @api public
*/
store.del = function(key) {
return promise(function(resolve) {
proxy('del', resolve, key)
})
}
/**
* Returns a boolean asserting whether a value has been associated
* to the key in the datastore or not.
*
* @param {String} key
* @return {Boolean}
* @return {Promise}
* @api public
*/
store.contains = function(key) {
return data.hasOwnProperty(key)
}
/**
* Returns a promise asserting whether a value has been associated
* to the key in the datastore or not.
*
* @param {String} key
* @return {Boolean}
* @api public
*/
store.has = function(key) {
return promise(function(resolve) {
proxy('has', resolve, key)
})
}
return store
}