-
Notifications
You must be signed in to change notification settings - Fork 2
/
kev.js
80 lines (69 loc) · 2.19 KB
/
kev.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
var KevMemory = require('./plugins/memory.js')
var KevMongo = require('./plugins/mongo.js')
var KevRedis = require('./plugins/redis.js')
var Kev = module.exports = function Kev(options) {
if (!(this instanceof Kev)) return new Kev(options)
if (!options.store) this.store = KevMemory()
else this.store = options.store
}
Kev.prototype.get = function get (keys, options, done) {
var single = typeof keys === 'string'
if (typeof options === 'function') {
done = options
options = {}
}
options = options || {}
this.store.get(single ? [keys] : keys, options, (e, v) => done(e, single && v ? v[keys] : v))
return this
}
Kev.prototype.put = Kev.prototype.set = function put (key, value, options, done) {
var keys = {}, single = typeof key === 'string'
if (single) {
keys[key] = value
single = true
} else if (Array.isArray(key)) {
keys = key.reduce((p, c) => { p[c] = value; return p }, {})
} else if (typeof key === 'object') {
keys = key
done = options
options = value
}
if (typeof options === 'function') { done = options; options = {} }
this.store.put(keys, options || {}, (e, v) => done && done(e, single && v ? v[key] : v))
return this
}
Kev.prototype.del = function del (key, done) {
var single = typeof key === 'string'
this.store.del(single ? [key] : key, (e, v) => done(e, single && v ? v[key] : v))
return this
}
Kev.prototype.drop = function drop (pattern, done) {
if (typeof pattern === 'function') {
done = pattern
pattern = '*'
}
this.store.drop(pattern, done)
return this
}
Kev.prototype.tag = function tag (key, tag, done) {
var single = typeof tag === 'string'
this.store.tag(key, single ? [tag] : tag, done)
return this
}
Kev.prototype.tags = function tags (key, done) {
var single = typeof key === 'string'
this.store.tags(single ? [key] : key, (e, v) => done && done(e, single && v ? v[key] : v))
return this
}
Kev.prototype.dropTag = function tag (tag, done) {
var single = typeof tag === 'string'
this.store.dropTag(single ? [tag] : tag, done)
return this
}
Kev.prototype.close = function close(done) {
this.store.close(done)
return this
}
Kev.Memory = KevMemory
Kev.Mongo = KevMongo
Kev.Redis = KevRedis