We're going to set up a kappa architecture and a view for sorting messages by timestamp.
There are some pre-made general purpose views built on kappa-core, like a key-value store (kappa-view-kv) and a sorted list (kappa-view-list).
Here is an example of a sorted list view that sorts a message's timestamp
field alphabetically. kappa-view-list
sorts lexigraphically by default.
// kappa-chat.js
const kappa = require('kappa-core')
const memdb = require('memdb')
const list = require('kappa-view-list')
const timestampView = list(memdb(), function (msg, next) {
if (msg.value.timestamp && typeof msg.value.timestamp === 'string') {
// sort on the 'timestamp' field
next(null, [msg.value.timestamp])
} else {
next()
}
})
const core = kappa('./multichat', {valueEncoding: 'json'})
core.use('chats', timestampView)
Running core.use(title, view)
installs the list view API under core.api.chats
.
Look at the API for kappa-view-list
and write code that will print out every name in the view, in alphabetical order.
core.writer('local', function (err, feed) {
feed.append({type: 'chat-message', timestamp: new Date().toISOString() })
})
Once you solve this exercise continue to exercise 11