SQLiteSyncStorage

See source code
Table of contents

SQLite-based implementation of TLSyncStorage. Stores documents, tombstones, metadata, and clock values in SQLite tables.

This storage backend provides persistent synchronization state that survives process restarts, unlike InMemorySyncStorage which loses data when the process ends.

class SQLiteSyncStorage<R extends UnknownRecord> implements TLSyncStorage<R> {}

Example

// With Cloudflare Durable Objects
import {
  SQLiteSyncStorage,
  DurableObjectSqliteSyncWrapper,
} from "@tldraw/sync-core";

const sql = new DurableObjectSqliteSyncWrapper(this.ctx.storage);
const storage = new SQLiteSyncStorage({ sql });
// With Node.js sqlite (Node 22.5+)
import { DatabaseSync } from "node:sqlite";
import { SQLiteSyncStorage, NodeSqliteWrapper } from "@tldraw/sync-core";

const db = new DatabaseSync("sync-state.db");
const sql = new NodeSqliteWrapper(db);
const storage = new SQLiteSyncStorage({ sql });
// Initialize with an existing snapshot
const storage = new SQLiteSyncStorage({ sql, snapshot: existingSnapshot });

Constructor

Constructs a new instance of the SQLiteSyncStorage class

Parameters

NameDescription

{ sql, snapshot, onChange }

{
  onChange?(
    arg: TLSyncStorageOnChangeCallbackProps,
  ): unknown;
  snapshot?: RoomSnapshot | StoreSnapshot<R>;
  sql: TLSyncSqliteWrapper;
};

Methods

getDocumentClock( )

static

Get the current document clock value from storage without fully initializing. Returns null if storage has not been initialized. Useful for comparing storage freshness against external sources.

static getDocumentClock(storage: TLSyncSqliteWrapper): null | number;

Parameters

NameDescription

storage

Returns

null | number;

hasBeenInitialized( )

static

Check if the storage has been initialized (has data in the clock table). Useful for determining whether to load from an external source on first access.

static hasBeenInitialized(storage: TLSyncSqliteWrapper): boolean;

Parameters

NameDescription

storage

Returns

boolean;

getClock( )

getClock(): number;

getSnapshot( )

getSnapshot(): RoomSnapshot;

onChange( )

onChange(
  callback: (arg: TLSyncStorageOnChangeCallbackProps) => void,
): () => void;

Parameters

NameDescription

callback

(
  arg: TLSyncStorageOnChangeCallbackProps,
) => void;

Returns

() => void;

transaction( )

Parameters

NameDescription

callback

opts

Returns


Prev
NodeSqliteWrapper
Next
TLRemoteSyncError