Appearance
DocumentStore
The main class. Handles document creation, editing, versioning, and sync.
Constructor
typescript
import { createStore } from 'document-store';
const store = await createStore({
// Canonical log (the store's own SQLite file)
storage: './data/ds.sqlite',
// Rendering into YOUR database — see the Rendering chapter
renderHandler: async (ops) => { ... },
// Optional: cryptographic signatures
sign: async (uid, hash, claim) => { ... },
verify: async (uid, hash, signature, claim) => { ... },
// Optional: require signatures (default: true when sign/verify provided)
requireSignatures: true,
// Optional: custom permission resolver
permissionResolver: async (permission, context) => { ... },
});Options
| Option | Type | Description |
|---|---|---|
storage | string | Path for the store's canonical log (':memory:' for tests) — required |
renderHandler | (ops: RenderOp[]) => Promise<void> | Renders accepted writes into your database — see Rendering |
sign | (uid, hash, claim) => SignResult | Sign documents on create/edit |
verify | (uid, hash, signature, claim) => boolean | Verify signatures on inject |
requireSignatures | boolean | Require valid signatures (default: true when callbacks set) |
permissionResolver | function | Custom permission resolver for write rules — see Write Rules |
shareResolver | (share, accessorUid, doc) => boolean | Custom share logic fallback — see Share Policies |
registerTypeSchema(schema)
Register a full document-type schema — fields, membership, temporal config — with content validation enforced in the engine on every add()/edit() (see Schemas guide).
typescript
await store.registerTypeSchema(discussionSchema);registerType(schema)
Register a document type. Must be called before adding documents of that type. Accepts a full schema or a minimal type name with options.
Full schema
typescript
store.registerType({
type: 'discussion',
meta: { label: { en: 'Discussion' } },
fields: {
name: { type: 'string', required: true },
members: {
type: 'array',
membership: { userField: 'userId', roleField: 'role', roleHierarchy: ['admin', 'member'] },
temporal: { table: 'discussion_members', key: 'userId' },
items: {
userId: { type: 'uid', required: true },
role: { type: 'enum', values: ['admin', 'member'] },
},
},
},
write: { '*': { allow: 'uid' } },
});Creates the rendered table, extracts membership config for token computation, and creates temporal tables. See Schemas guide for full schema reference.
Minimal registration
typescript
store.registerType('bookmark');add(type, doc) → EditResult
Create a new document.
typescript
const [errors, hash] = await store.add('bookmark', {
uid,
url: 'https://example.com',
title: 'Example',
share: { public: { license: 'SRL' } },
write: { '*': 'uid' },
});Returns [errors, null] on failure or [null, hash] on success.
addMany(type, docs) → [errors, hashes]
Create multiple documents atomically.
typescript
const [errors, hashes] = await store.addMany('bookmark', [
{ uid, url: 'https://a.com', title: 'A' },
{ uid, url: 'https://b.com', title: 'B' },
]);edit(uid, filter, update) → EditResult
Apply an update to a document.
typescript
const [errors, newHash] = await store.edit(uid, { hash }, {
$set: { title: 'Updated' }
});See Editing guide for all operators.
save(uid, hash, content) → EditResult
Replace document content. The store computes the minimal diff.
typescript
const [errors, newHash] = await store.save(uid, hash, {
title: 'New Title',
url: 'https://example.com',
});delete(uid, hash)
Soft-delete a document and its children.
typescript
await store.delete(uid, hash);restore(uid, hash)
Restore a soft-deleted document and its children.
typescript
await store.restore(uid, hash);Reads — query your database directly
Document-store renders documents into your database. For reads, query the database directly using its native tools. See Querying guide.
type(type) → DSCollection
Get a type-bound collection for writes.
typescript
const bookmarks = store.type<Bookmark>('bookmark');
await bookmarks.add({ uid, url, title });versions(hash)
Get the full version history of a document.
typescript
const { original, versions, edits } = await store.versions(hash);get(hash) → SyncItem
Get a document for P2P transmission (CBOR-encoded).
typescript
const syncItem = await store.get(hash);
// { hash, data (Buffer), time, signed }inject(syncItem)
Receive and apply a document from a peer.
typescript
await store.inject(syncItem);getShared(accessorUid, timestamp) → SyncItem[]
Get documents the accessor has access to, newer than timestamp.
typescript
const items = await store.getShared(peerUidHex, lastSyncTime);find(query, section?) → Buffer[]
Full-text search across documents with registered search functions. Returns matching document hashes.
typescript
// Search all sections
const hashes = await store.find('search term');
// Search a specific section
const hashes = await store.find('javascript', 'tags');See Search guide for details on registering search functions and keyword extraction.