DurableObjectSqliteSyncWrapper

See source code
Table of contents

A wrapper around Cloudflare Durable Object's SqlStorage that implements TLSyncSqliteWrapper.

Use this wrapper with SQLiteSyncStorage to persist tldraw sync state using Cloudflare Durable Object's built-in SQLite storage. This provides automatic persistence that survives Durable Object hibernation and restarts.

class DurableObjectSqliteSyncWrapper implements TLSyncSqliteWrapper {}

Example

import {
  DurableObjectSqliteSyncWrapper,
  SQLiteSyncStorage,
} from '@tldraw/sync-core'

// In your Durable Object class:
class MyDurableObject extends DurableObject {
  private storage: SQLiteSyncStorage

  constructor(ctx: DurableObjectState, env: Env) {
    super(ctx, env)
    const sql = new DurableObjectSqliteSyncWrapper(ctx.storage)
    this.storage = new SQLiteSyncStorage({ sql })
  }
}
// With table prefix to avoid conflicts with other tables
const sql = new DurableObjectSqliteSyncWrapper(this.ctx.storage, {
  tablePrefix: 'tldraw_',
})
// Creates tables: tldraw_documents, tldraw_tombstones, tldraw_metadata

Constructor

Constructs a new instance of the DurableObjectSqliteSyncWrapper class

Parameters

NameDescription

storage

{
  sql: {
    exec(
      sql: string,
      ...bindings: unknown[]
    ): Iterable<any> & {
      toArray(): any[]
    }
  }
  transactionSync(callback: () => any): any
}

config


Properties

config

optional
config?: TLSyncSqliteWrapperConfig | undefined

Methods

exec( )

exec(sql: string): void

Parameters

NameDescription

sql

string

Returns

void

prepare( )

prepare<
  TResult extends TLSqliteRow | void = void,
  TParams extends TLSqliteInputValue[] = [],
>(sql: string): TLSyncSqliteStatement<TResult, TParams>

Parameters

NameDescription

sql

string

Returns

TLSyncSqliteStatement<TResult, TParams>

transaction( )

transaction<T>(callback: () => T): T

Parameters

NameDescription

callback

() => T

Returns

T

Prev
StoreSideEffects
Next
InMemorySyncStorage