-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.js
63 lines (55 loc) · 1.82 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
62
63
const kappa = require('kappa-core')
const ram = require('random-access-memory')
const collect = require('collect-stream')
const memdb = require('memdb')
const Query = require('./')
const { validator } = require('./util')
const { cleaup, tmp } = require('./test/util')
const core = kappa(ram, { valueEncoding: 'json' })
const db = memdb()
const indexes = [
{ key: 'log', value: [['value', 'timestamp']] },
{ key: 'typ', value: [['value', 'type'], ['value', 'timestamp']] },
{ key: 'cha', value: [['value', 'type'], ['value', 'content', 'channel'], ['value', 'timestamp']] }
]
core.use('query', Query(db, { indexes, validator }))
core.writer('local', (err, feed) => {
const data = [{
type: 'chat/message',
timestamp: Date.now(),
content: { body: 'Hi im new here...' }
}, {
type: 'user/about',
timestamp: Date.now(),
content: { name: 'Grace' }
}, {
type: 'chat/message',
timestamp: Date.now(),
content: { body: 'Second post' }
}, {
type: 'chat/message',
timestamp: Date.now(),
content: { channel: 'dogs', body: 'Lurchers rule' }
}, {
type: 'chat/message',
timestamp: Date.now(),
content: { channel: 'dogs', body: 'But sometimes I prefer labra-doodles' }
}, {
type: 'user/about',
timestamp: Date.now(),
content: { name: 'Poison Ivy' }
}]
feed.append(data, (err, _) => {
core.ready('query', () => {
const query = [{ $filter: { value: { type: 'chat/message', content: { channel: 'dogs' } } } }]
collect(core.api.query.read({ query }), (err, chats) => {
if (err) return console.error(err)
console.log(chats)
collect(core.api.query.read({ query: [{ $filter: { value: { type: 'user/about' } } }] }), (err, users) => {
if (err) return console.error(err)
console.log(users)
})
})
})
})
})