@groundstate/crdt
v1.0.0-beta
Published
Schema-defined CRDTs with TypeScript inference
Downloads
38
Maintainers
Readme
@groundstate/crdt
Schema-defined CRDTs with full TypeScript inference for building local-first applications.
Installation
# npm
npm install @groundstate/crdt
# yarn
yarn add @groundstate/crdt
# pnpm
pnpm add @groundstate/crdtQuick Start
import { Doc, Field, Counter, List, HLC } from '@groundstate/crdt';
// Define a schema-driven CRDT document
const todo = Doc({
title: Field(''),
done: Field(false),
priority: Counter(0),
tags: List<string>(),
});
// Mutate with automatic conflict-free merging
todo.title = 'Buy groceries';
todo.priority.increment(1);
todo.tags.push('shopping');
// Merge changes from another peer
const remoteTodo = Doc({ title: Field(''), done: Field(false), priority: Counter(0), tags: List<string>() });
remoteTodo.title = 'Buy groceries and snacks';
todo.merge(remoteTodo);API Highlights
Clock
HLC-- Hybrid Logical Clock for causally ordered timestampsHLCTimestamp-- Timestamp type used across all CRDT operations
CRDT Types
LWWRegister-- Last-writer-wins register for single valuesPNCounter-- Positive-negative counter supporting increment/decrementRGAList-- Replicated growable array for ordered collectionsLWWMap-- Last-writer-wins map for key-value dataPeriText-- Rich-text CRDT with inline formatting marks
Schema
Doc/Document-- Create typed CRDT documents from a schema definitionDocFactory-- Factory for producing documents with a shared schemaField/Counter/List-- Schema field builders
Encoding
BinaryEncoder/BinaryDecoder-- Compact binary serializationencodeDocument/decodeDocument-- Full document serializationLazyDocument-- Decode documents on demand for large datasets
History & Diffing
OpLog-- Append-only operation logUndoManager-- Undo/redo support for local changesdiff/applyPatches-- Compute and apply structural diffs
Garbage Collection
GarbageCollector-- Reclaim memory from tombstoned operationscollectableTombstones/gcList/gcMap-- Fine-grained GC helpers
Relations
RefManager-- Manage document-to-document references with cascade/nullify delete semantics
Documentation
See the full documentation for complete API reference.
License
MIT
