dataTables

tcmts.cli.data_pages.dataTables · #zjtwqgsl · term · runs on the server

uses tcmts.cli.data_pages.withStore, which uses node:path

Every table with data, and every table the codebase declares: rows, how many are stored as an older row type than the declaring definition's, when one was last written, and that definition.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { declaredTables, withStore } from "#codebase/tcmts/cli/data_pages";

export function dataTables(cb: Codebase): {
  table: string;
  rows: number;
  older: number;
  last?: number;
  path?: string;
  hash?: string;
  fields?: { name: string; type: string }[];
}[] {
  const declared = declaredTables(cb);
  const stored =
    withStore(
      cb,
      (db) =>
        db
          .prepare(
            "SELECT tbl, schema_hash, count(*) AS n, max(updated) AS last FROM rows GROUP BY tbl, schema_hash",
          )
          .all() as { tbl: string; schema_hash: string; n: number; last: number }[],
    ) ?? [];
  const names = [...new Set([...declared.keys(), ...stored.map((r) => r.tbl)])].sort();
  return names.map((name) => {
    const d = declared.get(name);
    const rows = stored.filter((r) => r.tbl === name);
    const total = rows.reduce((n, r) => n + r.n, 0);
    const older = d
      ? rows.filter((r) => r.schema_hash !== d.schema).reduce((n, r) => n + r.n, 0)
      : 0;
    const last = rows.reduce((m, r) => Math.max(m, r.last), 0);
    return {
      table: name,
      rows: total,
      older,
      ...(last ? { last } : {}),
      ...(d ? { path: d.path, hash: d.hash, fields: d.fields } : {}),
    };
  });
}

uses

used by