cogens 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/admin/app.ts CHANGED
@@ -23,10 +23,10 @@ import { ADMIN_CSS, ADMIN_JS } from './assets.ts';
23
23
  import type { ResolveRef } from './fields.ts';
24
24
  import { parseForm } from './forms.ts';
25
25
  import { can, mayWrite, mayWriteColumn, ruleRow, writable, type Writable } from './rules.ts';
26
- import type { AdminAction, AdminAssets, AdminOptions, ExternalCollection, ExternalRow } from './types.ts';
26
+ import type { AdminAction, AdminAssets, AdminOptions, AdminRelated, ExternalCollection, ExternalRow } from './types.ts';
27
27
  import {
28
- confirmPage, entryListPage, entryPage, externalListPage, externalRowPage, messagePage, noticeText, signInPage, typeListPage,
29
- type Column, type Row, type Shell
28
+ confirmPage, entryListPage, entryPage, externalListPage, externalRowPage, homePage, messagePage, noticeText, signInPage,
29
+ type Belonging, type Column, type HomeSection, type ListColumn, type Row, type Shell
30
30
  } from './views.ts';
31
31
 
32
32
  export const DEFAULT_ASSETS: AdminAssets = {
@@ -41,6 +41,21 @@ const PAGE = 50;
41
41
  const FORM = /^(application\/x-www-form-urlencoded|multipart\/form-data)\b/i;
42
42
  /** Most rows offered in a target picker. */
43
43
  const PICKER_CAP = 2000;
44
+ /** Most children or related rows listed on an entry's page. */
45
+ const BELONGING_CAP = 50;
46
+ /** Most children counted for "and N more". */
47
+ const COUNT_CAP = 1000;
48
+ /** Ids per read when resolving reference names. */
49
+ const NAME_CHUNK = 90;
50
+ /** The built-in columns a list may show, and their headings. */
51
+ const BUILTIN_COLUMNS: Record<string, string> = {
52
+ slug: 'Slug',
53
+ status: 'Status',
54
+ owner_id: 'Owner',
55
+ parent_id: 'Parent',
56
+ created_at: 'Created',
57
+ updated_at: 'Updated'
58
+ };
44
59
 
45
60
  const status = (error: unknown, fallback: number): number => {
46
61
  const n = (error as { status?: unknown } | null)?.status;
@@ -57,6 +72,55 @@ export function createAdmin<H extends HostPrincipal = never>(options: AdminOptio
57
72
  const shell: Shell = { mount, home: mount || '/', siteName: options.siteName ?? 'Admin', assets, session: Boolean(options.session) };
58
73
  const externals = new Map((options.externalCollections ?? []).map(c => [c.id, c]));
59
74
  const typeActions = options.typeActions ?? {};
75
+ const typeRelated = options.typeRelated ?? {};
76
+ /** A host action with a built-in control's id takes its place: the control is not drawn and its route is refused. */
77
+ const replaced = (typeId: string, verb: 'publish' | 'unpublish' | 'delete') => (typeActions[typeId] ?? []).some(a => a.id === verb);
78
+
79
+ // The host's arrangement names types, tables and fields; a name that matches nothing is a mistake in the host's config.
80
+ const known = (id: string) => cms.types.has(id) || externals.has(id);
81
+ for (const section of options.sections ?? []) {
82
+ for (const id of section.items) {
83
+ if (!known(id)) throw new ConfigError(`The admin's section "${section.title}" lists "${id}", which is no type or table.`, id);
84
+ }
85
+ }
86
+ if (options.ownerTable !== undefined && !externals.has(options.ownerTable)) {
87
+ throw new ConfigError(`The admin's owner table "${options.ownerTable}" is no external collection.`, options.ownerTable);
88
+ }
89
+ const ownerMeta = (): FieldMeta | undefined =>
90
+ options.ownerTable ? { label: 'Owner', fieldType: 'reference', refContentType: options.ownerTable, required: false, system: false } : undefined;
91
+ /** The columns of a store type's list, or undefined for the default. */
92
+ const typeColumns = new Map<string, ListColumn[]>();
93
+ for (const [typeId, names] of Object.entries(options.typeColumns ?? {})) {
94
+ const type = cms.types.get(typeId);
95
+ if (!type) throw new ConfigError(`The admin names columns for "${typeId}", which is no type.`, typeId);
96
+ typeColumns.set(
97
+ typeId,
98
+ names.filter(n => n !== type.displayField).map((name): ListColumn => {
99
+ const meta = type.fieldMeta[name];
100
+ if (meta) return { name, label: meta.label, meta };
101
+ if (name === 'parent_id' && type.parent) {
102
+ const parent = cms.types.get(type.parent);
103
+ return { name, label: parent?.name ?? 'Parent', meta: { label: parent?.name ?? 'Parent', fieldType: 'reference', refContentType: type.parent, required: false, system: false } };
104
+ }
105
+ if (name === 'owner_id' && type.owned) return { name, label: 'Owner', meta: ownerMeta() };
106
+ if (name in BUILTIN_COLUMNS && name !== 'parent_id' && name !== 'owner_id') return { name, label: BUILTIN_COLUMNS[name]! };
107
+ throw new ConfigError(`The admin's columns for "${typeId}" name "${name}", which is not one of its fields or columns.`, typeId, name);
108
+ })
109
+ );
110
+ }
111
+ const tableColumns = new Map<string, ListColumn[]>();
112
+ for (const collection of externals.values()) {
113
+ if (!collection.columns) continue;
114
+ tableColumns.set(
115
+ collection.id,
116
+ collection.columns.filter(n => n !== collection.displayField).map((name): ListColumn => {
117
+ const meta = collection.fields[name]?.meta;
118
+ if (meta) return { name, label: meta.label, meta };
119
+ if (name === 'owner_id') return { name, label: 'Owner', meta: ownerMeta() };
120
+ throw new ConfigError(`The columns of "${collection.id}" name "${name}", which is not one of its fields.`, collection.id, name);
121
+ })
122
+ );
123
+ }
60
124
 
61
125
  const app = new Hono<{ Variables: { principal: P } }>();
62
126
 
@@ -185,45 +249,105 @@ export function createAdmin<H extends HostPrincipal = never>(options: AdminOptio
185
249
  const notice = (c: Ctx) => noticeText(c.req.query('notice'));
186
250
  const label = (type: ContentType<any>, e: Entry) => String(e.data[type.displayField] ?? '') || e.slug || e.id;
187
251
 
188
- /** Where each reference field's ids lead and what to call them. Reads only what the caller may read. */
189
- async function resolver(c: Ctx, fields: Record<string, FieldMeta>, data: Record<string, unknown>): Promise<ResolveRef> {
252
+ /** The name a row of a store type or a table is shown by. */
253
+ const rowLabel = (c: Ctx, tableId: string, row: ExternalRow): string => {
254
+ const type = readable(c, tableId);
255
+ const display = type ? type.displayField : external(c, tableId)?.displayField;
256
+ return (display ? String(row[display] ?? '') : '') || (typeof row.slug === 'string' ? row.slug : '') || row.id;
257
+ };
258
+ const hrefOf = (c: Ctx, tableId: string, id: string): string | null =>
259
+ readable(c, tableId) ? `${mount}/t/${tableId}/${encodeURIComponent(id)}` : external(c, tableId) ? `${mount}/x/${tableId}/${encodeURIComponent(id)}` : null;
260
+
261
+ /**
262
+ * Where each reference field's ids lead and what to call them, for one entry or a whole list of them. Reads only
263
+ * what the caller may read, and one read per referred type for every id named (in chunks of 90).
264
+ */
265
+ async function resolver(c: Ctx, fields: Record<string, FieldMeta | undefined>, rows: Record<string, unknown>[]): Promise<ResolveRef> {
190
266
  const wanted = new Map<string, Set<string>>();
191
267
  for (const [name, meta] of Object.entries(fields)) {
192
- if ((meta.fieldType !== 'reference' && meta.fieldType !== 'references') || !meta.refContentType) continue;
193
- const value = data[name];
194
- const ids = Array.isArray(value) ? value : typeof value === 'string' && value ? [value] : [];
268
+ if (!meta || (meta.fieldType !== 'reference' && meta.fieldType !== 'references') || !meta.refContentType) continue;
195
269
  const set = wanted.get(meta.refContentType) ?? new Set<string>();
196
- for (const id of ids) if (typeof id === 'string') set.add(id);
270
+ for (const data of rows) {
271
+ const value = data[name];
272
+ const ids = Array.isArray(value) ? value : typeof value === 'string' && value ? [value] : [];
273
+ for (const id of ids) if (typeof id === 'string') set.add(id);
274
+ }
197
275
  wanted.set(meta.refContentType, set);
198
276
  }
199
277
  const names = new Map<string, string>();
200
278
  for (const [refType, ids] of wanted) {
201
- const list = [...ids].slice(0, 90);
202
- if (list.length === 0) continue;
279
+ const all = [...ids];
280
+ if (all.length === 0) continue;
203
281
  const type = readable(c, refType);
204
282
  const collection = external(c, refType);
205
283
  if (type) {
206
- for (const e of await apiOf(c).list(refType, { where: { id: { in: list } }, limit: 90 })) names.set(`${refType}:${e.id}`, label(type, e));
207
- } else if (collection) {
208
- for (const id of list.slice(0, 20)) {
209
- const row = await collection.get(c, id);
210
- if (row) names.set(`${refType}:${id}`, String(row[collection.displayField] ?? '') || id);
284
+ for (let i = 0; i < all.length; i += NAME_CHUNK) {
285
+ const list = all.slice(i, i + NAME_CHUNK);
286
+ for (const e of await apiOf(c).list(refType, { where: { id: { in: list } }, limit: list.length })) names.set(`${refType}:${e.id}`, label(type, e));
211
287
  }
288
+ } else if (collection) {
289
+ const found = collection.getMany
290
+ ? await collection.getMany(c, all)
291
+ : (await Promise.all(all.slice(0, BELONGING_CAP).map(id => collection.get(c, id)))).filter((r): r is ExternalRow => r !== null);
292
+ for (const row of found) names.set(`${refType}:${row.id}`, String(row[collection.displayField] ?? '') || row.id);
212
293
  }
213
294
  }
214
- return (refType, id) => {
215
- const href = readable(c, refType) ? `${mount}/t/${refType}/${encodeURIComponent(id)}` : external(c, refType) ? `${mount}/x/${refType}/${encodeURIComponent(id)}` : null;
216
- return { href, label: names.get(`${refType}:${id}`) ?? id };
217
- };
295
+ return (refType, id) => ({ href: hrefOf(c, refType, id), label: names.get(`${refType}:${id}`) ?? id });
296
+ }
297
+
298
+ /** The host's related lists for an entry or row, as the caller may read them. */
299
+ async function relatedLists(c: Ctx, lists: AdminRelated<H>[], id: string, entry: Entry | ExternalRow): Promise<Belonging[]> {
300
+ const out: Belonging[] = [];
301
+ for (const list of lists) {
302
+ if (!readable(c, list.table) && !external(c, list.table)) continue;
303
+ const answer = await list.rows({ c, principal: principalOf(c), id, entry });
304
+ const rows = Array.isArray(answer) ? answer : answer.rows;
305
+ const total = Array.isArray(answer) ? rows.length : Math.max(answer.total, rows.length);
306
+ out.push({
307
+ label: list.label,
308
+ total,
309
+ items: rows.slice(0, BELONGING_CAP).map(row => ({
310
+ href: hrefOf(c, list.table, row.id),
311
+ label: rowLabel(c, list.table, row),
312
+ status: typeof row.state === 'string' ? row.state : typeof row.status === 'string' ? row.status : undefined
313
+ }))
314
+ });
315
+ }
316
+ return out;
317
+ }
318
+
319
+ /** An entry's children: the entries of every type whose parent is its type and that the caller may read. */
320
+ async function childLists(c: Ctx, type: ContentType<any>, entry: Entry): Promise<Belonging[]> {
321
+ const out: Belonging[] = [];
322
+ for (const child of cms.types.values()) {
323
+ if (child.parent !== type.id || !readable(c, child.id)) continue;
324
+ const found = await apiOf(c).list(child.id, { where: { parent_id: entry.id }, orderBy: 'created_at', direction: 'asc', limit: COUNT_CAP + 1 });
325
+ out.push({
326
+ label: child.name,
327
+ total: found.length,
328
+ more: `${mount}/t/${child.id}?parent=${encodeURIComponent(entry.id)}`,
329
+ items: found.slice(0, BELONGING_CAP).map(e => ({ href: `${mount}/t/${child.id}/${encodeURIComponent(e.id)}`, label: label(child, e), status: e.status }))
330
+ });
331
+ }
332
+ return out;
218
333
  }
219
334
 
220
335
  // ── Types ─────────────────────────────────────────────────────────
221
336
 
222
337
  app.get('/', c => {
223
- const rules = rulesOf(c);
224
- const stored = [...cms.types.values()].filter(t => !t.service && can(rules, t.id, 'read')).map(t => ({ id: t.id, name: t.name }));
225
- const outside = [...externals.values()].filter(x => allowed(x, principalOf(c))).map(x => ({ id: x.id, name: x.name }));
226
- return c.html(typeListPage(shell, stored, outside));
338
+ const item = (id: string) => {
339
+ const type = readable(c, id);
340
+ if (type) return { href: `${mount}/t/${type.id}`, name: type.name };
341
+ const collection = external(c, id);
342
+ return collection ? { href: `${mount}/x/${collection.id}`, name: collection.name } : null;
343
+ };
344
+ const sections: HomeSection[] = options.sections
345
+ ? options.sections.map(s => ({ title: s.title, line: s.line, items: s.items.map(item).filter(i => i !== null) }))
346
+ : [
347
+ { title: 'Types', items: [...cms.types.values()].map(t => item(t.id)).filter(i => i !== null) },
348
+ { title: 'Tables', items: [...externals.keys()].map(item).filter(i => i !== null) }
349
+ ];
350
+ return c.html(homePage(shell, sections));
227
351
  });
228
352
 
229
353
  // ── Entry lists ───────────────────────────────────────────────────
@@ -234,12 +358,14 @@ export function createAdmin<H extends HostPrincipal = never>(options: AdminOptio
234
358
  const q = (c.req.query('q') ?? '').trim();
235
359
  const statusFilter = c.req.query('status') === 'published' || c.req.query('status') === 'draft' ? c.req.query('status')! : '';
236
360
  const owner = (c.req.query('owner') ?? '').trim();
361
+ const parent = type.parent ? (c.req.query('parent') ?? '').trim() : '';
237
362
  const offset = Math.max(Math.trunc(Number(c.req.query('offset') ?? 0)) || 0, 0);
238
363
  const conditions: Where[] = [];
239
364
  // The store's conditions are equality, so a slug search is exact.
240
365
  if (q) conditions.push({ slug: q });
241
366
  if (statusFilter) conditions.push({ status: statusFilter });
242
367
  if (owner && type.owned) conditions.push({ owner_id: owner });
368
+ if (parent) conditions.push({ parent_id: parent });
243
369
  const found = await apiOf(c).list(type.id, {
244
370
  ...(conditions.length === 0 ? {} : { where: conditions.length === 1 ? conditions[0]! : { and: conditions } }),
245
371
  orderBy: 'updated_at',
@@ -247,10 +373,22 @@ export function createAdmin<H extends HostPrincipal = never>(options: AdminOptio
247
373
  limit: PAGE + 1,
248
374
  offset
249
375
  });
376
+ const entries = found.slice(0, PAGE);
377
+ const columns = typeColumns.get(type.id);
378
+ const refs = columns
379
+ ? await resolver(
380
+ c,
381
+ Object.fromEntries(columns.map(col => [col.name, col.meta])),
382
+ entries.map(e => ({ ...e.data, owner_id: e.owner_id, parent_id: e.parent_id }))
383
+ )
384
+ : await resolver(c, {}, []);
250
385
  return c.html(
251
386
  entryListPage(shell, {
252
387
  type,
253
- entries: found.slice(0, PAGE),
388
+ entries,
389
+ columns,
390
+ refs,
391
+ parent,
254
392
  q,
255
393
  status: statusFilter,
256
394
  owner: type.owned ? owner : '',
@@ -301,7 +439,8 @@ export function createAdmin<H extends HostPrincipal = never>(options: AdminOptio
301
439
  rows.push({ name, meta, value: shown(name), editable, error: state.fieldErrors?.[name] });
302
440
  }
303
441
  const data = Object.fromEntries(Object.keys(type.fieldMeta).map(name => [name, shown(name)]));
304
- const refs = await resolver(c, type.fieldMeta, data);
442
+ const refs = await resolver(c, type.fieldMeta, [data]);
443
+ const lists = entry ? [...(await childLists(c, type, entry)), ...(await relatedLists(c, typeRelated[type.id] ?? [], entry.id, entry))] : [];
305
444
  const actions = entry
306
445
  ? (typeActions[type.id] ?? []).filter(a => (a.requires ? can(rules, type.id, a.requires, row) : true))
307
446
  : [];
@@ -312,11 +451,12 @@ export function createAdmin<H extends HostPrincipal = never>(options: AdminOptio
312
451
  rows,
313
452
  refs,
314
453
  canSave: columns.some(col => col.editable) || rows.some(r => r.editable),
315
- canPublish: !!entry && can(rules, type.id, 'publish', row),
316
- // A type's own `unpublish` and `delete` actions take the place of the plain controls.
317
- canUnpublish: !!entry && can(rules, type.id, 'publish', row) && !(typeActions[type.id] ?? []).some(a => a.id === 'unpublish'),
318
- canDelete: !!entry && can(rules, type.id, 'delete', row) && !(typeActions[type.id] ?? []).some(a => a.id === 'delete'),
454
+ // A type's own `publish`, `unpublish` and `delete` actions take the place of the plain controls.
455
+ canPublish: !!entry && can(rules, type.id, 'publish', row) && !replaced(type.id, 'publish'),
456
+ canUnpublish: !!entry && can(rules, type.id, 'publish', row) && !replaced(type.id, 'unpublish'),
457
+ canDelete: !!entry && can(rules, type.id, 'delete', row) && !replaced(type.id, 'delete'),
319
458
  actions,
459
+ belongings: lists,
320
460
  notice: notice(c),
321
461
  problems: state.problems
322
462
  });
@@ -407,7 +547,8 @@ export function createAdmin<H extends HostPrincipal = never>(options: AdminOptio
407
547
  for (const verb of ['publish', 'unpublish'] as const) {
408
548
  app.post(`/t/:type/:id/${verb}`, async c => {
409
549
  const { type, entry } = await entryOf(c);
410
- if (!type || !entry) return notFound(c);
550
+ // A replaced control is refused at its route, the same answer as a control the caller lacks.
551
+ if (!type || !entry || replaced(type.id, verb)) return notFound(c);
411
552
  await apiOf(c)[verb](type.id, entry.id);
412
553
  return c.redirect(`${mount}/t/${type.id}/${encodeURIComponent(entry.id)}?notice=${verb === 'publish' ? 'published' : 'unpublished'}`, 303);
413
554
  });
@@ -415,7 +556,7 @@ export function createAdmin<H extends HostPrincipal = never>(options: AdminOptio
415
556
 
416
557
  app.get('/t/:type/:id/delete', async c => {
417
558
  const { type, entry } = await entryOf(c);
418
- if (!type || !entry) return notFound(c);
559
+ if (!type || !entry || replaced(type.id, 'delete')) return notFound(c);
419
560
  if (!can(rulesOf(c), type.id, 'delete', ruleRow(type, entry))) return notFound(c);
420
561
  const name = label(type, entry);
421
562
  return c.html(
@@ -433,7 +574,7 @@ export function createAdmin<H extends HostPrincipal = never>(options: AdminOptio
433
574
 
434
575
  app.post('/t/:type/:id/delete', async c => {
435
576
  const { type, entry } = await entryOf(c);
436
- if (!type || !entry) return notFound(c);
577
+ if (!type || !entry || replaced(type.id, 'delete')) return notFound(c);
437
578
  await apiOf(c).remove(type.id, entry.id);
438
579
  return c.redirect(`${mount}/t/${type.id}?notice=deleted`, 303);
439
580
  });
@@ -571,15 +712,19 @@ export function createAdmin<H extends HostPrincipal = never>(options: AdminOptio
571
712
  if (!collection) return notFound(c);
572
713
  const offset = Math.max(Math.trunc(Number(c.req.query('offset') ?? 0)) || 0, 0);
573
714
  const rows = await collection.list(c, { limit: PAGE + 1, offset });
574
- return c.html(externalListPage(shell, collection, rows.slice(0, PAGE), offset, PAGE, rows.length > PAGE));
715
+ const shown = rows.slice(0, PAGE);
716
+ const columns = tableColumns.get(collection.id);
717
+ const refs = columns ? await resolver(c, Object.fromEntries(columns.map(col => [col.name, col.meta])), shown) : undefined;
718
+ return c.html(externalListPage(shell, collection, shown, offset, PAGE, rows.length > PAGE, columns, refs));
575
719
  });
576
720
 
577
721
  app.get('/x/:collection/:id', async c => {
578
722
  const collection = external(c, c.req.param('collection'));
579
723
  const row: ExternalRow | null | undefined = await collection?.get(c, c.req.param('id'));
580
724
  if (!collection || !row) return notFound(c);
581
- const refs = await resolver(c, Object.fromEntries(Object.entries(collection.fields).map(([n, b]) => [n, b.meta])), row);
582
- return c.html(externalRowPage(shell, collection, row, refs, collection.actions ?? [], notice(c)));
725
+ const refs = await resolver(c, Object.fromEntries(Object.entries(collection.fields).map(([n, b]) => [n, b.meta])), [row]);
726
+ const related = await relatedLists(c, collection.related ?? [], row.id, row);
727
+ return c.html(externalRowPage(shell, collection, row, refs, collection.actions ?? [], notice(c), related));
583
728
  });
584
729
 
585
730
  app.get('/x/:collection/:id/do/:action', async c => {
@@ -101,11 +101,14 @@ const date: Renderer = {
101
101
  read: ({ value }) => (present(value) ? html`<time datetime="${asText(value)}">${asText(value)}</time>` : MUTED)
102
102
  };
103
103
 
104
+ /** What a select shows for a stored value: its option's label, or the value itself. */
105
+ export const optionLabel = (meta: FieldMeta, value: string): string => meta.optionLabels?.[value] ?? value;
106
+
104
107
  const select: Renderer = {
105
108
  edit: ({ name, meta, value }) => html`<select id="${idOf(name)}" name="${name}">${
106
109
  meta.required && present(value) ? '' : html`<option value="">—</option>`
107
- }${(meta.options ?? []).map(option => html`<option value="${option}"${value === option ? ' selected' : ''}>${option}</option>`)}</select>`,
108
- read: ({ value }) => (present(value) ? html`<span>${asText(value)}</span>` : MUTED)
110
+ }${(meta.options ?? []).map(option => html`<option value="${option}"${value === option ? ' selected' : ''}>${optionLabel(meta, option)}</option>`)}</select>`,
111
+ read: ({ meta, value }) => (present(value) ? html`<span>${optionLabel(meta, asText(value))}</span>` : MUTED)
109
112
  };
110
113
 
111
114
  function refLink(meta: FieldMeta, id: string, refs: ResolveRef): Html {
@@ -9,8 +9,9 @@
9
9
  */
10
10
  export { createAdmin, mountAdmin, DEFAULT_ASSETS } from './app.ts';
11
11
  export { ADMIN_CSS, ADMIN_JS } from './assets.ts';
12
- export { KIND, RENDERERS, renderField } from './fields.ts';
12
+ export { KIND, RENDERERS, optionLabel, renderField } from './fields.ts';
13
13
  export type { Html, Kind, RefTarget, RenderInput, Renderer, ResolveRef } from './fields.ts';
14
14
  export type {
15
- ActionContext, AdminAction, AdminAssets, AdminSession, AdminOptions, ExternalCollection, ExternalRow
15
+ ActionContext, AdminAction, AdminAssets, AdminRelated, AdminSection, AdminSession, AdminOptions, ExternalCollection, ExternalRow,
16
+ RelatedContext
16
17
  } from './types.ts';
@@ -1,6 +1,6 @@
1
1
  /** What a host gives the admin, and what it may add: external collections and actions. */
2
2
  import type { Context } from 'hono';
3
- import type { Cms, FieldBuilder, HostPrincipal, Operation, Principal } from '../core/index.ts';
3
+ import type { Cms, Entry, FieldBuilder, HostPrincipal, Operation, Principal } from '../core/index.ts';
4
4
 
5
5
  /** What an action's `run` is told. */
6
6
  export interface ActionContext<H extends HostPrincipal = never> {
@@ -49,13 +49,48 @@ export interface ExternalCollection<H extends HostPrincipal = never> {
49
49
  fields: Record<string, FieldBuilder<any, any>>;
50
50
  list(c: Context, page: { limit: number; offset: number }): Promise<ExternalRow[]>;
51
51
  get(c: Context, id: string): Promise<ExternalRow | null>;
52
+ /** Several rows by id in one read, for the names a list's references show; `get` for each id when absent. */
53
+ getMany?(c: Context, ids: string[]): Promise<ExternalRow[]>;
52
54
  actions?: AdminAction<H>[];
55
+ /** The fields its list shows after the display field, in order; the display field and State when absent. */
56
+ columns?: string[];
57
+ /** Lists of rows that belong to a row of this collection, on the row's page. */
58
+ related?: AdminRelated<H>[];
53
59
  /** Who may see it: only the admin kind when absent. */
54
60
  allow?(principal: Principal<H>): boolean;
55
61
  /** Whether a row is offered when this collection is an action's target; every row but the one acted on when absent. */
56
62
  pickable?(row: ExternalRow): boolean;
57
63
  }
58
64
 
65
+ /** What a related list's `rows` is told: the entry or row whose page is being drawn. */
66
+ export interface RelatedContext<H extends HostPrincipal = never> {
67
+ c: Context;
68
+ principal: Principal<H>;
69
+ id: string;
70
+ /** The entry (a store type's page) or the row (an external collection's page). */
71
+ entry: Entry | ExternalRow;
72
+ }
73
+
74
+ /**
75
+ * A read-only list on an entry's or row's page of rows that belong to it but are not its children: the
76
+ * revisions of a document, an author's posts. `table` is the store type or external collection the rows are of, and each row
77
+ * links to its page there.
78
+ */
79
+ export interface AdminRelated<H extends HostPrincipal = never> {
80
+ table: string;
81
+ label: string;
82
+ /** The rows, or the first rows and how many there are in all. At most 50 are shown. */
83
+ rows(context: RelatedContext<H>): Promise<ExternalRow[] | { rows: ExternalRow[]; total: number }>;
84
+ }
85
+
86
+ /** One heading of the home page: what the items are for, and the type or table ids under it, in order. */
87
+ export interface AdminSection {
88
+ title: string;
89
+ /** One line saying what the section is for. */
90
+ line?: string;
91
+ items: string[];
92
+ }
93
+
59
94
  /** Where the admin's pages find the host's static files. Each is a URL the host serves. */
60
95
  export interface AdminAssets {
61
96
  largen: string;
@@ -88,6 +123,22 @@ export interface AdminOptions<H extends HostPrincipal = never> {
88
123
  * page's own Unpublish or Delete, for a type whose host does more than the store does.
89
124
  */
90
125
  typeActions?: Record<string, AdminAction<H>[]>;
126
+ /**
127
+ * The fields a store type's list shows after the display field, in order, by type id. A field, or a built-in
128
+ * column (`slug`, `status`, `owner_id`, `parent_id`, `created_at`, `updated_at`). A type with none shows the
129
+ * display field, slug, status, owner and when it was updated.
130
+ */
131
+ typeColumns?: Record<string, string[]>;
132
+ /** Lists of rows that belong to a store type's entry and are not its children, on the entry's page, by type id. */
133
+ typeRelated?: Record<string, AdminRelated<H>[]>;
134
+ /** An external collection whose rows are the owners `owner_id` names; owners are then shown by name and linked. */
135
+ ownerTable?: string;
136
+ /**
137
+ * The home page, in sections. An item the caller may not read is left out, and a section left with none is not
138
+ * drawn. A type or table in no section keeps its pages. Absent, the home page lists every type the caller may
139
+ * read and every table.
140
+ */
141
+ sections?: AdminSection[];
91
142
  /** The path the host mounts the admin at, with no trailing slash; every link starts with it. */
92
143
  mount: string;
93
144
  siteName?: string;