Last active
February 25, 2025 11:56
-
-
Save wernst/9a1a42a7bb95bc2268b38612e9ee4339 to your computer and use it in GitHub Desktop.
Triplit op-sqlite storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { OPSQLiteConnection, open } from '@op-engineering/op-sqlite'; | |
import { | |
AsyncSQLiteAdapter, | |
AsyncSQLiteExecutor, | |
AsyncAdapterSQLiteStorage | |
} from '@triplit/db/storage/adapter-sqlite'; | |
import { AsyncTupleStorageApi } from '@triplit/tuple-database'; | |
// Create DB instance | |
const db = open({ name: 'triplit.db' }); | |
// Create adapter | |
const adapter = new OPSQLiteAdapter(db); | |
// Create storage provider | |
export const storage = new AsyncAdapterSQLiteStorage(adapter); | |
class OPSQLiteAdapter implements AsyncSQLiteAdapter { | |
constructor(private db: OPSQLiteConnection) {} | |
async execute(sql: string, args?: any[] | undefined) { | |
return this.db.execute(sql, args); | |
} | |
normalizeResults(results: any): { key: string; value: string }[] { | |
if (!results.rows) return []; | |
return results.rows._array as { key: string; value: string }[]; | |
} | |
async transact(fn: (adapter: AsyncSQLiteExecutor) => Promise<void>) { | |
await this.db.transaction(async (tx) => { | |
await fn({ | |
execute: (sql, args) => { | |
return tx.execute(sql, args); | |
}, | |
}); | |
tx.commit(); | |
}); | |
} | |
async close() { | |
this.db.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
updated to work with
"@op-engineering/op-sqlite": "^11.4.4",