-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (32 loc) · 892 Bytes
/
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
var levelup = require('levelup')
var noop = function() {}
var Proxy = function(db) {
this.db = db
this.leveldown = null
}
Proxy.prototype.open = function(opts, cb) {
if (typeof opts === 'function') return this.open(null, opts)
if (!cb) cb = noop
var self = this
this.db.open(function(err) {
if (err) return cb(err)
self.leveldown = self.db.db
cb()
})
}
'put get del batch approximateSize iterator close'.split(' ').forEach(function(m) {
Proxy.prototype[m] = function() {
return this.leveldown[m].apply(this.leveldown, arguments)
}
})
module.exports = function(db, defaults) {
if (!defaults) defaults = {}
defaults.db = function(l) {
return new Proxy(db)
}
var up = levelup(db.location || 'no-location', defaults)
up.on('ready', function() { // remove the proxy
if (up.db instanceof Proxy) up.db = up.db.leveldown
})
return up
}