Commit 3b4dcd1fd6d4f5e8c7a7dc56ba8dbcd131583dfa
v02 - todays post!
mixmix committed on 10/17/2018, 10:21:40 AMParent: 732c846359bc5138a27a082a1fa03a3d8ba0bf85
Files changed
README.md | changed |
v02.js | added |
README.md | ||
---|---|---|
@@ -45,4 +45,49 @@ | ||
45 | 45 … | The sink is a `pull.collect`, which waits until the stream is finished (here when we've pulled 100 messages), collecting all the results then passing them as an Array to the callback `onDone`. |
46 | 46 … | |
47 | 47 … | |
48 | 48 … | NOTE - you need to be using a server with the ssb-query plugin installed for this to work (most have this!) |
49 … | + | |
50 … | + | |
51 … | +## `v02` - todays post | |
52 … | + | |
53 … | +Instead of just getting the last 100 messages then filtering them down to the `post` messages, we can get the server to do a much tighter query and to send just those results over: | |
54 … | + | |
55 … | +```js | |
56 … | +const opts = { | |
57 … | + reverse: true, | |
58 … | + query: [{ | |
59 … | + $filter: { | |
60 … | + value: { | |
61 … | + content: { type: 'post' }, | |
62 … | + timestamp: { | |
63 … | + $gte: 1539687600000, | |
64 … | + $lt: 1539774000000 | |
65 … | + } | |
66 … | + } | |
67 … | + } | |
68 … | + }] | |
69 … | +} | |
70 … | +``` | |
71 … | + | |
72 … | +`reverse: true` means we still get these results in an order that's from the most recent to the oldest. | |
73 … | + | |
74 … | +In ssb-query, the 'special' query-based properties are prefixed with a `$`, so `$filter` means everything inside here use that as a filter. You might notice the _shape_ of the object inside there mostly matches the shape of messages in the databse. i.e.: | |
75 … | + | |
76 … | +``` | |
77 … | +{ | |
78 … | + key: ...., | |
79 … | + value: { | |
80 … | + author: .... | |
81 … | + content: { | |
82 … | + type: 'post' | |
83 … | + .... | |
84 … | + }, | |
85 … | + timestamp: 1539687602391 // when it was created by the author | |
86 … | + } | |
87 … | +} | |
88 … | +``` | |
89 … | +There are some other fields but we're not querying on them so I've left them off. | |
90 … | +Notice in the `timestamp` field, that I've not given a time I've given a _time range_ : `$gte: 1539687600000` means _greater than or equal to the start of 2018-10-17 in Melbourne Australia_ (relevant because that's the start of the day _subjectivtly_ for me where I am as I write this). `$lt: 1539774000000` means _less than the start of the day following that_ | |
91 … | + | |
92 … | +The `query` property is an Array because with more advanced queries we can also get the server to map and reduce the data we've got thereby further reducing the amount of data sent over muxrpc to us. I've put a commented out example in the code you can play with. | |
93 … | + |
v02.js | |||
---|---|---|---|
@@ -1,0 +1,73 @@ | |||
1 … | +const Connection = require('ssb-client') | ||
2 … | +const pull = require('pull-stream') | ||
3 … | + | ||
4 … | +console.log('Connecting') | ||
5 … | + | ||
6 … | +Connection((err, server) => { | ||
7 … | + if (err) throw err | ||
8 … | + console.log('Connection established') | ||
9 … | + | ||
10 … | + const rightNow = new Date() | ||
11 … | + const opts = { | ||
12 … | + reverse: true, | ||
13 … | + query: [ | ||
14 … | + { | ||
15 … | + $filter: { | ||
16 … | + value: { | ||
17 … | + content: { type: 'post' }, | ||
18 … | + timestamp: { | ||
19 … | + $gte: Number(startOfDay(rightNow)), | ||
20 … | + $lt: Number(startOfDay(rightNow, +1)) | ||
21 … | + } | ||
22 … | + } | ||
23 … | + } | ||
24 … | + }, | ||
25 … | + // { | ||
26 … | + // $map: { | ||
27 … | + // author: ['value', 'author'], | ||
28 … | + // timestamp: ['value', 'timestamp'], | ||
29 … | + // text: ['value', 'content', 'text'], | ||
30 … | + // root: ['value', 'content', 'root'] // the root messages of a thread, this is present if this post is a reply to another message | ||
31 … | + // } | ||
32 … | + // } | ||
33 … | + ] | ||
34 … | + } | ||
35 … | + | ||
36 … | + pull( | ||
37 … | + server.query.read(opts), | ||
38 … | + pull.collect(onDone) | ||
39 … | + ) | ||
40 … | + | ||
41 … | + function onDone (err, msgs) { | ||
42 … | + if (err) { | ||
43 … | + console.error('oh noes', err) | ||
44 … | + server.close() | ||
45 … | + return | ||
46 … | + } | ||
47 … | + | ||
48 … | + msgs.forEach(msg => { | ||
49 … | + prettyPrint(msg) | ||
50 … | + console.log('------') | ||
51 … | + }) | ||
52 … | + | ||
53 … | + console.log(`${msgs.length} messages`) | ||
54 … | + server.close() | ||
55 … | + } | ||
56 … | +}) | ||
57 … | + | ||
58 … | +// helpers | ||
59 … | + | ||
60 … | +function startOfDay (time = new Date(), dayOffset = 0) { | ||
61 … | + // dayOffset = 0 means if this argument is not supplied to set it to default to 0 | ||
62 … | + | ||
63 … | + const year = time.getFullYear() | ||
64 … | + const month = time.getMonth() | ||
65 … | + const date = time.getDate() + dayOffset | ||
66 … | + return new Date(year, month, date, 0, 0, 0) // 0 hours, 0 minutes, 0 secords | ||
67 … | +} | ||
68 … | + | ||
69 … | +function prettyPrint (obj) { | ||
70 … | + console.log(JSON.stringify(obj, null, 2)) | ||
71 … | + // this just print the full object out as a string that's been nicely indented | ||
72 … | + // with each level of nesting | ||
73 … | +} |
Built with git-ssb-web