Getting Started
Get up and running with Corpus in minutes.
-
Install the package
Terminal window bun add @f0rbit/corpus zodTerminal window npm install @f0rbit/corpus zodTerminal window pnpm add @f0rbit/corpus zod -
Define a schema
Corpus uses Zod for type-safe serialization. Define a schema for your data:
import { z } from 'zod'const PostSchema = z.object({title: z.string(),body: z.string(),published: z.boolean(),tags: z.array(z.string()).optional(),})type Post = z.infer<typeof PostSchema> -
Create a corpus
Use the builder pattern to configure your corpus with a backend and stores:
import {create_corpus,create_memory_backend,define_store,json_codec} from '@f0rbit/corpus'const corpus = create_corpus().with_backend(create_memory_backend()).with_store(define_store('posts', json_codec(PostSchema))).build() -
Store and retrieve data
Use the typed store to save and load snapshots:
const post: Post = {title: 'Hello World',body: 'My first post using Corpus.',published: true,}const result = await corpus.stores.posts.put(post)if (result.ok) {const { version, content_hash } = result.valueconsole.log(`Saved version ${version} with hash ${content_hash}`)} -
Handle errors with pattern matching
Corpus uses
Result<T, E>types instead of throwing exceptions:const snapshot = await corpus.stores.posts.get('nonexistent-version')if (!snapshot.ok) {switch (snapshot.error.kind) {case 'not_found':console.log(`Version ${snapshot.error.version} does not exist`)breakcase 'decode_error':console.log('Data corruption detected:', snapshot.error.cause)breakcase 'storage_error':console.log('Backend failed:', snapshot.error.operation)break}return}console.log('Post title:', snapshot.value.data.title)
Next Steps
- Storage Backends - Learn about memory, file, and Cloudflare backends
- Cloudflare Deployment - Deploy to Cloudflare Workers with D1 and R2
- Observations - Extract structured facts from documents with provenance tracking
- Testing Patterns - Write tests using the memory backend
- API Reference - Full API documentation