define_store
function define_store<Id extends string, T>( id: Id, codec: Codec<T>, description?: string): StoreDefinition<Id, T>Creates a type-safe store definition that can be passed to create_corpus().with_store(). The store ID becomes the key used to access the store, and the codec determines how data is serialized.
Usage
import { define_store, json_codec, text_codec, binary_codec } from '@f0rbit/corpus'import { z } from 'zod'
// JSON store with Zod validationconst UserSchema = z.object({ name: z.string(), email: z.string().email(),})const users = define_store('users', json_codec(UserSchema))
// Plain text storeconst logs = define_store('logs', text_codec())
// Binary store for filesconst images = define_store('images', binary_codec())Parameters
| Parameter | Type | Description |
|---|---|---|
id | string | Unique identifier for the store |
codec | Codec<T> | Serialization codec for the data |
description | string? | Optional description for documentation |
With create_corpus
Store definitions are passed to the corpus builder:
const corpus = create_corpus() .with_backend(create_memory_backend()) .with_store(users) // Access via corpus.stores.users .with_store(logs) // Access via corpus.stores.logs .with_store(images) // Access via corpus.stores.images .build()
// Type-safe accessawait corpus.stores.users.put({ name: 'Alice', email: 'alice@example.com' })await corpus.stores.logs.put('User Alice created')See Also
- create_corpus - Use store definitions
- Codecs - Available codecs
- Core Types -
StoreDefinition,Codec