SSB-DB2

8.1.0 · active · verified Wed Apr 22

SSB-DB2 is a new database for secure-scuttlebutt, designed as a replacement for the older `ssb-db`. Currently at version 8.1.0, it offers significant architectural changes aimed at improving performance and flexibility. Key differentiators include using `bipf` for data storage, replacing `flume` with `jitdb` for specialized indexes, supporting browser environments via `ssb-browser-core`, and enabling efficient partial replication. It has expanded capabilities beyond `ssb-db`, featuring deletion and compaction, support for customizable feed and encryption formats (defaulting to `ssb-classic`), and a powerful query language based on composable JavaScript functions. SSB-DB2 operates as a `secret-stack` plugin, registering within the `db` namespace, and is optimized for modern Node.js environments (>=16).

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up an SSB peer with ssb-db2, publish a message, and then query for 'post' messages using the new query operators.

const SecretStack = require('secret-stack')
const caps = require('ssb-caps')
const fs = require('fs')
const path = require('path')

const testPath = './temp-ssb-db2-quickstart'

// Ensure a clean state for the quickstart
if (fs.existsSync(testPath)) {
  fs.rmSync(testPath, { recursive: true, force: true });
}
fs.mkdirSync(testPath, { recursive: true });

const sbot = SecretStack({ caps })
  .use(require('ssb-db2'))
  .call(null, { path: testPath })

sbot.db.create({ content: { type: 'post', text: 'hello from ssb-db2!' } }, (err, msg) => {
  if (err) {
    console.error('Error publishing message:', err);
    sbot.close()
    return;
  }
  console.log('Published message:', msg.value.content);

  const { where, type, toCallback } = require('ssb-db2/operators');
  sbot.db.query(
    where(type('post')),
    toCallback((err, msgs) => {
      if (err) {
        console.error('Error querying messages:', err);
      } else {
        console.log('Found ' + msgs.length + ' post messages:');
        msgs.forEach(m => console.log('- ' + m.value.content.text));
      }
      sbot.close();
      fs.rmSync(testPath, { recursive: true, force: true }); // Clean up
    })
  );
});

view raw JSON →