-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
41 lines (34 loc) · 974 Bytes
/
db.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
const _fp = require('lodash/fp')
const getStream = require('get-stream')
const level = require('level')
const levelPromise = require('level-promise')
const sublevel = require('subleveldown')
const db = level(process.env.DB, {valueEncoding: 'json'})
function getVals () {
return getStream.array(this.createValueStream())
}
function getKeys () {
return getStream.array(this.createKeyStream())
}
function getBy (predicate) {
return this.getVals().then(_fp.filter(predicate))
}
function sub (name) {
const subdb = sublevel(db, name, {valueEncoding: 'json'})
subdb.getVals = getVals.bind(subdb)
subdb.getKeys = getKeys.bind(subdb)
subdb.getBy = getBy.bind(subdb)
return subdb
}
const movie = levelPromise(sub('movie'))
const review = levelPromise(sub('review'))
const session = sub('session')
const similarity = levelPromise(sub('similarity'))
const user = levelPromise(sub('user'))
module.exports = {
movie,
review,
session,
similarity,
user,
}