-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.js
68 lines (57 loc) · 1.78 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
64
65
66
67
68
const kappa = require('kappa-core')
const Query = require('./')
const ram = require('random-access-memory')
const memdb = require('memdb')
const level = require('level')
const pull = require('pull-stream')
const core = kappa(ram, { valueEncoding: 'json' })
const db = memdb() || level('/tmp/db')
function validator (msg) {
if (typeof msg !== 'object') return null
if (typeof msg.value !== 'object') return null
if (typeof msg.value.timestamp !== 'number') return null
if (typeof msg.value.type !== 'string') return null
return msg
}
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, core, { indexes, validator }))
core.ready(() => {
core.writer('local', (err, feed) => {
const data = [{
type: 'chat/message',
timestamp: 1561996331739,
content: { body: 'First message' }
}, {
type: 'user/about',
timestamp: 1561996331740,
content: { name: 'Grace' }
}, {
}, {
type: 'chat/message',
timestamp: 1561996331742,
content: { body: 'Third message' }
}, {
type: 'chat/message',
timestamp: 1561996331743,
content: { channel: 'dogs', body: 'Lurchers rule' }
}, {
type: 'chat/message',
timestamp: 1561996331741,
content: { body: 'Second message' }
}, {
type: 'user/about',
timestamp: 1561996331754,
content: { name: 'Poison Ivy' }
}]
feed.append(data)
})
const query = [{ $filter: { value: { type: 'chat/message', content: { channel: 'dogs' } } } }]
pull(
core.api.query.read({ live: true, query }),
pull.drain(console.log)
)
})