Skip to content

Admin panel

Arrays and JSON fields

An array column is a list the form repeats. A jsonb column is a document: the CMS builds a form for it when the column describes its shape with json.*, and offers free key/value editing when it does not.

// backend/schema/pages.ts
import { defineTable, col, json } from "@rebasepro/server/schema";
export const pages = defineTable("pages", {
columns: {
id: col.uuid().primaryKey().defaultRandom(),
tags: col.text().array(),
settings: col.jsonb().$type<{ theme?: "light" | "dark"; pinned?: boolean }>(),
address: col.jsonb(json.object({
street: json.string().required(),
city: json.string(),
zip: json.string(),
})),
body: col.jsonb(json.array(json.oneOf({
paragraph: json.string(),
image: json.storage({ path: "pages/images/", accept: ["image/*"] }),
quote: json.object({ text: json.string().required(), author: json.string() }),
}))),
},
rls: "admin-only",
});

An array column (col.text().array()) is a repeatable list of the item’s field: tags above is a list of text inputs. .validate({ minItems, maxItems, uniqueItems }) on the array sets how many items the form accepts.

An array of a closed set, col.text({ enum: [...] }).array(), is a multi-select instead; see closed sets. A col.storage().multiple() column is a multi-file upload; see File uploads.

col.jsonb(json.object({ … })) is a group of fields, one per key, each the field its json.* builder calls for: json.string() a text input, json.number() a number, json.date() a date picker, json.storage() an upload field. A .required() key is required in the form, and a .default() fills a new record’s form.

json.array(of) repeats its item. An array of json.oneOf({ … }) is a block editor: each variant is a kind of block a person can add, reorder and remove. body above holds paragraphs, images and quotes.

A json.oneOf on its own, outside an array, and json.unknown() have no structured field: they are edited as free JSON.

col.jsonb().$type<T>() types the document for TypeScript, but T is erased when the code runs, so the server and the CMS know nothing about it. settings above is edited as free key/value pairs. Give the column a json.* shape when the CMS should draw a form for it.

// frontend/src/cms/pages.tsx
import { customizeTable } from "@rebasepro/cms";
import type { pages } from "../../../backend/schema/pages";
export const pagesCms = customizeTable<typeof pages>("pages", {
fields: {
tags: { sortable: false, of: { previewAsTag: true } },
address: {
spreadChildren: true,
fieldOrder: ["zip", "city", "street"],
fields: {
street: { name: "Street and number" },
zip: { name: "Postal code", span: 1 },
},
},
body: {
expanded: true,
oneOf: {
paragraph: { markdown: true },
quote: { fields: { author: { name: "Who said it" } } },
},
},
},
});

On an array:

Option Effect
of The options of each item: any field option, nested as deep as the item is.
sortable Items can be reordered by dragging.
canAddElements Items can be added.
expanded Every item open on load.
minimalistView Items without their frame and header.
oneOf For a list of typed blocks: the options of each variant, by variant name.
enumLabels, enumOrder, storage Written on the array, applied to each item.

On a document:

Option Effect
fields The options of each key, by key; each takes any field option, including its own fields.
fieldOrder The order the keys are shown in.
previewFields The keys shown when the document is drawn as a preview.
spreadChildren The keys laid out as if they were the record’s own fields.
expanded The keys open on load.
minimalistView The keys without the document’s frame.
keyValue Edit as free key/value pairs, even when the column has a shape.

fields, fieldOrder and previewFields are typed by the document’s keys, whether the column is col.jsonb().$type<T>() or has a json.* shape. On a col.jsonb().$type<T>() column, though, the CMS has no shape to apply them to: they are skipped, with a warning in development. Nested options for a key, a variant or an item the document does not have are skipped the same way.