Skip to content

create_corpus

function create_corpus(): CorpusBuilder<{}>

Creates a new Corpus instance using the builder pattern. A Corpus is a collection of typed stores backed by a storage backend.

Usage

Chain the builder methods to configure your corpus:

1
with_backend(backend) — Set the storage backend
2
with_store(definition) — Add a typed store (can be called multiple times)
3
build() — Create the final Corpus instance
import { create_corpus, create_memory_backend, define_store, json_codec } from '@f0rbit/corpus'
import { z } from 'zod'
const UserSchema = z.object({
name: z.string(),
email: z.string()
})
const corpus = create_corpus()
.with_backend(create_memory_backend())
.with_store(define_store('users', json_codec(UserSchema)))
.with_store(define_store('notes', text_codec()))
.build()

Accessing Stores

After building, access your stores through the stores property with full type safety:

// Type-safe store access
await corpus.stores.users.put({ name: 'Alice', email: 'alice@example.com' })
await corpus.stores.notes.put('Hello, world!')
// Get latest snapshot
const latest = await corpus.stores.users.get_latest()
if (latest.ok) {
console.log(latest.value.data.name) // "Alice"
}

Returns

Returns a CorpusBuilder that provides:

MethodDescription
with_backend(backend)Sets the storage backend (required); returns a CorpusBuilderWithBackend
with_store(definition)Adds a store definition
with_observations(types)Registers observation types

build() is only available on the CorpusBuilderWithBackend returned by with_backend(), so building without a backend is a compile error. Steps may be called in any order before build():

const base = create_corpus().with_store(define_store('users', json_codec(UserSchema)))
const corpus = base.with_backend(create_memory_backend()).build() // corpus.stores.users is typed

Immutability

Every builder step returns a new builder; the receiver is never modified. Branching from one base is safe:

const base = create_corpus().with_backend(create_memory_backend())
const a = base.with_store(define_store('a', text_codec())).build()
const b = base.with_store(define_store('b', text_codec())).build()
// a has only store "a"; b has only store "b"

Untyped (plain JS) callers who invoke build() without a backend still get a runtime Error.

See Also