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 {
  DurableObjectSqliteSyncWrapper,
  SQLiteSyncStorage,
} from '@tldraw/sync-core'

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

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

Returns

() => void

transaction( )

Parameters

Returns


Prev
NodeSqliteWrapper
Next
TLRemoteSyncError