Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

docs(api): clarify table vs view usage for reads and writes (#1003)

Carbon Agent62ea6c5f5c6e
diff --git a/docs/app/api-reference/[module]/[resource]/page.tsx b/docs/app/api-reference/[module]/[resource]/page.tsx
index 1f55bf0285..fff576fd6f 100644
--- a/docs/app/api-reference/[module]/[resource]/page.tsx
+++ b/docs/app/api-reference/[module]/[resource]/page.tsx
@@ -3,7 +3,9 @@ import { notFound } from "next/navigation";
import { BaseUrl } from "@/components/api/base-url";
import { Breadcrumb } from "@/components/api/breadcrumb";
import { EndpointSection } from "@/components/api/endpoint-section";
+import { ViewCallout, TableCallout } from "@/components/api/view-callout";
import { allResourceParams, apiBase, getResource } from "@/lib/api-data";
+import { TABLE_VIEW_COMPANIONS, VIEW_TABLE_COMPANIONS } from "@/lib/api-companion-views";
import { pageSeo } from "@/lib/seo";
type Params = { params: Promise<{ module: string; resource: string }> };
@@ -43,6 +45,20 @@ export default async function ResourcePage(props: Params) {
</p>
<BaseUrl path={r.endpoints[0]?.path ?? ""} />
+ {r.kind === "table" && TABLE_VIEW_COMPANIONS[r.table] && (
+ <ViewCallout
+ tableName={r.table}
+ viewName={TABLE_VIEW_COMPANIONS[r.table].viewTable}
+ viewHref={`/api-reference/${TABLE_VIEW_COMPANIONS[r.table].viewModule}/${TABLE_VIEW_COMPANIONS[r.table].viewSlug}`}
+ />
+ )}
+ {r.kind === "view" && VIEW_TABLE_COMPANIONS[r.table] && (
+ <TableCallout
+ tableName={VIEW_TABLE_COMPANIONS[r.table].tableTable}
+ tableHref={`/api-reference/${VIEW_TABLE_COMPANIONS[r.table].tableModule}/${VIEW_TABLE_COMPANIONS[r.table].tableSlug}`}
+ />
+ )}
+
{r.endpoints.map((e) => (
<EndpointSection key={e.id} endpoint={e} base={apiBase} />
))}
diff --git a/docs/app/api-reference/page.tsx b/docs/app/api-reference/page.tsx
index 93ef059bee..c7925cb22f 100644
--- a/docs/app/api-reference/page.tsx
+++ b/docs/app/api-reference/page.tsx
@@ -7,7 +7,10 @@ import {
DocTitle,
H2,
Lead,
- P
+ P,
+ Warn,
+ Table,
+ Row
} from "@/components/api/doc";
import { ContentFooter } from "@/components/api/page-footer";
import { SdkCards } from "@/components/api/sdk-cards";
@@ -63,6 +66,46 @@ export default async function ApiIntroPage() {
</P>
<SdkCards />
+ <H2 id="tables-and-views">Tables &amp; views</H2>
+ <P>
+ The API exposes both <strong>tables</strong> (read/write) and{" "}
+ <strong>views</strong> (read-only, computed). Some resources appear
+ twice — for example <Code>salesInvoice</Code> (a table) and{" "}
+ <Code>salesInvoices</Code> (a view). These are not interchangeable.
+ </P>
+ <Warn title="Always read from the view">
+ Tables like <Code>salesInvoice</Code> and <Code>purchaseInvoice</Code>{" "}
+ have stored total and status columns that are set at creation and{" "}
+ <strong>not updated</strong> when line items change. The corresponding
+ views (<Code>salesInvoices</Code>, <Code>purchaseInvoices</Code>)
+ compute totals, tax, balance, and status live from line items and
+ settlements. If you read from the table, you will get stale data.
+ </Warn>
+ <P>The rule is simple:</P>
+ <Table>
+ <Row head cols="1fr 1fr" cells={["Operation", "Use"]} />
+ <Row
+ cols="1fr 1fr"
+ cells={[
+ "List / retrieve / analytics",
+ <>The <strong>view</strong> (plural, e.g. salesInvoices)</>,
+ ]}
+ />
+ <Row
+ cols="1fr 1fr"
+ cells={[
+ "Create / update / delete",
+ <>The <strong>table</strong> (singular, e.g. salesInvoice)</>,
+ ]}
+ />
+ </Table>
+ <P>
+ In the sidebar, views are marked with an{" "}
+ <span title="eye icon">eye icon</span> and tables with a{" "}
+ <span title="grid icon">grid icon</span>. Affected resource pages also
+ show a banner linking to the correct counterpart.
+ </P>
+
<H2 id="quickstart">Quickstart</H2>
<P>Save your key and the API URL as environment variables:</P>
<CodeBlock html={env} code={ENV} label=".env" />
diff --git a/docs/components/api/view-callout.tsx b/docs/components/api/view-callout.tsx
new file mode 100644
index 0000000000..ce3e25802a
--- /dev/null
+++ b/docs/components/api/view-callout.tsx
@@ -0,0 +1,68 @@
+import Link from "next/link";
+
+/**
+ * Rendered on table resource pages that have a companion view with computed
+ * columns (e.g. salesInvoice → salesInvoices). Tells the reader to use the
+ * view for reads and the table for writes.
+ */
+export function ViewCallout({
+ tableName,
+ viewName,
+ viewHref,
+}: {
+ tableName: string;
+ viewName: string;
+ viewHref: string;
+}) {
+ return (
+ <div className="my-5 rounded-xl border border-ed-blue-border/60 bg-ed-blue-surface px-4 py-3.5">
+ <p className="m-0 text-ed-14 font-semi text-ed-brand-ink">
+ Use the view for reads
+ </p>
+ <p className="m-0 mt-1 text-ed-14 leading-[155%] text-ed-ink/78">
+ The <code className="font-mono text-ed-13 text-ed-brown">{tableName}</code> table
+ has stored total and status columns that may be stale. For accurate computed
+ values (totals, tax, balance, status), read from the{" "}
+ <Link
+ href={viewHref}
+ className="font-medium text-ed-brand-ink underline decoration-ed-blue-border underline-offset-2 hover:decoration-ed-brand-ink"
+ >
+ {viewName}
+ </Link>{" "}
+ view instead. Use this table for <strong>create</strong>, <strong>update</strong>,
+ and <strong>delete</strong> operations.
+ </p>
+ </div>
+ );
+}
+
+/**
+ * Rendered on view resource pages that are the "read" companion to a table.
+ */
+export function TableCallout({
+ tableName,
+ tableHref,
+}: {
+ tableName: string;
+ tableHref: string;
+}) {
+ return (
+ <div className="my-5 rounded-xl border border-ed-hairline bg-ed-warm-50 px-4 py-3.5">
+ <p className="m-0 text-ed-14 font-semi text-ed-ink/80">
+ Read-only view
+ </p>
+ <p className="m-0 mt-1 text-ed-14 leading-[155%] text-ed-ink/78">
+ This view returns computed totals, tax, balance, and status derived from line
+ items and settlements — use it for all reads. To create, update, or delete
+ records, use the{" "}
+ <Link
+ href={tableHref}
+ className="font-medium text-ed-brand-ink underline decoration-ed-blue-border underline-offset-2 hover:decoration-ed-brand-ink"
+ >
+ {tableName}
+ </Link>{" "}
+ table.
+ </p>
+ </div>
+ );
+}
diff --git a/docs/lib/api-companion-views.ts b/docs/lib/api-companion-views.ts
new file mode 100644
index 0000000000..1316c85cfd
--- /dev/null
+++ b/docs/lib/api-companion-views.ts
@@ -0,0 +1,44 @@
+/**
+ * Mapping of tables that have a companion view with computed columns.
+ *
+ * When a table has stored columns (subtotal, totalTax, totalAmount, balance,
+ * status) that are only correct in the companion view — because the view
+ * computes them live from line items and settlements — we surface a callout
+ * directing API consumers to use the view for reads and the table for writes.
+ *
+ * Key: table name (singular), Value: view name (plural)
+ */
+export const TABLE_VIEW_COMPANIONS: Record<
+ string,
+ { viewTable: string; viewSlug: string; viewModule: string }
+> = {
+ salesInvoice: {
+ viewTable: "salesInvoices",
+ viewSlug: "sales-invoices",
+ viewModule: "invoicing",
+ },
+ purchaseInvoice: {
+ viewTable: "purchaseInvoices",
+ viewSlug: "purchase-invoices",
+ viewModule: "invoicing",
+ },
+};
+
+/**
+ * Reverse lookup: view → table
+ */
+export const VIEW_TABLE_COMPANIONS: Record<
+ string,
+ { tableTable: string; tableSlug: string; tableModule: string }
+> = {
+ salesInvoices: {
+ tableTable: "salesInvoice",
+ tableSlug: "sales-invoice",
+ tableModule: "invoicing",
+ },
+ purchaseInvoices: {
+ tableTable: "purchaseInvoice",
+ tableSlug: "purchase-invoice",
+ tableModule: "invoicing",
+ },
+};