saveRow

tcmts.studio.saveRow · #gydcb2iw · term · runs on the server

uses tcmts.studio.editableCodebase, which uses tcmts.share.servedCodebase, which uses process

Save row id of table (a new row, if there's no id) as json says: read by the table's row type (ISO strings where it has dates, and the like), checked against it as the app's own writes are, refused if it would share a unique field with another row. The row's id.

import { fromJson, tableSchemaOf, writeRow } from "#codebase/tcmts/cli/data_pages";
import { HttpError } from "#codebase/tcmts/server";
import { editableCodebase } from "#codebase/tcmts/studio";

export async function saveRow(table: string, id: string, json: string): Promise<string> {
  const cb = editableCodebase();
  const t = tableSchemaOf(cb, table);
  if (!t)
    throw new HttpError(
      404,
      `No table "${table}" is declared in the codebase: there's no row type to check a row against.`,
    );
  let parsed: unknown;
  try {
    parsed = JSON.parse(json);
  } catch (e) {
    throw new HttpError(400, `That isn't JSON: ${(e as Error).message}`);
  }
  try {
    return writeRow(cb, table, id || undefined, fromJson(t.s, parsed, t.defs));
  } catch (e) {
    const m = (e as Error).message;
    throw new HttpError(/another row/.test(m) ? 409 : 400, m);
  }
}

uses

used by