This repository has been archived by the owner on Apr 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlevel-multiply.js
82 lines (70 loc) · 2.11 KB
/
level-multiply.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
const after = require('after')
var getCallback = function (options, callback) {
return typeof options == 'function' ? options : callback
}
, get = function (db, keys, options, callback) {
if (Array.isArray(keys)) {
callback = getCallback(options, callback)
options = typeof options != 'object' || typeof options != 'string'
? {}
: options
var data = {}
, done = after(keys.length, function (err) {
if (err) return callback(err)
callback(null, data)
})
keys.forEach(function (key) {
db.get(key, options, function (err, value) {
if (err) {
if (err.name == 'NotFoundError')
value = null
else
return done(err)
}
data[key] = value
done()
})
})
} else {
db._multiply.get.call(db, keys, options, callback)
}
}
, put = function (db, key, value, options, callback) {
if (typeof key == 'object') {
var batch = Object.keys(key).reduce(function (p, k) {
if (Object.prototype.hasOwnProperty.call(key, k)) {
p.push({ type: 'put', key: k, value: key[k] })
}
return p
}, [])
db.batch(batch, value, options)
} else {
db._multiply.put.call(db, key, value, options, callback)
}
}
, del = function (db, keys, options, callback) {
if (Array.isArray(keys)) {
var batch = keys.map(function (k) {
return { type: 'del', key: k }
})
db.batch(batch, options, callback)
} else {
db._multiply.del.call(db, keys, options, callback)
}
}
, setup = function (db, mpfx) {
if (db._multiply)
return
if (typeof mpfx != 'string')
mpfx = ''
db._multiply = {
get : db.get
, put : db.put
, del : db.del
}
db[mpfx + 'get'] = get.bind(null, db)
db[mpfx + 'put'] = put.bind(null, db)
db[mpfx + 'del'] = del.bind(null, db)
return db
}
module.exports = setup