Skip to content

create_store

function create_store<T>(
backend: Backend,
definition: StoreDefinition<string, T>
): Store<T>

Creates a typed Store instance bound to a specific backend. This is primarily used internally by create_corpus().build(), but can be used directly for advanced use cases.

Usage

import { create_store, create_memory_backend, define_store, json_codec } from '@f0rbit/corpus'
import { z } from 'zod'
const backend = create_memory_backend()
const definition = define_store('items', json_codec(z.object({ name: z.string() })))
const store = create_store(backend, definition)
await store.put({ name: 'Example' })

Parameters

ParameterTypeDescription
backendBackendThe storage backend to use
definitionStoreDefinition<Id, T>Store definition from define_store()

Returns

Returns a Store<T> with the following methods:

MethodDescription
put(data, opts?)Store a new snapshot
get(version)Get a specific version
get_latest()Get the most recent snapshot
get_meta(version)Get metadata only (no data)
list(opts?)List all snapshots
delete(version)Delete a specific version

Content Deduplication

Stores automatically deduplicate content. If you store the same data twice, it creates two versions pointing to the same underlying data:

const v1 = await store.put({ name: 'Same' })
const v2 = await store.put({ name: 'Same' })
// Same content hash, same data_key
v1.value.content_hash === v2.value.content_hash // true
v1.value.data_key === v2.value.data_key // true
// But different versions
v1.value.version !== v2.value.version // true

See Also