Skip to content

Latest commit

 

History

History
82 lines (66 loc) · 1.22 KB

EXAMPLES.md

File metadata and controls

82 lines (66 loc) · 1.22 KB

Example

This page shows some examples of converting from ssb-db (and its various helper modules, ssb-backlinks, ssb-query, etc) to ssb-db2.

Live stream of vote messages

Includes also old messages.

Before

pull(
  sbot.backlinks.read({
    query: [{ $filter: { dest: msgId } }],
    index: 'DTA',
    live: true
  }),
  pull.drain(msg => {
    // ...
  })
)

After

const {and, votesFor, live, toPullStream} = require('ssb-db2/operators')

pull(
  sbot.db.query(
    and(votesFor(msgId)),
    live({ old: true }),
    toPullStream()
  ),
  pull.drain(msg => {
    // ...
  })
)

Get latest contact message from Alice about Bob

Before

pull(
  sbot.links({
    source: aliceId,
    dest: bobId,
    rel: 'contact',
    live: false,
    reverse: true
  }),
  pull.take(1),
  pull.drain(msg => {
    // ...
  })
)

After

const {and, author, contact, descending, paginate, toCallback} =
  require('ssb-db2/operators')

sbot.db.query(
  and(author(aliceId), contact(bobId)),
  descending(),
  paginate(1),
  toCallback((err, response) => {
    const msg = response.results[0]
    // ...
  })
)