OverviewFilesCommitsIssuesDocsReleasesPeers

pear-git-v3 / lib / services / migrate / read-old-orgs.js

33 lines · javascript ref:
/**
 * Read the legacy single-`orgs` autobase (orgs v1): yield each org record
 * from the linearized view, which is a Hyperbee keyed `org:{name}` — the
 * shape the old single-autobase HyperOrgs produced.
 *
 * Generic over anything exposing a Hyperbee `.view` (a hyper-rooms Room,
 * an old HyperOrgs, or a raw Autobase). Read-only — it updates the view
 * to the latest linearized state but never appends. This is the migration
 * source AND the org-scoped slice of the autobase-reader capability.
 *
 * Pairs with migrateOrgs (which takes an async-iterable of old records).
 *
 * @param {object} base - has `.view` (Hyperbee) and optionally `.update()`
 * @yields old org records { name, displayName, members, resources, ... }
 */
import { linearize } from './linearize.js'

export async function * readOldOrgs (base) {
  if (!base) throw new Error('readOldOrgs requires an opened autobase/room')
  const view = base.view
  if (!view || typeof view.createReadStream !== 'function') {
    throw new Error('readOldOrgs: base.view must be a Hyperbee')
  }
  // Linearize to the latest state before reading (no-op on a single writer
  // with nothing pending; pulls in replicated ops on a follower). Handles a
  // reopened Room (.base.update) as well as a raw Autobase (.update).
  await linearize(base)

  for await (const entry of view.createReadStream({ gt: 'org:', lt: 'org:\xff' })) {
    if (entry && entry.value) yield entry.value
  }
}