readRow

tcmts.cli.data_pages.readRow · #63tapmd2 · term · runs on the server

uses node:path

Row id of table as it's stored: its value (decoded), when it was written, and the schema it was written with.

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

export function readRow(
  cb: Codebase,
  table: string,
  id: string,
): { value: unknown; updated: number; schema: string } | 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 {
    const r = db
      .prepare("SELECT data, schema_hash, updated FROM rows WHERE tbl = ? AND id = ?")
      .get(table, id) as { data: string; schema_hash: string; updated: number } | undefined;
    return r && { value: decode(r.data), updated: r.updated, schema: r.schema_hash };
  } finally {
    db.close();
  }
}

uses

used by