37 lines · 823 B · TypeScript
RawDownload
import fs from "node:fs";import path from "node:path";import { DatabaseSync } from "node:sqlite"; import { runStateMigrations } from "./stateMigrations"; export interface StateDbOptions {  dbPath: string;} export class StateDb {  readonly dbPath: string;  readonly db: DatabaseSync;   constructor(options: StateDbOptions) {    this.dbPath = path.resolve(options.dbPath);    fs.mkdirSync(path.dirname(this.dbPath), { recursive: true });    this.db = new DatabaseSync(this.dbPath);    runStateMigrations(this.db);  }   close(): void {    this.db.close();  }   transaction<T>(fn: () => T): T {    this.db.exec("BEGIN IMMEDIATE;");    try {      const result = fn();      this.db.exec("COMMIT;");      return result;    } catch (error) {      this.db.exec("ROLLBACK;");      throw error;    }  }}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t