-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
example.js
48 lines (39 loc) · 1.05 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
var level = require('level');
var Secondary = require('./');
var sub = require('level-sublevel');
var db = sub(level(__dirname + '/db', {
valueEncoding: 'json'
}));
var posts = db.sublevel('posts');
posts.byTitle = Secondary(posts, 'title');
posts.byLength = Secondary(posts, 'length', function(post){
return post.body.length;
});
posts.put('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);
posts.del('1337', function(err) {
if (err) throw err;
posts.byTitle.get('a title', function(err) {
console.log(err.name)
});
});
});
posts.byLength.createReadStream({
start: 10,
end: 20
}).on('data', console.log.bind(console, 'read'));
posts.byLength.createKeyStream({
start: 10,
end: 20
}).on('data', console.log.bind(console, 'key'));
posts.byLength.createValueStream({
start: 10,
end: 20
}).on('data', console.log.bind(console, 'value'));
});