Typed tables
Generate TypeScript types from your app's schema, so @authoric/js knows your tables, columns and custom actions.
Generate a types file for your app, then pass its Database type to createClient:
import { createClient } from "@authoric/js";
import type { Database } from "./authoric-types";
const authoric = createClient<Database>({ url, appUid });
const { items } = await authoric.from("invoices").list();
items[0].status; // "open" | "paid"
await authoric.from("invoices").create({ customer_uid, total: 4200 }); // checked against your schemaYour editor then completes table and column names, and a typo, a missing required column or a write to a table that does not allow it is a compile error.
Queries are typed too:
const { items } = await authoric.from("invoices").list({
where: { status: { $in: ["received", "matched"] } }, // each column takes its own type
columns: ["invoice_number", "amount"],
include: ["vendor"], // only the table's own Relations
});
items[0].amount; // in the answer: the columns you chose, and uid
items[0].vendor; // the vendors table's Row, or null
items[0].memo; // compile error: not one of `columns`Generate the file
From the console: open the app, then Deployments › Client types › Download types.
From a terminal or CI, with a deploy key for the app:
export AUTHORIC_DEPLOY_KEY="<the app's deploy key>"
npx @authoric/js types --app <app uid> --url https://api.authoric.ai --out src/authoric-types.d.tsThe command reads the key from AUTHORIC_DEPLOY_KEY, never from a flag, and sends it only to the
address you give with --url (or AUTHORIC_API). Regenerate after you change a table.
What is in it
Everything your app's API serves to its users, as it serves it:
- Your tables, each as a
Row(what reads answer), anInsert(what a create takes, with optional columns optional), anUpdate(every column optional) andRelations(each reference's name, without_uid, and the table it points at). - Rows named by
uid, and each reference to another table as<name>_uid. - Redacted columns typed as their value or
"[redacted]", since that is what reads answer. - No type for an operation a table locks: a table that takes no updates has
Update: never. - Custom actions, by path, method and input.
The file holds types only. Nothing in it reaches your users' browsers, since types are removed when your app is built.