DrizzleAdapter
Storage adapter for local data persistence using Drizzle ORM.
Import
import { DrizzleAdapter } from '@gluonic/client-drizzle'Signature
function DrizzleAdapter(config: DrizzleAdapterConfig): StorageAdapterParameters
config: DrizzleAdapterConfig
| Property | Type | Required | Description |
|---|---|---|---|
db | DrizzleDB | Yes | Drizzle database instance |
opDb | OPSQLiteDB | No | OP-SQLite instance (React Native only) |
tables | Record<string, TableConfig> | No | Custom table mappings |
Returns
StorageAdapter instance with methods for local storage operations.
Examples
React Native (OP-SQLite)
import { drizzle } from 'drizzle-orm/op-sqlite'
import { open } from '@op-rs/sqlite'
import { DrizzleAdapter } from '@gluonic/client-drizzle'
export const opDb = open('app.db')
export const db = drizzle(opDb)
const storage = DrizzleAdapter({ db, opDb })Web (Better-SQLite3)
import { drizzle } from 'drizzle-orm/better-sqlite3'
import Database from 'better-sqlite3'
import { DrizzleAdapter } from '@gluonic/client-drizzle'
const sqlite = new Database('app.db')
export const db = drizzle(sqlite)
const storage = DrizzleAdapter({ db })With Custom Tables
import { relay, transcript } from './schema'
const storage = DrizzleAdapter({
db,
opDb,
tables: {
relay: { model: relay, idField: 'id' },
transcript: { model: transcript, idField: 'id' }
}
})What It Does
DrizzleAdapter implements the StorageAdapter interface:
interface StorageAdapter {
// Row operations
getRow(t: string, id: string): Promise<WireRow | null>
putRow(row: WireRow): Promise<void>
deleteRow(t: string, id: string): Promise<void>
// Bulk operations
putRows(rows: WireRow[]): Promise<void>
deleteRows(items: Array<{ t: string, id: string }>): Promise<void>
// Query operations
listByType(t: string): Promise<WireRow[]>
listByIndex(t: string, indexKey: string, indexVal: string): Promise<WireRow[]>
// Metadata
getLastSyncId(): Promise<number>
setLastSyncId(n: number): Promise<void>
// Transaction queue
enqueueTx(tx: Transaction): Promise<void>
dequeueTx(id: string): Promise<void>
listTx(): Promise<Transaction[]>
// Utility
clearAll(): Promise<void>
}Internal Tables
DrizzleAdapter creates these tables automatically:
sync_rows_{modelType}
Stores synced data for each model type.
CREATE TABLE sync_rows_task (
id TEXT PRIMARY KEY,
data TEXT NOT NULL -- JSON serialized WireRow
)sync_tx_queue
Stores pending mutations.
CREATE TABLE sync_tx_queue (
id TEXT PRIMARY KEY,
type TEXT NOT NULL,
model_id TEXT NOT NULL,
patch TEXT NOT NULL,
prev TEXT,
op TEXT,
created_at INTEGER
)sync_meta
Stores metadata like lastSyncId.
CREATE TABLE sync_meta (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
)Platform Support
| Platform | Database | Package |
|---|---|---|
| React Native | OP-SQLite | @op-rs/sqlite |
| Web | Better-SQLite3 | better-sqlite3 |
| Electron | Better-SQLite3 | better-sqlite3 |
Same adapter works on all platforms - just use the appropriate Drizzle driver.
Performance
DrizzleAdapter is optimized for:
- ✅ Batch operations (putRows, deleteRows)
- ✅ Indexed queries (listByIndex)
- ✅ Transaction support
- ✅ Minimal overhead
Benchmarks:
getRow: ~1msputRow: ~2msputRows(1000): ~50ms (batched)listByType(10k rows): ~30ms
See Also
- SyncClient - Create client
- StorageAdapter Interface - Full interface
- Client Setup - Setup guide
Last updated on