-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
51 lines (40 loc) · 1.23 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
'use strict'
const fp = require('fastify-plugin')
const levelup = require('levelup')
const leveldown = require('leveldown')
const encode = require('encoding-down')
// mostly from level-packager
function levelMore (location, options, next) {
const store = options.store || leveldown
delete options.store
for (const m of ['destroy', 'repair']) {
if (typeof store[m] === 'function') {
levelMore[m] = store[m].bind(store)
}
}
return levelup(encode(store(location), options), options, next)
}
levelMore.errors = levelup.errors
function fastifyLeveldb (fastify, opts, next) {
if (!opts.name) {
return next(new Error('Missing database name'))
}
const { name, path } = opts
opts.options = opts.options || {}
if (!fastify.hasDecorator('level')) {
fastify.decorate('level', {})
}
if (fastify.level[name]) {
return next(new Error(`Level namespace already used: ${name}`))
}
fastify.addHook('onClose', (instance, done) => {
instance.level[name].close(done)
})
fastify.level[name] = levelMore(path || name, opts.options, next)
}
module.exports = fp(fastifyLeveldb, {
fastify: '5.x',
name: '@fastify/leveldb'
})
module.exports.default = fastifyLeveldb
module.exports.fastifyLeveldb = fastifyLeveldb