SDK
SDK overview
@mdbase-dev/sdk is the TypeScript client for mdbase collections: reads from a local view, optimistic writes and live queries.Web apps, Node scripts and editor plugins use it to read, query, watch and change records and files in one collection.
Where to start
@mdbase-dev/sdk is not on npm yet. Until it is, the install
commands in these guides fail.
How the SDK works
Your app talks to a replica of the collection: a copy that applies changes in the order the collection’s log confirms them. The SDK gives you that replica’s view, plus your own writes that are still waiting.
-
Reads are local.
get,queryandliveanswer from the replica’s view. They include your pending writes. -
Writes are intents. Each write returns at once with a
receipt in the
pendingstate. It becomesconfirmedwhen the log accepts it, orrejectedwith a reason. - Nothing is lost silently. Concurrent edits to the same field are merged, and the losing value is kept as a conflict you can review. A file-backed replica can also put a local edit on hold instead of overwriting it.
Pending writes and sync status covers receipts, holds and conflicts.
Getting a client
Every client comes from connect(), given a
connector that says how to reach a replica:
| You are building | How you reach the collection |
|---|---|
| A web or mobile app |
createMdbase() signs the user in and hands you a
connection. Its client is an ordinary MdbaseClient.
See Connect a browser app.
|
| A script or plugin on a computer running the mdbase desktop app |
Connectors from @mdbase-dev/sdk/node reach the desktop
app’s replica on the same machine. See
How apps reach a collection.
|
| Tests and demo modes |
MemoryReplica from @mdbase-dev/sdk/testing.
See Testing.
|
Application shape
Keep domain code on MdbaseClient. It then runs unchanged
against any connector.
import type { MdbaseClient } from "@mdbase-dev/sdk";
// Accept the client, not a connector, so the same code runs against
// a real collection and against MemoryReplica in tests.
export async function listOpenTasks(db: MdbaseClient) {
const page = await db.query({
types: ["task"],
where: 'status == "open"',
order_by: [{ field: "due", direction: "asc" }],
limit: 50,
});
return page.records;
}
Package map
| Import | What it gives you |
|---|---|
@mdbase-dev/sdk |
connect, MdbaseClient,
createMdbase, errors, status helpers and connectors
|
@mdbase-dev/sdk/node |
Connectors to the desktop app over local IPC or localhost |
@mdbase-dev/sdk/testing |
MemoryReplica and test fixtures |
@mdbase-dev/sdk/browser |
The same surface as a browser module. The package also ships a classic-script bundle; see Browser bundle. |
@mdbase-dev/sdk/account |
Encryption password and recovery key set-up for Private collections |
@mdbase-dev/sdk/app-host, @mdbase-dev/sdk/app-storage |
The parts that let an app run its own replica in the browser. These are for mdbase’s own apps at launch. |
The package needs Node 20 or later outside the browser. It is what apps
use instead of the @mdbase-dev/connect beta.