-
Notifications
You must be signed in to change notification settings - Fork 3
/
example.js
61 lines (51 loc) · 1.54 KB
/
example.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
var level = require('memdb')
var AutoIndex = require('./')
var keyReducer = AutoIndex.keyReducer
var sub = require('subleveldown')
var db = level()
var posts = sub(db, 'posts', { valueEncoding: 'json' })
var idx = {
title: sub(db, 'title'),
length: sub(db, 'length')
}
// add a title index
posts.byTitle = AutoIndex(posts, idx.title, keyReducer('title'))
// add a length index
// append the post.id for unique indexes with possibly overlapping values
posts.byLength = AutoIndex(posts, idx.length, function (post) {
return post.body.length + '!' + post.id
})
posts.put('1337', {
id: '1337',
title: 'a title',
body: 'lorem ipsum'
}, function (err) {
if (err) throw err
posts.byTitle.get('a title', function (err, post) {
if (err) throw err
console.log('get', post)
// => get: { id: '1337', title: 'a title', body: 'lorem ipsum' }
posts.del('1337', function (err) {
if (err) throw err
posts.byTitle.get('a title', function (err) {
console.log(err.name)
// => NotFoundError
})
})
})
posts.byLength.createReadStream({
start: 10,
end: 20
}).on('data', console.log.bind(console, 'read'))
// => read { key: '1337', value: { id: '1337', title: 'a title', body: 'lorem ipsum' } }
posts.byLength.createKeyStream({
start: 10,
end: 20
}).on('data', console.log.bind(console, 'key'))
// => key 1337
posts.byLength.createValueStream({
start: 10,
end: 20
}).on('data', console.log.bind(console, 'value'))
// => value { id: '1337', title: 'a title', body: 'lorem ipsum' }
})