Skip to content
WIP

This guide is a work in progress. Barry is also a work in progress.

@barry-rocks/sdk/stores

Per-bag SQLite stores: openBagStore opens (and migrates) a bag’s store, migrateStore and Migration run ordered migrations, and the registry types describe where each store lives. The machine or an identity owns each store, as bag.yaml declares.

import { … } from "@barry-rocks/sdk/stores";
  • Error

new AmbiguousSessionLocation(): AmbiguousSessionLocation

AmbiguousSessionLocation

Error.constructor


Providers are installed locally. Snapshot metadata never supplies executable code.

new StoreRegistry(nodeId, root?): StoreRegistry

Parameter Type
nodeId string
root string

StoreRegistry

Property Modifier Type
nodeId readonly string
root readonly string

declarationsFor(scope?): object[]

Parameter Type
scope? StoreScope

object[]

forRoot(root): StoreRegistry

Parameter Type
root string

StoreRegistry

hasRestoreAdapter(bag, name): boolean

Parameter Type
bag string
name string

boolean

hasSnapshotAdapter(bag, name): boolean

Parameter Type
bag string
name string

boolean

open(ref, readonly?): Promise<RegistryStoreHandle>

Parameter Type Default value
ref StoreRef undefined
readonly boolean false

Promise<RegistryStoreHandle>

prepareRestore(snapshot, destination): Promise<void>

Parameter Type
snapshot StoreSnapshot
destination string

Promise<void>

register(bag, name, declaration, provider): void

Parameter Type
bag string
name string
declaration StoreDeclaration
provider StoreProvider

void

registerRestoreAdapter(bag, name, adapter): void

Parameter Type
bag string
name string
adapter StoreRestoreAdapter

void

registerSnapshotAdapter(bag, name, adapter): void

Parameter Type
bag string
name string
adapter StoreSnapshotAdapter

void

snapshot(ref, directory): Promise<StoreSnapshot>

Parameter Type
ref StoreRef
directory string

Promise<StoreSnapshot>

validateDatabase(ref, path): Promise<void>

Parameter Type
ref StoreRef
path string

Promise<void>

Type Parameter
D extends StoreHandle

close(): void

Close every handle; the next use opens again.

void

db(): D

The handle for the current store context, opened on first use.

D

path(): string

The path the current store context’s handle opens.

string

replace(db): void

Use this handle instead (a test); it is migrated, and the previous one closed.

Parameter Type
db D

void


Type Parameter
D extends StoreHandle
Property Type Description
adopt? (db) => void Run on each newly opened handle before its migrations: adopting a store from before it had any.
connect (path) => D Open the file with the bag’s driver: (path) => new Database(path).
file string The store’s file name, as migrations and errors name it: events.db.
foreignKeys? boolean On by default.
identityScoped? boolean Keep a handle per identity when the store context names one.
migrations readonly Migration[] -
onOpen? (db, path) => void Run on each newly opened handle, after its migrations.
path () => string Where the store is for the current store context; asked on every open.
scratchGuard? string Refuse a path outside a scratch directory under test, naming this label.
synchronous? "FULL" | "NORMAL" -

The part of a SQLite handle this needs. better-sqlite3 and node:sqlite both fit.

exec(sql): unknown

Parameter Type
sql string

unknown

prepare(sql): object

Parameter Type
sql string

object

Name Type
all() (…params) => unknown
get() (…params) => unknown
run() (…params) => unknown

One forward schema change loaded from SQL or a generated migration catalog.

Property Type
name string
sql string

Property Type Description
from number The version the store was at when opened.
to number The version it is at now.

close(): void

void

revision(): string

string

schemaVersion(): number

number

snapshot(destination): Promise<void | object[]>

Parameter Type
destination string

Promise<void | object[]>


Property Type Inherited from
bag string StoreRef.bag
bytes number -
capturedAt string -
checksum string -
format 1 -
identity? string StoreRef.identity
node string StoreRef.node
resources? object[] -
revision string -
schemaVersion number -
store string StoreRef.store

Property Type
overrides? Readonly<Record<string, { app_url?: string; engine?: "sqlite"; filepath?: string; url?: string; }>>
remoteProtocols? readonly string[]
stores Readonly<Record<string, StoreDeclaration>>

Property Type
bags? Readonly<Record<string, StoreBagConfiguration>>
execution? ExecutionContext
identity? string
node? string
root? string
workflowActor? (path, body?) => string | Promise<string>

Property Type
engine "sqlite"
migrations string
owned_by StoreScope

The part of a SQLite handle a bag store needs. better-sqlite3 and node:sqlite both fit.

close(): unknown

unknown

exec(sql): unknown

Parameter Type
sql string

unknown

MigratableDatabase.exec

prepare(sql): object

Parameter Type
sql string

object

Name Type
all() (…params) => unknown
get() (…params) => unknown
run() (…params) => unknown

MigratableDatabase.prepare


Property Type
migrations readonly Migration[]

open(path, options): RegistryStoreHandle | Promise<RegistryStoreHandle>

Parameter Type
path string
options { readonly: boolean; }
options.readonly boolean

RegistryStoreHandle | Promise<RegistryStoreHandle>


Property Type
bag string
identity? string
node string
store string

Property Type
identity? string
name? string
node string
overrides? Readonly<Record<string, { app_url?: string; engine?: "sqlite"; filepath?: string; url?: string; }>>
remoteProtocols? readonly string[]
root? string

Property Type
manifest SnapshotManifest
path string

SnapshotResource = z.infer<typeof SnapshotResourceSchema>


StoreOptions = z.infer<typeof StoreOptionsSchema>


StoreRestoreAdapter = (snapshot, ref, destination) => Promise<void>

Parameter Type
snapshot StoreSnapshot
ref StoreRef
destination string

Promise<void>


StoreScope = "identity" | "node"


StoreSnapshotAdapter = (ref, source, destination) => Promise<SnapshotResource[]>

Parameter Type
ref StoreRef
source string
destination string

Promise<SnapshotResource[]>

const SnapshotManifestSchema: ZodObject<{ bag: ZodString; bytes: ZodNumber; capturedAt: ZodISODateTime; checksum: ZodString; format: ZodLiteral<1>; identity: ZodOptional<ZodString>; node: ZodString; resources: ZodOptional<ZodArray<ZodObject<{ files: ZodArray<ZodObject<{ bytes: ZodNumber; checksum: ZodString; path: ZodString; }, $strict>>; name: ZodString; }, $strict>>>; revision: ZodString; schemaVersion: ZodNumber; store: ZodString; }, $strict>


const SnapshotResourceSchema: ZodObject<{ files: ZodArray<ZodObject<{ bytes: ZodNumber; checksum: ZodString; path: ZodString; }, $strict>>; name: ZodString; }, $strict>


const StoreOptionsSchema: ZodObject<{ app_url: ZodOptional<ZodURL>; engine: ZodOptional<ZodLiteral<"sqlite">>; filepath: ZodOptional<ZodString>; url: ZodOptional<ZodURL>; }, $strict>

assertScratchStoreUnderTest(label, path): void

Enforce scratch paths at store opening under VITEST, unless the explicit BARRY_ALLOW_NON_TEST_STORE opt-out is set. Path-only lookups do not open stores. See docs/decisions/0038-test-isolation-and-command-reservations.md.

Parameter Type
label string
path string

void


copySnapshot(snapshot, destination): Promise<void>

Parameter Type
snapshot StoreSnapshot
destination string

Promise<void>


identityStorePaths(bag, store): object[]

Parameter Type
bag string
store string

object[]


installSnapshot(snapshot, directory): Promise<StoreSnapshot>

Install into a new generation; callers switch their index only after validation.

Parameter Type
snapshot StoreSnapshot
directory string

Promise<StoreSnapshot>


mergeStoreOptions(parent?, child?): object

Parameter Type
parent { app_url?: string; engine?: "sqlite"; filepath?: string; url?: string; }
parent.app_url? string
parent.engine? "sqlite"
parent.filepath? string
parent.url? string
child { app_url?: string; engine?: "sqlite"; filepath?: string; url?: string; }
child.app_url? string
child.engine? "sqlite"
child.filepath? string
child.url? string

object

Name Type
app_url? string
engine? "sqlite"
filepath? string
url? string

migratedStorePath(bag, store, legacy, nodeStore?, scope?): string

Parameter Type Default value
bag string undefined
store string undefined
legacy () => string undefined
nodeStore string store
scope "identity" | "node" "identity"

string


migrateStore(db, migrations, store): MigrationResult

Apply every migration the store has not seen, in order, all in one transaction with the version read.

Refuses a store stamped newer than migrations knows about: an older build would otherwise read the tables it recognises and silently ignore columns added since.

Parameter Type Description
db MigratableDatabase -
migrations readonly Migration[] -
store string A name for error messages, e.g. “sessions.db”.

MigrationResult


openBagStore<D>(options): BagStore<D>

Type Parameter
D extends StoreHandle
Parameter Type
options BagStoreOptions<D>

BagStore<D>


openStoreFile<D>(path, options): D

Open a SQLite file with the settings every bag store uses, before any migration.

Type Parameter
D extends StoreHandle
Parameter Type
path string
options Pick<BagStoreOptions<D>, "connect" | "foreignKeys" | "synchronous">

D


resolveStoreBinding(bag, declarations, resolution): StoreBinding

Parameter Type
bag string
declarations Readonly<Record<string, StoreDeclaration>>
resolution StoreResolution

StoreBinding


sessionIdentity(sessionId): string | undefined

Parameter Type
sessionId string

string | undefined


storeKey(ref): string

Parameter Type
ref StoreRef

string


storePath(ref, scope, root?): string

Parameter Type
ref StoreRef
scope StoreScope
root string

string


validateSnapshot(snapshot): Promise<void>

Parameter Type
snapshot StoreSnapshot

Promise<void>

Re-exports StoreBinding