cogens 0.2.0 → 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/CHANGELOG.md +31 -0
- package/README.md +35 -7
- package/dist/admin/app.d.ts +21 -0
- package/dist/admin/app.d.ts.map +1 -0
- package/dist/admin/app.js +702 -0
- package/dist/admin/app.js.map +1 -0
- package/dist/admin/assets.d.ts +17 -0
- package/dist/admin/assets.d.ts.map +1 -0
- package/dist/admin/assets.js +51 -0
- package/dist/admin/assets.js.map +1 -0
- package/dist/admin/fields.d.ts +29 -0
- package/dist/admin/fields.d.ts.map +1 -0
- package/dist/admin/fields.js +130 -0
- package/dist/admin/fields.js.map +1 -0
- package/dist/admin/forms.d.ts +18 -0
- package/dist/admin/forms.d.ts.map +1 -0
- package/dist/admin/forms.js +53 -0
- package/dist/admin/forms.js.map +1 -0
- package/dist/admin/index.d.ts +15 -0
- package/dist/admin/index.d.ts.map +1 -0
- package/dist/admin/index.js +13 -0
- package/dist/admin/index.js.map +1 -0
- package/dist/admin/rules.d.ts +24 -0
- package/dist/admin/rules.d.ts.map +1 -0
- package/dist/admin/rules.js +51 -0
- package/dist/admin/rules.js.map +1 -0
- package/dist/admin/types.d.ts +147 -0
- package/dist/admin/types.d.ts.map +1 -0
- package/dist/admin/types.js +2 -0
- package/dist/admin/types.js.map +1 -0
- package/dist/admin/views.d.ts +138 -0
- package/dist/admin/views.d.ts.map +1 -0
- package/dist/admin/views.js +225 -0
- package/dist/admin/views.js.map +1 -0
- package/dist/auth/handoff.d.ts +27 -0
- package/dist/auth/handoff.d.ts.map +1 -0
- package/dist/auth/handoff.js +41 -0
- package/dist/auth/handoff.js.map +1 -0
- package/dist/auth/index.d.ts +10 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +10 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/password.d.ts +48 -0
- package/dist/auth/password.d.ts.map +1 -0
- package/dist/auth/password.js +154 -0
- package/dist/auth/password.js.map +1 -0
- package/dist/auth/principal.d.ts +30 -0
- package/dist/auth/principal.d.ts.map +1 -0
- package/dist/auth/principal.js +30 -0
- package/dist/auth/principal.js.map +1 -0
- package/dist/auth/session.d.ts +68 -0
- package/dist/auth/session.d.ts.map +1 -0
- package/dist/auth/session.js +92 -0
- package/dist/auth/session.js.map +1 -0
- package/dist/core/access/ability.d.ts +24 -0
- package/dist/core/access/ability.d.ts.map +1 -0
- package/dist/core/access/ability.js +64 -0
- package/dist/core/access/ability.js.map +1 -0
- package/dist/core/access/can.d.ts +41 -0
- package/dist/core/access/can.d.ts.map +1 -0
- package/dist/core/access/can.js +38 -0
- package/dist/core/access/can.js.map +1 -0
- package/dist/core/access/compile.d.ts +12 -0
- package/dist/core/access/compile.d.ts.map +1 -0
- package/dist/core/access/compile.js +53 -0
- package/dist/core/access/compile.js.map +1 -0
- package/dist/core/access/permits.d.ts +24 -0
- package/dist/core/access/permits.d.ts.map +1 -0
- package/dist/core/access/permits.js +29 -0
- package/dist/core/access/permits.js.map +1 -0
- package/dist/core/access/when.d.ts +6 -0
- package/dist/core/access/when.d.ts.map +1 -0
- package/dist/core/access/when.js +64 -0
- package/dist/core/access/when.js.map +1 -0
- package/dist/core/access/where.d.ts +25 -0
- package/dist/core/access/where.d.ts.map +1 -0
- package/dist/core/access/where.js +81 -0
- package/dist/core/access/where.js.map +1 -0
- package/dist/core/columns.d.ts +6 -0
- package/dist/core/columns.d.ts.map +1 -0
- package/dist/core/columns.js +6 -0
- package/dist/core/columns.js.map +1 -0
- package/dist/core/config/define.d.ts +101 -0
- package/dist/core/config/define.d.ts.map +1 -0
- package/dist/core/config/define.js +97 -0
- package/dist/core/config/define.js.map +1 -0
- package/dist/core/config/field.d.ts +87 -0
- package/dist/core/config/field.d.ts.map +1 -0
- package/dist/core/config/field.js +75 -0
- package/dist/core/config/field.js.map +1 -0
- package/dist/core/errors.d.ts +66 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +94 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/index.d.ts +31 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +20 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/media/index.d.ts +36 -0
- package/dist/core/media/index.d.ts.map +1 -0
- package/dist/core/media/index.js +99 -0
- package/dist/core/media/index.js.map +1 -0
- package/dist/core/principal.d.ts +28 -0
- package/dist/core/principal.d.ts.map +1 -0
- package/dist/core/principal.js +25 -0
- package/dist/core/principal.js.map +1 -0
- package/dist/core/store/assets.d.ts +3 -0
- package/dist/core/store/assets.d.ts.map +1 -0
- package/dist/core/store/assets.js +3 -0
- package/dist/core/store/assets.js.map +1 -0
- package/dist/core/store/cms.d.ts +53 -0
- package/dist/core/store/cms.d.ts.map +1 -0
- package/dist/core/store/cms.js +154 -0
- package/dist/core/store/cms.js.map +1 -0
- package/dist/core/store/content.d.ts +57 -0
- package/dist/core/store/content.d.ts.map +1 -0
- package/dist/core/store/content.js +455 -0
- package/dist/core/store/content.js.map +1 -0
- package/dist/core/store/sql.d.ts +88 -0
- package/dist/core/store/sql.d.ts.map +1 -0
- package/dist/core/store/sql.js +149 -0
- package/dist/core/store/sql.js.map +1 -0
- package/dist/core/store/types.d.ts +107 -0
- package/dist/core/store/types.d.ts.map +1 -0
- package/dist/core/store/types.js +2 -0
- package/dist/core/store/types.js.map +1 -0
- package/dist/core/testing/index.d.ts +27 -0
- package/dist/core/testing/index.d.ts.map +1 -0
- package/dist/core/testing/index.js +60 -0
- package/dist/core/testing/index.js.map +1 -0
- package/dist/migrations-tool/bin.d.ts +3 -0
- package/dist/migrations-tool/bin.d.ts.map +1 -0
- package/dist/migrations-tool/bin.js +40 -0
- package/dist/migrations-tool/bin.js.map +1 -0
- package/dist/migrations-tool/index.d.ts +16 -0
- package/dist/migrations-tool/index.d.ts.map +1 -0
- package/dist/migrations-tool/index.js +24 -0
- package/dist/migrations-tool/index.js.map +1 -0
- package/dist/testing/db.d.ts +16 -0
- package/dist/testing/db.d.ts.map +1 -0
- package/dist/testing/db.js +43 -0
- package/dist/testing/db.js.map +1 -0
- package/dist/testing/index.d.ts +10 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +8 -0
- package/dist/testing/index.js.map +1 -0
- package/package.json +33 -11
- package/src/admin/app.ts +181 -36
- package/src/admin/fields.ts +5 -2
- package/src/admin/index.ts +3 -2
- package/src/admin/types.ts +52 -1
- package/src/admin/views.ts +99 -21
- package/src/core/config/define.ts +1 -1
- package/src/core/config/field.ts +19 -2
- package/src/core/index.ts +1 -1
- package/src/core/store/cms.ts +1 -1
- package/src/migrations-tool/bin.ts +34 -0
- package/{scripts/print-migration.ts → src/migrations-tool/index.ts} +6 -19
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogens",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A light multi-tenant content store for Cloudflare Workers: every entry may have an owner and every query passes an access check. Forked from Koguma.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -25,36 +25,56 @@
|
|
|
25
25
|
"content-store"
|
|
26
26
|
],
|
|
27
27
|
"engines": {
|
|
28
|
+
"node": ">=20",
|
|
28
29
|
"bun": ">=1.4.4"
|
|
29
30
|
},
|
|
30
31
|
"packageManager": "bun@1.4.4",
|
|
31
32
|
"exports": {
|
|
32
|
-
".":
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"./
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./dist/core/index.d.ts",
|
|
35
|
+
"default": "./dist/core/index.js"
|
|
36
|
+
},
|
|
37
|
+
"./admin": {
|
|
38
|
+
"types": "./dist/admin/index.d.ts",
|
|
39
|
+
"default": "./dist/admin/index.js"
|
|
40
|
+
},
|
|
41
|
+
"./auth": {
|
|
42
|
+
"types": "./dist/auth/index.d.ts",
|
|
43
|
+
"default": "./dist/auth/index.js"
|
|
44
|
+
},
|
|
45
|
+
"./testing": {
|
|
46
|
+
"types": "./dist/testing/index.d.ts",
|
|
47
|
+
"default": "./dist/testing/index.js"
|
|
48
|
+
},
|
|
49
|
+
"./migrations-tool": {
|
|
50
|
+
"types": "./dist/migrations-tool/index.d.ts",
|
|
51
|
+
"default": "./dist/migrations-tool/index.js"
|
|
52
|
+
},
|
|
37
53
|
"./scripts/*": "./scripts/*",
|
|
38
54
|
"./package.json": "./package.json",
|
|
39
55
|
"./migrations/*": "./migrations/*"
|
|
40
56
|
},
|
|
41
57
|
"bin": {
|
|
42
|
-
"cogens-print-migration": "./
|
|
58
|
+
"cogens-print-migration": "./dist/migrations-tool/bin.js"
|
|
43
59
|
},
|
|
44
60
|
"files": [
|
|
61
|
+
"dist",
|
|
45
62
|
"src",
|
|
46
63
|
"migrations",
|
|
47
64
|
"scripts",
|
|
48
65
|
"editor",
|
|
49
66
|
"LICENSE",
|
|
50
67
|
"NOTICE",
|
|
51
|
-
"README.md"
|
|
68
|
+
"README.md",
|
|
69
|
+
"CHANGELOG.md"
|
|
52
70
|
],
|
|
53
71
|
"scripts": {
|
|
54
|
-
"
|
|
72
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
73
|
+
"test": "bun run build && bun test",
|
|
55
74
|
"typecheck": "tsc --noEmit -p tsconfig.json && tsc --noEmit -p tsconfig.scripts.json",
|
|
56
|
-
"print-migration": "bun run
|
|
75
|
+
"print-migration": "bun run src/migrations-tool/bin.ts",
|
|
57
76
|
"build:editor": "bun run scripts/build-admin-editor.ts",
|
|
77
|
+
"prepublishOnly": "bun run build && bun test",
|
|
58
78
|
"example:migrate": "wrangler d1 migrations apply DB --local -c examples/worker/wrangler.jsonc",
|
|
59
79
|
"example:dev": "wrangler dev -c examples/worker/wrangler.jsonc"
|
|
60
80
|
},
|
|
@@ -84,5 +104,7 @@
|
|
|
84
104
|
"remark-directive": "^4.0.0",
|
|
85
105
|
"typescript": "^5.7.0",
|
|
86
106
|
"wrangler": "^4.71.0"
|
|
87
|
-
}
|
|
107
|
+
},
|
|
108
|
+
"main": "./dist/core/index.js",
|
|
109
|
+
"types": "./dist/core/index.d.ts"
|
|
88
110
|
}
|
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,
|
|
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
|
-
/**
|
|
189
|
-
|
|
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
|
|
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
|
|
202
|
-
if (
|
|
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 (
|
|
207
|
-
|
|
208
|
-
|
|
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
|
-
|
|
216
|
-
|
|
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
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
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
|
|
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
|
-
|
|
316
|
-
|
|
317
|
-
canUnpublish: !!entry && can(rules, type.id, 'publish', row) && !(
|
|
318
|
-
canDelete: !!entry && can(rules, type.id, 'delete', row) && !(
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 => {
|
package/src/admin/fields.ts
CHANGED
|
@@ -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 {
|
package/src/admin/index.ts
CHANGED
|
@@ -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';
|
package/src/admin/types.ts
CHANGED
|
@@ -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;
|