Skip to content

Instantly share code, notes, and snippets.

@hackergrrl
Created October 30, 2018 20:14
Show Gist options
  • Save hackergrrl/9db1de68eb3366196c60c9434dccbd38 to your computer and use it in GitHub Desktop.
Save hackergrrl/9db1de68eb3366196c60c9434dccbd38 to your computer and use it in GitHub Desktop.
// ------------------ KV ------------------
// API:
// - put
// - get
// EVENTS:
// - new value for key listened to
// id view
// requires 'msg.links' being populated to point to old version
// TODO: how is 'version' encapulated/known-about here?
// A: it's just an opaque identifier (umkv already works this way)
db.view.kv(function (msg, next) {
var ops = []
if (!msg.id) return next()
ops.push({
key: msg.id,
links: msg.links
})
else next(null, ops)
})
// content hash view
var sha = require('sha.js')
db.view.kv(function (msg, next) {
var ops = []
var hash = sha('sha256').update(JSON.stringify(msg)).digest('hex')
ops.push({ key: hash, links: [] })
next(null, ops)
})
// ------------------ REFS ------------------
// API:
// - read(refId) -> ReadableStream
// EVENTS:
// - new item added to ref id listened to
// changeset backrefs
db.view.refs(function (msg, next) {
var ops = []
if (msg.changeset) ops.push({ src: msg.changeset, dst: msg.id })
next(null, ops)
})
// ways backrefs
db.view.refs(function (msg, next) {
var ops = []
if (msg.type === 'way') {
msg.refs.forEach(function (ref) {
ops.push({
src: ref,
dst: msg.id
})
})
}
next(null, ops)
})
// ------------------ LIST ------------------
// API:
// - read(lt, gt) -> ReadableStream
// - (maybe some kind of pagination)
// EVENTS:
// - new item added to range listened to
// by type
db.view.ascendingList(function (msg, next) {
var ops = []
ops.push(msg.type)
next(null, ops)
})
// ------------------ SPATIAL ------------------
// API:
// - query(bbox) -> ReadableStream
// EVENTS:
// - new item added to bbox listened to
// lat/lon spatial
db.view.spatial(function (msg, next) {
if (msg.lat && msg.lon) next(null, { lat: msg.lat, lon: msg.lon })
else next(null, [])
})
//----------------------------
// cabal: messages by channel
db.view.ascendingList(function (msg, next) {
if (msg.type !== 'chat/text') return next()
var ops = []
ops.push(msg.channel + '!' + msg.timestamp)
next(null, ops)
})
// cabal: latest nick
// not shown: 'links' field needs to be written @ publish time, so it can be
// used by the view here
db.view.kv(function (msg, next) {
if (msg.type !== 'chat/about') return next()
ops.push({
key: msg.id,
links: msg.links
})
next(null, ops)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment