SDK

Four small packages, provided by the platform, cover what an app needs beyond React and SQL. Rows are typed from your db/schema, template vars from your messages/, config from app.config.ts, so a misspelled column is a type error at its line. Every read and write goes through your access policies.

PackageWhereFor
@appbricx/uiroutes/, components/, lib/Route meta, links, reads and writes, sign-in, live updates, components, icons, config
@appbricx/serverserver/defineAction, input schemas, the server ctx
@appbricx/workflowsworkflows/Triggers and durable steps
@appbricx/defineapp.config.tsdefineApp

Never add them (or React) to package.json: the platform provides one copy of each.

Read data

import { useQuery, useRow, useNamedQuery } from "@appbricx/ui";

const { rows, loading, hasMore, nextCursor } = useQuery("booking", {
  where: { status: "requested" },
  filter: [{ field: "team_size", op: "gte", value: 10 }],
  sort: "-created_at",
  limit: 20,
  embed: "experience(title,city)",
});
const { row, notFound } = useRow("booking", id);
const stats = useNamedQuery("experience-bookings", { experience: id });

Reads run as the signed-in user. On a public page they run on the server as a signed-out visitor and the browser hydrates from the same rows. useLive("booking") keeps them current as rows change.

Write data

import { useTable } from "@appbricx/ui";
import { confirmBooking } from "../server/bookings";

const bookings = useTable("booking");
await bookings.create({ experience_id: id, hr_id: me, team_size: 12, email });   // policies and triggers apply
await bookings.update(bookingId, { notes: "Vegetarian lunch" });
const r = await confirmBooking({ booking: bookingId });                            // a typed server action

A refused write rejects with the server's message: your trigger's raise exception text, or the policy that refused. Use an action when a change needs more than one row, a secret, or a check the browser can't be trusted with.

Server actions

// server/payments.ts
import { defineAction, v } from "@appbricx/server";

export const startPayment = defineAction({
  input: v.object({ booking: v.uuid(), amount: v.integer({ min: 1 }) }),
  roles: ["hr"],
  handler: async (ctx, input) => {
    const res = await ctx.http.fetch(String(ctx.config.PAYMENTS_URL ?? "") + "/v1/orders", {
      method: "POST",
      auth: { basic: ["teamday", ctx.secrets.ref("PAYMENTS_KEY")] },   // the key never enters the code
      json: { amount: input.amount, receipt: input.booking },
    });
    if (!res.ok) throw new Error("The payment provider answered " + res.status + ".");
    return { order: res.json<{ id: string }>()?.id ?? "" };
  },
});

Sign-in

import { useAuth, Link } from "@appbricx/ui";

const auth = useAuth();
if (auth.status === "unknown") return null;
return auth.user
  ? <button onClick={() => void auth.signOut()}>Sign out {auth.user.name}</button>
  : <Link to="/signin">Sign in</Link>;

Workflows

import { on } from "@appbricx/workflows";

export const bookingCreated = on.created<{ id: string; email: string; ref: string }>("booking", async ({ row }, step) => {
  await step.send.email("booking-received", { to: row.email, vars: { ref: row.ref } });
  await step.sleep("2d");
  const b = await step.run("reload", () => step.ctx.db.get("booking", row.id));
  if (b?.status === "requested") await step.send.notify("booking-waiting", { role: "ops", vars: { ref: row.ref } });
});

Reference

Not in the SDK

  • A global fetch to other sites from the browser: call an action that uses ctx.http.fetch.
  • Node built-ins in server code (fs, node:crypto): server code runs in a V8 isolate.
  • Ready-made table and form blocks: write them with React and the hooks, or add a component library from npm.

Related