anchordb-sync-server
v1.3.2
Published
Framework-agnostic server implementation of the Anchor sync protocol, with Express, NestJS and Next.js adapters.
Maintainers
Readme
anchordb-sync-server
The server half of the AnchorDB sync protocol, with Express, NestJS and Next.js adapters.
Overview · Report a bug or get support
npm install anchordb-sync-serverExpress
import express from "express";
import { createAnchorRouter, MemoryStore } from "anchordb-sync-server";
const app = express();
app.use(express.json());
app.use("/anchor/v1", createAnchorRouter(express.Router, { store: new MemoryStore() }));Next.js (App Router)
// app/anchor/v1/[...anchor]/route.ts
import { createAnchorRouteHandlers } from "anchordb-sync-server/nextjs";
import { MemoryStore } from "anchordb-sync-server";
export const { POST, GET } = createAnchorRouteHandlers({ store: new MemoryStore() });NestJS
import { AnchorSyncService, createAnchorSyncService } from "anchordb-sync-server/nestjs";NESTJS_CONTROLLER_EXAMPLE exports a ready-to-paste controller and module. NestJS itself is not a
dependency of this package — the controller lives in your app, so it keeps your routing and your
own Nest version.
Stores
| Store | Use |
| --- | --- |
| MemoryStore | default; development, tests, demos. Runs with no database |
| MongooseStore | MongoDB, from anchordb-sync-server/mongoose |
import mongoose from "mongoose";
import { MongooseStore } from "anchordb-sync-server/mongoose";
const store = new MongooseStore({ mongoose });Mongoose is a peer dependency and is passed in rather than imported, so the package installs without it and your app keeps a single mongoose instance.
What the server is responsible for
- Version counters — per-document integers; a stale
baseVersionis a conflict. - Authoritative timestamps — every response carries
serverTime, which clients use to correct their own clock skew. - A global change sequence — pull cursors are positions in it, so a partial sync resumes.
- Idempotency —
(clientId, mutationId)verdicts are memoised, so a retry after a timeout replays the original result instead of applying the write twice.
The server never picks a conflict winner. It returns its document and lets the client's configured strategy decide, because only the client knows which strategy the developer chose.
Status
1.0.0. All three adapters share one protocol implementation and are held to the same 49-test
conformance suite. MongooseStore is typechecked and written against the documented Mongoose API
but is not exercised against a live MongoDB in this package's tests — set MONGODB_URI and run
your own integration check before relying on it.
The AnchorDB family
Six packages. Only anchordb is required — the rest exist so that an offline-only app never
has to download Express, and an Express server never has to download React.
| Package | What it is | Runs where |
| --- | --- | --- |
| anchordb | The database — schema, models, queries, aggregation, optional sync | phone · browser · Node |
| anchordb-react | React and React Native hooks | the device |
| anchordb-angular | Angular / Ionic module, DI and RxJS observables | the device |
| anchordb-sync-server (this package) | Server half of sync — Express, NestJS, Next.js | your backend |
| anchordb-relay | Dev relay for the Anchor Lens inspector | your laptop |
| anchordb-lens-link | Open a QA build's database in Anchor Lens on the same phone, from a file | the device, in QA builds |
Each one needs a different third-party framework as a peer dependency, and npm resolves those per package rather than per import — which is why they are not one package. Full reasoning and API reference: github.com/knnadeera/anchordb.
MIT
Bugs, support and feedback
Report a bug, ask for help or suggest a feature on the AnchorDB project page —
choose anchordb-sync-server as the package, and the reply comes by email.
Include the version (npm ls anchordb), where it runs, the smallest snippet that reproduces it, and
the full error.
Sync problems: say which conflict strategy you use, and whether the mutation shows as parked in Anchor Lens — a push rejected by a unique index is parked rather than retried forever.
