crm
uses node:fs
import fs from "node:fs"; import path from "node:path"; export const crm = (dir: string, o: { field?: string; opts?: string; value?: string }) => fs.writeFileSync( path.join(dir, "scratch.ts"), `// tcm: namespace crm import assert from "node:assert/strict"; import { table, request } from "#codebase/lib/server"; export type User = { name: string; email: string; joined: Date${o.field ?? ""} }; export const users = table<User>("users"${o.opts ?? ""}); export async function addUser(name: string, email: string): Promise<string> { request(); return users.insert({ name, email, joined: new Date(0)${o.value ?? ""} }); } export async function listUsers(): Promise<string[]> { request(); return (await users.find()).map((r) => r.value.name); } export async function badInsert(): Promise<string> { request(); return users.insert({ name: "x" } as unknown as User); } export async function main() { document.body.textContent = [await addUser("ada", "a@x"), await listUsers(), await badInsert()].join(); } // tables work in tests too, in memory export const testTablesInMemory = async () => { const id = await users.insert({ name: "t", email: "t@x", joined: new Date(5)${o.value ?? ""} }); assert.equal((await users.get(id))?.joined.getTime(), 5, "dates come back as dates"); assert.equal((await users.find({ name: "t" })).length, 1); await assert.rejects(users.put("bad", { name: 1 } as unknown as User), /table "users": value\\.name should be a string/); }; `, )