withStore

tcmts.cli.data_pages.withStore · #5x4jfj6u · term · runs on the server

uses node:path

Open the app data store read-only, or undefined while there's no data yet.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import fs from "node:fs";
import path from "node:path";
import { DatabaseSync } from "node:sqlite";

export function withStore<T>(
  cb: Codebase,
  fn: (db: InstanceType<typeof DatabaseSync>) => T,
): T | undefined {
  const file = path.join(cb.lay.tcmDir, "data", "store.sqlite");
  if (!fs.existsSync(file)) return undefined;
  const db = new DatabaseSync(file, { readOnly: true });
  try {
    return fn(db);
  } finally {
    db.close();
  }
}

uses

used by