MCPcopy
hub / github.com/aspen-cloud/triplit / BunSQLiteKVStore

Class BunSQLiteKVStore

packages/db/src/kv-store/storage/bun-sqlite.ts:36–237  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34};
35
36export class BunSQLiteKVStore implements KVStore {
37 db: Database;
38 private statements: SQLiteKVState['statements'];
39 private transactions: SQLiteKVState['transactions'];
40 private walGuard?: NodeJS.Timer;
41
42 constructor(databasePath: string, options?: SQLiteKVStoreOptions);
43 constructor(database: Database, options?: SQLiteKVStoreOptions);
44 constructor(arg0: string | Database, options: SQLiteKVStoreOptions = {}) {
45 if (typeof arg0 === 'string') {
46 this.db = new Database(arg0, { create: true });
47 } else {
48 this.db = arg0;
49 }
50 const parsedOptions = parseSqliteKvStoreOptions(options);
51 this.db.exec(parsedOptions.pragma);
52 this.createTable();
53 this.statements = {
54 get: this.db.query(STATEMENTS.get),
55 set: this.db.query(STATEMENTS.set),
56 delete: this.db.query(STATEMENTS.delete),
57 deleteRange: this.db.query(STATEMENTS.deleteRange),
58 scan: this.db.query(STATEMENTS.scan),
59 scanValues: this.db.query(STATEMENTS.scanValues),
60 count: this.db.query(STATEMENTS.count),
61 countRange: this.db.query(STATEMENTS.countRange),
62 truncate: this.db.query(STATEMENTS.truncate),
63 };
64 this.transactions = {
65 write: this.db.transaction(
66 (sets: Iterable<[Tuple, any]>, deletes: Iterable<Tuple>) => {
67 for (const key of deletes) {
68 const encodedKey = encodeTuple(key);
69 this.freeStatement(this.statements.delete).run(encodedKey);
70 }
71 for (const [key, value] of sets) {
72 const encodedKey = encodeTuple(key);
73 const encodedValue = JSON.stringify(value);
74 this.freeStatement(this.statements.set).run(
75 encodedKey,
76 encodedValue
77 );
78 }
79 }
80 ),
81 };
82 this.walGuard = this.startWalGuard(parsedOptions);
83 }
84
85 private startWalGuard(options: Required<SQLiteKVStoreOptions>) {
86 if (this.walGuard) {
87 clearInterval(this.walGuard);
88 }
89 const dbPath = this.db.filename;
90 const walFile = `${dbPath}-wal`;
91 const walCheck = setInterval(() => {
92 walSizeGuard(this.db, walFile, {
93 restartMax: options.checkpointRestart,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected