Skip to content

SharedStateHandle

Handle for a shared state stream, returned by ctx.mkdb() and await ctx.observe(db(...)). Provides typed collection proxies keyed by the collection names declared in the schema map.

Source: @durable-streams/darix-runtime

ts
type SharedStateHandle<
  TSchema extends SharedStateSchemaMap = SharedStateSchemaMap,
> = {
  id: string
} & { [K in keyof TSchema]: StateCollectionProxy }

Properties

PropertyTypeDescription
idstringThe shared state stream identifier.
[collectionName]StateCollectionProxyOne property per collection defined in the schema. Provides insert/update/delete/get/toArray.

SharedStateSchemaMap

Defines the collections in a shared state stream.

ts
type SharedStateSchemaMap = Record<string, SharedStateCollectionSchema>

SharedStateCollectionSchema

ts
interface SharedStateCollectionSchema {
  schema: unknown
  type: string
  primaryKey: string
}
FieldTypeDescription
schemaunknownZod or Standard Schema validator for the row type.
typestringEvent type string used in the durable stream (e.g. "finding").
primaryKeystringPrimary key field name on the row (must be a string field).

Example

ts
import { db } from "@durable-streams/darix-runtime"

const schema = {
  findings: {
    schema: z.object({
      key: z.string(),
      domain: z.string(),
      finding: z.string(),
    }),
    type: "finding",
    primaryKey: "key",
  },
}

ctx.mkdb("research-findings", schema)
const shared = await ctx.observe(db("research-findings", schema))
shared.findings.insert({ key: "f1", domain: "security", finding: "..." })