Data
ALB'DO has a database built in, called FORGE. You declare a collection in the config; the table, the query that reads it, the live channel that keeps every open page current, and its TypeScript types all follow from that one declaration. There is no ORM, no migrations folder and no API layer to write.
Declare a collection
// albedo.config.ts
export default {
// …
forge: {
guestbook: {
fields: { author: "text", message: "text" },
},
},
};Restart the server and the table exists. The data lives in forge.db, one file beside the config.
Fields
| Type | Holds | A row gives you |
|---|---|---|
text | a string | a string |
int | a whole number | a number |
real | a floating-point number | a number |
bool | true or false | true / false, never 1 / 0 |
timestamp | a point in time | epoch milliseconds — new Date(row.at) works as is |
A trailing ? makes a field nullable: nickname: "text?".
Every collection also has an id, assigned by the database when a row is written. Do not declare it and do not pass it when writing.
More options
| Key | Does |
|---|---|
order_by | The order rows are read in, such as "joined_at desc, id". Defaults to id ascending |
partition_by | Splits the collection into independent live channels, one per value of a field — see below |
seed | Rows written once, into an empty table, on first start. For reference data your app needs |
Read it
useSharedSlot reads a collection in a component:
import { useSharedSlot } from "albedo";
export default function Guestbook() {
const entries = useSharedSlot("guestbook");
return (
<ul>
{entries.map((entry) => (
<li key={entry.id}>
{entry.author}: {entry.message}
</li>
))}
</ul>
);
}The page arrives with the rows already in it. After that, when any row changes — from any visitor — every page showing the list updates, without a refresh and without code in the page asking for it. Only the changed row crosses the wire, and rows that did not change keep their DOM nodes, so focus, selection and scroll survive.
Two rules make that work:
- Key the rows with
key={row.id}. The key is how a change finds its row. - Do not guard the
.map().(entries || []).map(…)hides the read from the compiler, and the list renders once and never updates.
A component that reads a collection is Tier B: the server owns the data, so no component JavaScript is sent.
Typed reads
Import the collection from albedo/forge instead of naming it by string, and the rows are typed from your config:
import { useSharedSlot } from "albedo";
import { guestbook } from "albedo/forge";
const entries = useSharedSlot(guestbook); // { id, author, message }[]The types are generated into .albedo/forge.d.ts on every build.
Warning
Reading a collection on a page sends its rows to every browser that opens that page. Never read a collection of private data — email addresses, orders — on a public page. A count of it belongs in a separate collection that holds no personal data.
One channel per room, per user
partition_by splits a collection into channels that never see each other's rows:
forge: {
messages: {
fields: { room: "text", author: "text", body: "text" },
partition_by: "room",
},
},// routes/room/[id].tsx
import { useSharedSlot } from "albedo";
import { messages } from "albedo/forge";
export default function Room({ params }) {
const rows = useSharedSlot(messages.where({ room: params.id }));
// …
}A partitioned collection is always read one partition at a time; reading it whole fails the build. /room/lobby and /room/quiet read the same table and never see each other's messages, and a write to one reaches only the pages watching it. Partitioning by the signed-in user's id is how per-user data works: the read is the permission, so there is no separate access rule to forget.
Write to it
Writes happen on the server, inside an action. Three functions are available there without importing anything:
append("guestbook", { author: "Ada", message: "First light" }); // add a row
update("guestbook", 3, { message: "Edited" }); // change row 3
remove("guestbook", 3); // delete row 3The write is applied after the action returns. The collection is then re-read, and the change is sent to every page watching it. The same functions work in background jobs.
Changing a collection
- Adding a nullable field (
"text?") is applied on the next start: the column is added and existing rows keep their data. The server says what it changed. - Anything else — removing or renaming a field, changing a type, adding a required field — stops the server from starting, and names the field. There is no value it could invent for rows written before the change, and a refusal never touches your data.
Collection names starting with albedo_ are reserved for ALB'DO's own tables and are refused.
Next: actions and forms.