@blueshed/joined 0.2.2 → 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/.claude/skills/joined/SKILL.md +9 -1
- package/.claude/skills/joined/reference.md +267 -53
- package/.claude/skills/joined-door/SKILL.md +57 -0
- package/README.md +19 -50
- package/docs/design.md +76 -10
- package/docs/introduction.md +18 -18
- package/docs/light-and-shadow.svg +1 -1
- package/examples/README.md +20 -0
- package/examples/account/account.json +1 -0
- package/examples/account/derive.ts +41 -0
- package/examples/account/generated/elements.ts +52 -0
- package/examples/account/generated/rows.ts +22 -0
- package/examples/account/generated/rules.ts +32 -0
- package/examples/account/routes.ts +5 -0
- package/examples/app.css +2 -1
- package/examples/cafe/books.html +2 -2
- package/examples/cafe/cafe.css +9 -16
- package/examples/cafe/generated/books.rows.ts +22 -0
- package/examples/cafe/generated/books.rules.ts +3 -2
- package/examples/cafe/generated/kitchen.rows.ts +27 -0
- package/examples/cafe/generated/kitchen.rules.ts +11 -1
- package/examples/cafe/generated/menu.rows.ts +22 -0
- package/examples/cafe/generated/menu.rules.ts +3 -2
- package/examples/cafe/generated/order.rows.ts +27 -0
- package/examples/cafe/generated/order.rules.ts +11 -1
- package/examples/cafe/generated/staff.rows.ts +22 -0
- package/examples/cafe/generated/staff.rules.ts +6 -1
- package/examples/cafe/generated/supplies.queries.ts +4 -2
- package/examples/cafe/generated/supplies.rows.ts +22 -0
- package/examples/cafe/generated/supplies.rules.ts +12 -1
- package/examples/cafe/generated/till.queries.ts +2 -0
- package/examples/cafe/generated/till.rows.ts +27 -0
- package/examples/cafe/generated/till.rules.ts +11 -1
- package/examples/cafe/index.html +14 -15
- package/examples/cafe/kitchen.ts +1 -1
- package/examples/cafe/look.ts +30 -0
- package/examples/cafe/menu.html +10 -26
- package/examples/cafe/menu.ts +8 -80
- package/examples/cafe/order.html +6 -6
- package/examples/cafe/order.ts +5 -1
- package/examples/cafe/routes.ts +0 -1
- package/examples/cafe/stories.jsonl +3 -1
- package/examples/cafe/supplies.html +34 -34
- package/examples/cafe/supplies.ts +14 -11
- package/examples/cafe/till.ts +5 -1
- package/examples/chart/arches.ts +148 -0
- package/examples/chart/chart.css +6 -0
- package/examples/chart/chart.json +87 -0
- package/examples/chart/generated/elements.ts +105 -0
- package/examples/chart/generated/queries.ts +36 -0
- package/examples/chart/generated/rows.ts +27 -0
- package/examples/chart/generated/rules.ts +111 -0
- package/examples/chart/grid.ts +118 -0
- package/examples/chart/index.html +78 -0
- package/examples/chart/migrations/001-chart.ts +6 -0
- package/examples/chart/migrations/002-create-conditions.ts +6 -0
- package/examples/chart/page.ts +20 -0
- package/examples/chart/routes.ts +7 -0
- package/examples/chart/stories.jsonl +14 -0
- package/examples/chart/teeth.ts +57 -0
- package/examples/desk/generated/rows.ts +22 -0
- package/examples/desk/generated/rules.ts +34 -1
- package/examples/desk/index.html +9 -2
- package/examples/desk/routes.ts +0 -2
- package/examples/desk/stories.jsonl +3 -2
- package/examples/desk/thumbs.ts +2 -2
- package/examples/hello/generated/elements.ts +2 -2
- package/examples/hello/generated/rules.ts +3 -2
- package/examples/hello/hello.css +2 -4
- package/examples/hello/index.html +10 -3
- package/examples/hello/page.ts +4 -4
- package/examples/index.html +4 -5
- package/examples/notes/generated/elements.ts +112 -0
- package/examples/notes/generated/queries.ts +36 -0
- package/examples/notes/generated/rows.ts +27 -0
- package/examples/notes/generated/rules.ts +133 -0
- package/examples/notes/index.html +32 -0
- package/examples/notes/look.ts +41 -0
- package/examples/notes/migrations/001-notes.ts +7 -0
- package/examples/notes/notes.json +78 -0
- package/examples/notes/page.ts +21 -0
- package/examples/notes/routes.ts +6 -0
- package/examples/notes/stories.jsonl +21 -0
- package/examples/people/generated/rows.ts +22 -0
- package/examples/people/generated/rules.ts +11 -1
- package/examples/people/links.ts +2 -1
- package/examples/people/routes.ts +1 -3
- package/examples/people/stories.jsonl +1 -0
- package/examples/people/view.ts +5 -2
- package/examples/trip/generated/elements.ts +4 -2
- package/examples/trip/generated/queries.ts +12 -1
- package/examples/trip/generated/rows.ts +27 -0
- package/examples/trip/generated/rules.ts +56 -2
- package/examples/trip/index.html +58 -12
- package/examples/trip/line.css +186 -0
- package/examples/trip/line.ts +51 -40
- package/examples/trip/page.ts +65 -71
- package/examples/trip/routes.ts +9 -5
- package/examples/trip/stays.css +12 -12
- package/examples/trip/stays.ts +15 -10
- package/examples/trip/stories.jsonl +2 -0
- package/examples/trip/told.ts +3 -26
- package/examples/trip/trip.css +16 -0
- package/examples/trips/generated/rows.ts +22 -0
- package/examples/trips/generated/rules.ts +9 -1
- package/examples/trips/index.html +10 -10
- package/examples/trips/map.ts +3 -14
- package/examples/trips/new-trip.ts +14 -16
- package/examples/weeks/generated/elements.ts +1 -1
- package/examples/weeks/generated/queries.ts +11 -0
- package/examples/weeks/generated/rows.ts +22 -0
- package/examples/weeks/generated/rules.ts +11 -5
- package/examples/weeks/routes.ts +3 -2
- package/examples/weeks/stories.jsonl +1 -0
- package/examples/welcome.css +7 -5
- package/package.json +8 -2
- package/src/awesome.ts +103 -0
- package/src/board.css +75 -0
- package/src/board.ts +173 -0
- package/src/cli.ts +15 -2
- package/src/compile.ts +569 -35
- package/src/definitions.ts +53 -15
- package/src/draft.ts +38 -23
- package/src/drawer.ts +222 -0
- package/src/engine.ts +240 -166
- package/src/fields.ts +268 -0
- package/src/files.ts +6 -4
- package/src/ground.ts +110 -0
- package/src/icon-names.ts +15 -2
- package/src/kit.ts +51 -0
- package/src/link.ts +3 -3
- package/src/map.css +133 -0
- package/src/map.ts +200 -0
- package/src/mcp.ts +106 -22
- package/src/members/demo.ts +13 -12
- package/src/members/generated/rows.ts +22 -0
- package/src/members/generated/rules.ts +3 -2
- package/src/members/group.ts +65 -39
- package/src/members/keys.ts +93 -77
- package/src/members/panel.css +23 -17
- package/src/members/panel.ts +49 -35
- package/src/migrate.ts +120 -104
- package/src/month.css +125 -0
- package/src/month.ts +179 -0
- package/src/page.css +8 -0
- package/src/page.ts +7 -4
- package/src/parts.css +462 -170
- package/src/parts.ts +209 -26
- package/src/plane.css +56 -0
- package/src/plane.ts +151 -0
- package/src/postgres-server.ts +60 -0
- package/src/reading.css +77 -0
- package/src/reading.ts +238 -0
- package/src/rows.css +308 -0
- package/src/rows.ts +780 -0
- package/src/search.css +80 -0
- package/src/search.ts +130 -0
- package/src/server.ts +205 -114
- package/src/settings.ts +9 -4
- package/src/sheet.ts +114 -0
- package/src/skill.ts +40 -0
- package/src/slots.css +94 -0
- package/src/slots.ts +213 -0
- package/src/specs.ts +38 -0
- package/src/starter.ts +5 -2
- package/src/store.ts +184 -143
- package/src/stories.ts +43 -11
- package/src/surface.ts +159 -0
- package/src/timeline.css +111 -0
- package/src/timeline.ts +226 -0
- package/src/toast.ts +21 -48
- package/src/together.ts +0 -7
- package/src/tokens.css +78 -1
- package/src/tokens.json +249 -0
- package/src/tool.ts +211 -0
- package/src/upkeep.ts +18 -20
- package/src/views.css +44 -0
- package/src/views.ts +94 -0
- package/src/week.css +151 -0
- package/src/week.ts +250 -0
- package/src/write.ts +202 -69
- package/starter/CLAUDE.md +3 -3
- package/starter/bunfig +4 -0
- package/starter/prettierignore +4 -0
- package/starter/tests/hello.test.ts +2 -0
- package/docs/learning.md +0 -76
- package/examples/cafe/trust.ts +0 -20
- package/examples/desk/trust.ts +0 -24
- package/examples/people/trust.ts +0 -14
- package/examples/trip/answer-rows.ts +0 -70
- package/examples/trip/bar.ts +0 -89
- package/examples/trip/calendar-map.css +0 -711
- package/examples/trip/calendar-map.ts +0 -249
- package/examples/trip/drags.ts +0 -143
- package/examples/trip/drawer.ts +0 -250
- package/examples/trip/event-form.ts +0 -200
- package/examples/trip/holding.ts +0 -61
- package/examples/trip/map.ts +0 -234
- package/examples/trip/month.ts +0 -87
- package/examples/trip/other-weeks.ts +0 -67
- package/examples/trip/peek.ts +0 -71
- package/examples/trip/people-chips.ts +0 -74
- package/examples/trip/shell.ts +0 -39
- package/examples/trip/trust.ts +0 -51
- package/examples/trip/week.ts +0 -237
- package/examples/trip/writes.ts +0 -91
- package/examples/trip/zones.ts +0 -16
- package/examples/weeks/derive.ts +0 -23
- package/src/scrim.ts +0 -81
package/src/compile.ts
CHANGED
|
@@ -18,11 +18,14 @@ import {
|
|
|
18
18
|
whoCode,
|
|
19
19
|
whoRules,
|
|
20
20
|
whoSql,
|
|
21
|
+
kindFrozen,
|
|
21
22
|
type Definitions,
|
|
23
|
+
type Frozen,
|
|
24
|
+
type Counted,
|
|
22
25
|
type Query,
|
|
23
26
|
type Where,
|
|
24
27
|
} from "./definitions.ts";
|
|
25
|
-
import { refuseSql, triggerSql, type Dialect, type Trigger } from "./engine.ts";
|
|
28
|
+
import { constraintSql, refuseSql, triggerSql, type Dialect, type Trigger } from "./engine.ts";
|
|
26
29
|
import { diff, fromFolder, stateOf, writeMigration, type Change, type Column, type Table } from "./migrate.ts";
|
|
27
30
|
import { docParts, parts, type Op, type Type } from "./ops.ts";
|
|
28
31
|
import { settingsCode, settingsSaid, settingsUnchecked } from "./settings.ts";
|
|
@@ -64,6 +67,9 @@ type Coll = {
|
|
|
64
67
|
own?: true;
|
|
65
68
|
/** The fields it takes from what a link points at, once, when it's added (definitions' copies). */
|
|
66
69
|
copies?: { from: string; fields: string[]; to: string; at: string };
|
|
70
|
+
/** Where no story adds one (the seed's items are all there are): the fields every seed item has, typed as there,
|
|
71
|
+
* as a field an add needs is; its tables are as they were, since the seed isn't what the store holds. */
|
|
72
|
+
seedHas?: string[];
|
|
67
73
|
};
|
|
68
74
|
/** A document's model: its name, and its collections. */
|
|
69
75
|
type Model = { doc: string; collections: Record<string, Coll> };
|
|
@@ -109,6 +115,9 @@ export const SAID: Record<Type, string> = {
|
|
|
109
115
|
ids: "a list of ids",
|
|
110
116
|
};
|
|
111
117
|
const STRINGS: Type[] = ["string", "date-time", "zone", "instant", "duration", "place", "file"];
|
|
118
|
+
/** The forms that have a view of their own, which a page pops (docs/design.md, "A peek"): a time its month, a place its
|
|
119
|
+
* map, a file its picture. */
|
|
120
|
+
const VIEWED: Type[] = ["date-time", "instant", "place", "file"];
|
|
112
121
|
|
|
113
122
|
/** Whether a value is an object of fields (an order's terms), not a list. */
|
|
114
123
|
const isObject = (v: unknown): v is Record<string, unknown> => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
@@ -205,7 +214,14 @@ export function filled(model: Model, defs: Definitions, from: string) {
|
|
|
205
214
|
* each item the write touched against the rest of its collection, why it doesn't hold or null. None, if they
|
|
206
215
|
* hold nothing. */
|
|
207
216
|
export function heldBy(collections: Record<string, Coll>, defs: Definitions, from: string) {
|
|
208
|
-
const
|
|
217
|
+
const together = (coll: string) => listsOf(defs.mine?.[coll]).filter((m) => typeof m !== "string");
|
|
218
|
+
const colls = [
|
|
219
|
+
...new Set([
|
|
220
|
+
...Object.keys(defs.unique ?? {}),
|
|
221
|
+
...Object.keys(defs.noOverlap ?? {}),
|
|
222
|
+
...Object.keys(defs.mine ?? {}).filter((c) => together(c).length),
|
|
223
|
+
]),
|
|
224
|
+
];
|
|
209
225
|
if (!colls.length) return "";
|
|
210
226
|
const field = (coll: string, f: string, what: string) => {
|
|
211
227
|
const t = collections[coll]?.fields[f];
|
|
@@ -232,6 +248,15 @@ export function heldBy(collections: Record<string, Coll>, defs: Definitions, fro
|
|
|
232
248
|
` }`,
|
|
233
249
|
);
|
|
234
250
|
}
|
|
251
|
+
// lists each member's own, together: nobody in two of them
|
|
252
|
+
for (const fs of together(coll))
|
|
253
|
+
tests.push(
|
|
254
|
+
` {`,
|
|
255
|
+
` const lists = ${JSON.stringify(fs)}.map((f) => (a[f] ?? []) as number[]);`,
|
|
256
|
+
` if (lists.some((l, n) => l.some((m) => lists.slice(n + 1).some((o) => o.includes(m)))))`,
|
|
257
|
+
` return ${JSON.stringify(`${coll} `)} + a.id + ${JSON.stringify(` has someone in two of ${fs.join(" and ")}: take one back first`)};`,
|
|
258
|
+
` }`,
|
|
259
|
+
);
|
|
235
260
|
const o = defs.noOverlap?.[coll];
|
|
236
261
|
if (o) {
|
|
237
262
|
const [s, e] = o.range,
|
|
@@ -287,6 +312,111 @@ ${
|
|
|
287
312
|
`;
|
|
288
313
|
}
|
|
289
314
|
|
|
315
|
+
/** An item's frozen, by collection (definitions' frozen, an item's own, not a kind's). */
|
|
316
|
+
export const itemsFrozen = (defs: Definitions): Record<string, Frozen> =>
|
|
317
|
+
defs.frozen && !kindFrozen(defs.frozen) ? (defs.frozen as Record<string, Frozen>) : {};
|
|
318
|
+
|
|
319
|
+
/** The lists held as a write leaves them (definitions' within, then apart), by collection: each a list of ids, as
|
|
320
|
+
* the item's other list (`of`) is. */
|
|
321
|
+
export function listRules(defs: Definitions, collections: Record<string, Coll>, from: string) {
|
|
322
|
+
const out: Record<string, { lists: string[]; of?: string; refused: string }[]> = {};
|
|
323
|
+
for (const [key, all] of [
|
|
324
|
+
["within", defs.within],
|
|
325
|
+
["apart", defs.apart],
|
|
326
|
+
] as const)
|
|
327
|
+
for (const [coll, rules] of Object.entries(all ?? {}))
|
|
328
|
+
for (const r of rules as { lists: string[]; of?: string; refused: string }[]) {
|
|
329
|
+
for (const l of [...r.lists, ...(r.of ? [r.of] : [])])
|
|
330
|
+
if (collections[coll]?.fields[l] !== "ids")
|
|
331
|
+
throw new Error(`${from}: ${key} names ${coll}' ${l}, which isn't a list of ids`);
|
|
332
|
+
if (typeof r.refused !== "string")
|
|
333
|
+
throw new Error(`${from}: ${key} says the words a write is refused in: refused`);
|
|
334
|
+
(out[coll] ??= []).push(r);
|
|
335
|
+
}
|
|
336
|
+
return out;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** generated/rules.ts's listsHold, of SQLite (definitions' within and apart): each item the write touched, as it leaves
|
|
340
|
+
* it, why its lists don't hold, or null. Postgres holds them by its triggers (takesSql). None, if there are none. */
|
|
341
|
+
export function listsHeldBy(collections: Record<string, Coll>, defs: Definitions, from: string) {
|
|
342
|
+
const all = Object.entries(listRules(defs, collections, from));
|
|
343
|
+
if (!all.length) return "";
|
|
344
|
+
return `
|
|
345
|
+
/** Why the lists of an item the write touched don't hold as it leaves them (its definitions' within and apart), or null:
|
|
346
|
+
* asked on SQLite, as Postgres's triggers ask it at the write's end. */
|
|
347
|
+
export function listsHold(d: Record<string, unknown>, touched: Record<string, number[]>): string | null {
|
|
348
|
+
${all
|
|
349
|
+
.map(
|
|
350
|
+
([coll, rules]) => ` for (const id of touched[${JSON.stringify(coll)}] ?? []) {
|
|
351
|
+
const a = ((d[${JSON.stringify(coll)}] ?? []) as any[]).find((i) => i.id === id);
|
|
352
|
+
if (!a) continue;
|
|
353
|
+
const ids = (l: string): number[] => a[l] ?? [];
|
|
354
|
+
${rules
|
|
355
|
+
.map(({ lists, of, refused }) =>
|
|
356
|
+
of
|
|
357
|
+
? ` if (!${JSON.stringify(lists)}.every((l) => ids(l).every((k) => ids(${JSON.stringify(of)}).includes(k)))) return ${JSON.stringify(refused)};`
|
|
358
|
+
: ` {
|
|
359
|
+
const given = ${JSON.stringify(lists)}.flatMap(ids);
|
|
360
|
+
if (new Set(given).size !== given.length) return ${JSON.stringify(refused)};
|
|
361
|
+
}`,
|
|
362
|
+
)
|
|
363
|
+
.join("\n")}
|
|
364
|
+
}
|
|
365
|
+
`,
|
|
366
|
+
)
|
|
367
|
+
.join("")} return null;
|
|
368
|
+
}
|
|
369
|
+
`;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** A collection's lists that are each member's own, as its mine says them: none, where its mine is of values. */
|
|
373
|
+
const listsOf = (mine: NonNullable<Definitions["mine"]>[string] | undefined) =>
|
|
374
|
+
Array.isArray(mine) ? mine.map((m) => (typeof m === "object" && !Array.isArray(m) ? Object.keys(m)[0]! : m)) : [];
|
|
375
|
+
/** The words each field that's each member's own is refused in, where its mine says them, by collection. */
|
|
376
|
+
const mineSaid = (defs: Definitions) =>
|
|
377
|
+
Object.fromEntries(
|
|
378
|
+
Object.entries(defs.mine ?? {}).flatMap(([coll, fs]) => {
|
|
379
|
+
const said = Array.isArray(fs) ? fs.filter((m) => typeof m === "object" && !Array.isArray(m)) : [];
|
|
380
|
+
return said.length ? [[coll, Object.assign({}, ...said) as Record<string, string>]] : [];
|
|
381
|
+
}),
|
|
382
|
+
);
|
|
383
|
+
|
|
384
|
+
/** The collections of values that are each member's own (definitions' mine, { of, by }), by collection: the link to
|
|
385
|
+
* the item each is of, and the link to its member. Both are one id, `by` a member's. None, if there are none. */
|
|
386
|
+
export function theirsOf(collections: Record<string, Coll>, defs: Definitions, from: string) {
|
|
387
|
+
const out = Object.entries(defs.mine ?? {}).filter(
|
|
388
|
+
(e): e is [string, { of: string; by: string }] => !Array.isArray(e[1]),
|
|
389
|
+
);
|
|
390
|
+
for (const [coll, { of, by }] of out) {
|
|
391
|
+
const one = (f: string) => {
|
|
392
|
+
const l = defs.links?.[f];
|
|
393
|
+
return collections[coll]?.fields[f] === "integer" && typeof l === "object" && !!l.one ? l.to : undefined;
|
|
394
|
+
};
|
|
395
|
+
if (!one(of)) throw new Error(`${from}: mine names ${coll}' ${of}, which isn't a link to one item`);
|
|
396
|
+
if (one(by) !== "members") throw new Error(`${from}: mine names ${coll}' ${by}, which isn't a link to one member`);
|
|
397
|
+
}
|
|
398
|
+
return out.length ? Object.fromEntries(out) : null;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/** The lists that are each member's own (definitions' mine), by collection: each a list of ids linking to the members,
|
|
402
|
+
* or a link to one member. None, if there are none. */
|
|
403
|
+
export function mineOf(collections: Record<string, Coll>, defs: Definitions, from: string) {
|
|
404
|
+
const out = Object.entries(defs.mine ?? {})
|
|
405
|
+
.filter(([, fs]) => Array.isArray(fs))
|
|
406
|
+
.map(([coll, fs]) => [coll, listsOf(fs).flat()] as const);
|
|
407
|
+
for (const [coll, fs] of out)
|
|
408
|
+
for (const f of fs)
|
|
409
|
+
if (
|
|
410
|
+
!(collections[coll]?.fields[f] === "ids" && (f === "members" || defs.links?.[f] === "members")) &&
|
|
411
|
+
!(
|
|
412
|
+
collections[coll]?.fields[f] === "integer" &&
|
|
413
|
+
((l) => typeof l === "object" && l.to === "members" && !!l.one)(defs.links?.[f])
|
|
414
|
+
)
|
|
415
|
+
)
|
|
416
|
+
throw new Error(`${from}: mine names ${coll}' ${f}, which isn't a list of members, nor a link to one`);
|
|
417
|
+
return out.length ? Object.fromEntries(out) : null;
|
|
418
|
+
}
|
|
419
|
+
|
|
290
420
|
/** A derived document's model (an app's derive.ts), from an example of it (<doc>.json): each list a collection,
|
|
291
421
|
* each field's type read off every item's value, a field needed if every item has it. It has no ops: nothing
|
|
292
422
|
* writes it, the server derives it. */
|
|
@@ -365,6 +495,65 @@ export function entities(
|
|
|
365
495
|
}
|
|
366
496
|
}
|
|
367
497
|
|
|
498
|
+
/** Every element's own attributes, which a field of an item can't be: its element would be styled, hidden or moved by
|
|
499
|
+
* the field's value (class, style, hidden, slot), or its id or tab order taken (id, tabindex). title is left a field:
|
|
500
|
+
* the tooltip it gives is what it says. */
|
|
501
|
+
const ELEMENTS_OWN = [
|
|
502
|
+
"accesskey",
|
|
503
|
+
"autocapitalize",
|
|
504
|
+
"autofocus",
|
|
505
|
+
"class",
|
|
506
|
+
"contenteditable",
|
|
507
|
+
"dir",
|
|
508
|
+
"draggable",
|
|
509
|
+
"enterkeyhint",
|
|
510
|
+
"exportparts",
|
|
511
|
+
"hidden",
|
|
512
|
+
"id",
|
|
513
|
+
"inert",
|
|
514
|
+
"inputmode",
|
|
515
|
+
"is",
|
|
516
|
+
"lang",
|
|
517
|
+
"nonce",
|
|
518
|
+
"part",
|
|
519
|
+
"popover",
|
|
520
|
+
"slot",
|
|
521
|
+
"spellcheck",
|
|
522
|
+
"style",
|
|
523
|
+
"tabindex",
|
|
524
|
+
"translate",
|
|
525
|
+
];
|
|
526
|
+
/** Why a field can't be named so, or null: it's its item element's attribute, so a-z, 0-9 and -; not key or may (an
|
|
527
|
+
* item's id, and what its person may do to it: the applier's own); nor an attribute every element has. */
|
|
528
|
+
export function unnamed(f: string): string | null {
|
|
529
|
+
const last = f.split(".").at(-1)!;
|
|
530
|
+
if (!/^[a-z][a-z0-9-]*$/.test(last) || f === "key" || f === "may")
|
|
531
|
+
return `${f} cannot be an attribute (a-z, 0-9 and -, and not key or may)`;
|
|
532
|
+
if (ELEMENTS_OWN.includes(last))
|
|
533
|
+
return `${f} cannot be a field: its item's element is given each field as an attribute, and ${last} is every HTML element's own (it would change how the element is drawn or found): name it otherwise (a boat's class, design)`;
|
|
534
|
+
return null;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/** Why a link's field isn't given as the link is, or null: every collection with the field gives it as one id (one) or
|
|
538
|
+
* a list of ids, since a link's name is its own wherever the app says it. Each collection is named as where says it
|
|
539
|
+
* (crossings, or race's crossings), so a field given otherwise is named beside one given as the link. */
|
|
540
|
+
export function linkGiven(
|
|
541
|
+
f: string,
|
|
542
|
+
l: NonNullable<Definitions["links"]>[string],
|
|
543
|
+
colls: [string, Coll][],
|
|
544
|
+
from: string,
|
|
545
|
+
): string | null {
|
|
546
|
+
const one = typeof l === "object" && !!l.one;
|
|
547
|
+
const as = one ? "one id" : "a list of ids";
|
|
548
|
+
const has = colls.filter(([, c]) => c.fields[f]);
|
|
549
|
+
const right = has.filter(([, c]) => c.fields[f] === (one ? "integer" : "ids"));
|
|
550
|
+
const wrong = has.find(([, c]) => c.fields[f] !== (one ? "integer" : "ids"));
|
|
551
|
+
if (!right.length) return `${from}: links names ${f}, but no story gives ${as} as ${f}`;
|
|
552
|
+
if (!wrong) return null;
|
|
553
|
+
const to = typeof l === "string" ? l : l.to;
|
|
554
|
+
return `${from}: ${f} is a link to ${to} (${as}), as ${right[0]![0]}' ${f} is, but ${wrong[0]}' ${f} is ${SAID[wrong[1].fields[f]!]}: a link's name is its own in every document of the app, so name one of them otherwise`;
|
|
555
|
+
}
|
|
556
|
+
|
|
368
557
|
/** A model with its seed read too: a field's type is what every value given it is, in a story or the seed. */
|
|
369
558
|
export function seeded(model: Model, seed: Record<string, unknown>, from: string) {
|
|
370
559
|
for (const [name, c] of Object.entries(model.collections))
|
|
@@ -377,7 +566,11 @@ export function seeded(model: Model, seed: Record<string, unknown>, from: string
|
|
|
377
566
|
),
|
|
378
567
|
);
|
|
379
568
|
for (const [f, v] of Object.entries(given))
|
|
380
|
-
if (f !== "id" && f !== model.doc && !(f in c.fields) && typeOf(v))
|
|
569
|
+
if (f !== "id" && f !== model.doc && !(f in c.fields) && typeOf(v)) {
|
|
570
|
+
const not = ELEMENTS_OWN.includes(f.split(".").at(-1)!) && unnamed(f);
|
|
571
|
+
if (not) throw new Error(`${from}: ${name} ${item.id}'s ${not}`);
|
|
572
|
+
c.fields[f] = typeOf(v)!;
|
|
573
|
+
}
|
|
381
574
|
for (const [f, t] of Object.entries(c.fields)) {
|
|
382
575
|
const v = given[f];
|
|
383
576
|
if (v === undefined) continue;
|
|
@@ -397,7 +590,7 @@ export const ownOf = (doc: Record<string, unknown>) => ({
|
|
|
397
590
|
});
|
|
398
591
|
|
|
399
592
|
/** Where a link of a model points: the collection it's to, the document holding that, and whether it's one id. */
|
|
400
|
-
export type LinkOf = (field: string) => { to: string; at: string; one: boolean; gone?: boolean };
|
|
593
|
+
export type LinkOf = (field: string) => { to: string; at: string; one: boolean; gone?: boolean; part?: boolean };
|
|
401
594
|
/** Each link of a model: to the collection it's named for, in the document that holds it, or to the one its
|
|
402
595
|
* definition names (one id, where it says so), its own document's or another's. */
|
|
403
596
|
export function linksOf(
|
|
@@ -414,10 +607,15 @@ export function linksOf(
|
|
|
414
607
|
one: typeof l === "object" && !!l.one,
|
|
415
608
|
// removed with what it links to (gone: "with"), not the removal refused
|
|
416
609
|
gone: typeof l === "object" && l.gone === "with",
|
|
610
|
+
// structure, not a reference: what the item is part of (part: true), as one that goes with it is
|
|
611
|
+
part: typeof l === "object" && (l.part === true || l.gone === "with"),
|
|
417
612
|
// asked only of a link: a field that isn't one has no document
|
|
418
613
|
get at() {
|
|
419
614
|
const at = l && collections[to] ? doc : (holders[to] ?? (collections[to] ? doc : undefined));
|
|
420
|
-
if (!at)
|
|
615
|
+
if (!at)
|
|
616
|
+
throw new Error(
|
|
617
|
+
`${from}: ${f} is a list of ids, but no other document has a collection named ${to}: name it for one, or say what it links to in a header line ({"links":{"${f}":"members"}})`,
|
|
618
|
+
);
|
|
421
619
|
return at;
|
|
422
620
|
},
|
|
423
621
|
};
|
|
@@ -822,6 +1020,83 @@ export function takesSql(
|
|
|
822
1020
|
}),
|
|
823
1021
|
);
|
|
824
1022
|
}
|
|
1023
|
+
const said = (w: string) => `'${w.replaceAll("'", "''")}'`;
|
|
1024
|
+
// an item added only in the write adding what a link of it points at (definitions' addedWith): that item noted as
|
|
1025
|
+
// added in the write (joined_touched, as it was before: none)
|
|
1026
|
+
for (const [coll, links] of Object.entries(defs.addedWith ?? {}))
|
|
1027
|
+
for (const [f, refused] of Object.entries(links)) {
|
|
1028
|
+
if (collections[coll]?.fields[f] !== "integer" || !link(f).one)
|
|
1029
|
+
throw new Error(`${from}: addedWith names ${coll}' ${f}, which isn't a link to one item`);
|
|
1030
|
+
const noted = `${d.dialect === "sqlite" ? "temp" : "pg_temp"}.joined_touched`;
|
|
1031
|
+
out.push(
|
|
1032
|
+
trigger({
|
|
1033
|
+
name: `joined_with_${coll}_${f}`,
|
|
1034
|
+
at: "BEFORE",
|
|
1035
|
+
event: "INSERT",
|
|
1036
|
+
table: coll,
|
|
1037
|
+
body: refuseSql(
|
|
1038
|
+
d.dialect,
|
|
1039
|
+
`NOT EXISTS (SELECT 1 FROM ${noted} t WHERE t.tbl = '${link(f).to}' AND t.id = NEW.${q(f)} AND t.before IS NULL)`,
|
|
1040
|
+
said(refused),
|
|
1041
|
+
),
|
|
1042
|
+
}),
|
|
1043
|
+
);
|
|
1044
|
+
}
|
|
1045
|
+
// an item kept as it is once it's settled (definitions' frozen, by collection): its fields changed by no write while
|
|
1046
|
+
// its own yes-or-no field is true
|
|
1047
|
+
for (const [coll, { by, fields, refused }] of Object.entries(itemsFrozen(defs))) {
|
|
1048
|
+
const c = collections[coll];
|
|
1049
|
+
if (c?.fields[by] !== "boolean")
|
|
1050
|
+
throw new Error(`${from}: frozen is by ${by}, which isn't a yes-or-no field of ${coll}`);
|
|
1051
|
+
const cols = Object.keys(c.fields).filter(
|
|
1052
|
+
(k) => c.fields[k] !== "ids" && (!fields || fields.some((x) => k === x || k.startsWith(`${x}.`))),
|
|
1053
|
+
);
|
|
1054
|
+
for (const x of fields ?? [])
|
|
1055
|
+
if (!cols.some((k) => k === x || k.startsWith(`${x}.`)))
|
|
1056
|
+
throw new Error(`${from}: frozen names ${coll}' ${x}, which isn't one of its fields`);
|
|
1057
|
+
out.push(
|
|
1058
|
+
trigger({
|
|
1059
|
+
name: `joined_frozen_${coll}`,
|
|
1060
|
+
at: "BEFORE",
|
|
1061
|
+
event: "UPDATE",
|
|
1062
|
+
table: coll,
|
|
1063
|
+
body: refuseSql(
|
|
1064
|
+
d.dialect,
|
|
1065
|
+
`OLD.${q(by)} = 1 AND (${cols.map((k) => `NEW.${q(k)} ${d.not} OLD.${q(k)}`).join(" OR ")})`,
|
|
1066
|
+
said(refused),
|
|
1067
|
+
),
|
|
1068
|
+
}),
|
|
1069
|
+
);
|
|
1070
|
+
}
|
|
1071
|
+
// lists held as the write leaves it (definitions' within and apart): on Postgres, a trigger at its end (SQLite's are
|
|
1072
|
+
// held by code, listsHold, as it has no trigger that waits)
|
|
1073
|
+
// (checked of both, written of Postgres)
|
|
1074
|
+
const lists = Object.entries(listRules(defs, collections, from));
|
|
1075
|
+
if (d.dialect === "postgres")
|
|
1076
|
+
for (const [coll, rules] of lists)
|
|
1077
|
+
rules.forEach(({ lists, of, refused }, n) => {
|
|
1078
|
+
const pairs = (l: string) => q(`${coll}_${l}`);
|
|
1079
|
+
const cond = of
|
|
1080
|
+
? lists
|
|
1081
|
+
.map(
|
|
1082
|
+
(l) =>
|
|
1083
|
+
`EXISTS (SELECT 1 FROM ${pairs(l)} l WHERE l.${q(coll)} = item AND NOT EXISTS (SELECT 1 FROM ${pairs(of)} p WHERE p.${q(coll)} = item AND p.${q(of)} = l.${q(l)}))`,
|
|
1084
|
+
)
|
|
1085
|
+
.join(" OR ")
|
|
1086
|
+
: `EXISTS (SELECT v FROM (${lists.map((l) => `SELECT ${q(l)} AS v FROM ${pairs(l)} WHERE ${q(coll)} = item`).join(" UNION ALL ")}) a GROUP BY v HAVING COUNT(*) > 1)`;
|
|
1087
|
+
out.push(
|
|
1088
|
+
...constraintSql({
|
|
1089
|
+
name: `joined_${of ? "within" : "apart"}_${coll}_${n}`,
|
|
1090
|
+
// what could leave it unheld: an id put in a list, or (within) taken from the list it must be in
|
|
1091
|
+
on: [
|
|
1092
|
+
...lists.map((l) => ({ table: `${coll}_${l}`, event: "INSERT" })),
|
|
1093
|
+
...(of ? [{ table: `${coll}_${of}`, event: "DELETE" }] : []),
|
|
1094
|
+
],
|
|
1095
|
+
item: q(coll),
|
|
1096
|
+
body: `IF ${cond} THEN RAISE EXCEPTION '%', ${said(refused)}; END IF;`,
|
|
1097
|
+
}),
|
|
1098
|
+
);
|
|
1099
|
+
});
|
|
825
1100
|
// a field kept as the sum of the rows linking to its item (definitions' totals): each change to them, summed again
|
|
826
1101
|
for (const [coll, fields] of Object.entries(defs.totals ?? {}))
|
|
827
1102
|
for (const [field, { of, by, sum, times }] of Object.entries(fields)) {
|
|
@@ -852,6 +1127,22 @@ const wordsOf = (refused: string) =>
|
|
|
852
1127
|
.join(".+")}$`,
|
|
853
1128
|
);
|
|
854
1129
|
|
|
1130
|
+
/** An app's definitions with what they imply said: of values each member's own (mine, { of, by }), whose one is the
|
|
1131
|
+
* server's to fill, with its writer, and a member has one for an item at most. */
|
|
1132
|
+
export function implied(d: Definitions): Definitions {
|
|
1133
|
+
const fills: Record<string, Record<string, unknown>> = { ...d.fills },
|
|
1134
|
+
unique: Record<string, (string | string[])[]> = { ...d.unique };
|
|
1135
|
+
const own = Object.entries(d.mine ?? {}).filter(
|
|
1136
|
+
(e): e is [string, { of: string; by: string }] => !Array.isArray(e[1]),
|
|
1137
|
+
);
|
|
1138
|
+
if (!own.length) return d;
|
|
1139
|
+
for (const [coll, { of, by }] of own) {
|
|
1140
|
+
fills[coll] = { ...fills[coll], [by]: "me" };
|
|
1141
|
+
unique[coll] = [...(unique[coll] ?? []), [of, by]];
|
|
1142
|
+
}
|
|
1143
|
+
return { ...d, fills, unique };
|
|
1144
|
+
}
|
|
1145
|
+
|
|
855
1146
|
/** Of an app's definitions, a document's: those of its collections and their fields; and the kind, its roles and
|
|
856
1147
|
* owns, to the document told on its instances (order:7), or to every one where there's no kind. */
|
|
857
1148
|
export function definedFor(d: Definitions, m: Model, stories: Story[]): Definitions {
|
|
@@ -865,6 +1156,7 @@ export function definedFor(d: Definitions, m: Model, stories: Story[]): Definiti
|
|
|
865
1156
|
"copies",
|
|
866
1157
|
"unique",
|
|
867
1158
|
"noOverlap",
|
|
1159
|
+
"mine",
|
|
868
1160
|
"window",
|
|
869
1161
|
"order",
|
|
870
1162
|
"fills",
|
|
@@ -875,13 +1167,20 @@ export function definedFor(d: Definitions, m: Model, stories: Story[]): Definiti
|
|
|
875
1167
|
"totals",
|
|
876
1168
|
"guests",
|
|
877
1169
|
"visitors",
|
|
1170
|
+
"addedWith",
|
|
1171
|
+
"within",
|
|
1172
|
+
"apart",
|
|
878
1173
|
] as const) {
|
|
879
1174
|
const mine = has(d[k] as Record<string, unknown> | undefined);
|
|
880
1175
|
if (mine && Object.keys(mine).length) out[k] = mine;
|
|
881
1176
|
}
|
|
882
1177
|
if (d.where?.[m.doc]) out.where = { [m.doc]: d.where[m.doc]! };
|
|
883
1178
|
const instance = stories.some((s) => (s.does ?? s.cannot)!.doc.includes(":"));
|
|
884
|
-
|
|
1179
|
+
// frozen: a kind's, as its roles are; an item's own, its collection's document's
|
|
1180
|
+
const kinds = ["kind", "roles", "waits", ...(kindFrozen(d.frozen) ? ["frozen" as const] : [])] as const;
|
|
1181
|
+
if (instance || !d.kind) for (const k of kinds) if (d[k]) out[k] = d[k] as never;
|
|
1182
|
+
const frozen = kindFrozen(d.frozen) ? undefined : has(d.frozen as Record<string, Frozen> | undefined);
|
|
1183
|
+
if (frozen && Object.keys(frozen).length) out.frozen = frozen as Record<string, Frozen>;
|
|
885
1184
|
// owns: one who-path, the kind's or every document's; or one a collection, its documents'
|
|
886
1185
|
if ((instance || !d.kind) && typeof d.owns === "string") out.owns = d.owns;
|
|
887
1186
|
const owned = typeof d.owns === "object" ? has(d.owns) : undefined;
|
|
@@ -894,6 +1193,7 @@ export function definedFor(d: Definitions, m: Model, stories: Story[]): Definiti
|
|
|
894
1193
|
export function queryOf({ doc }: Model, from: string, spec: Query) {
|
|
895
1194
|
const q = (name: string) => `"${name.replaceAll('"', '""')}"`;
|
|
896
1195
|
const colls = Object.entries(spec).map(([coll, s]) => {
|
|
1196
|
+
if ("count" in s) return counted(coll, s);
|
|
897
1197
|
const sides = Object.entries(s.sides);
|
|
898
1198
|
const side = `CASE ${sides.map(([f, w]) => `WHEN o.${q(f)} = $member THEN '${w}'`).join(" ")} END`;
|
|
899
1199
|
// the sum of the rows linking to it, or of its own fields
|
|
@@ -913,8 +1213,34 @@ export const query = \`${query(SQLITE)}\`;
|
|
|
913
1213
|
export const postgres = ${JSON.stringify({ query: query(POSTGRES) }, null, 2)};
|
|
914
1214
|
`;
|
|
915
1215
|
}
|
|
1216
|
+
/** A read-only document's collection counting rows by what they link to and their week (definitions' documents,
|
|
1217
|
+
* Counted): each item seen by the member, its rows by week, numbered in order. */
|
|
1218
|
+
function counted(coll: string, s: Counted) {
|
|
1219
|
+
const q = (name: string) => `"${name.replaceAll('"', '""')}"`;
|
|
1220
|
+
const [[by, to]] = Object.entries(s.by) as [[string, string]];
|
|
1221
|
+
const seen = s.seen
|
|
1222
|
+
.map((l) => `EXISTS (SELECT 1 FROM ${q(`${to}_${l}`)} p WHERE p.${q(to)} = o.${q(by)} AND p.${q(l)} = $member)`)
|
|
1223
|
+
.join(" OR ");
|
|
1224
|
+
return (d: Sql) => {
|
|
1225
|
+
// the Monday of the week a wall clock's day is in
|
|
1226
|
+
const week =
|
|
1227
|
+
d.dialect === "sqlite"
|
|
1228
|
+
? `date(o.${q(s.week)}, 'weekday 0', '-6 days')`
|
|
1229
|
+
: `to_char(date_trunc('week', CAST(o.${q(s.week)} AS timestamp)), 'YYYY-MM-DD')`;
|
|
1230
|
+
const rows = `SELECT ROW_NUMBER() OVER (ORDER BY o.${q(by)}, ${week}) AS id, o.${q(by)} AS item, ${week} AS week, COUNT(*) AS n FROM ${q(s.from)} o WHERE (${seen}) GROUP BY o.${q(by)}, ${week}`;
|
|
1231
|
+
return ` '${coll}', ${d.rows(d.object(`'id', w.id, '${by}', w.item, 'week', w.week, '${s.count}', w.n`), "w.id", `\n FROM (${rows}) w`)}`;
|
|
1232
|
+
};
|
|
1233
|
+
}
|
|
1234
|
+
|
|
916
1235
|
/** The tables a read-only document's query reads: what a change to them is told to. */
|
|
917
|
-
const readsOf = (spec: Query) =>
|
|
1236
|
+
const readsOf = (spec: Query) =>
|
|
1237
|
+
[
|
|
1238
|
+
...new Set(
|
|
1239
|
+
Object.values(spec).flatMap((s) =>
|
|
1240
|
+
"count" in s ? [s.from, ...s.seen.map((l) => `${Object.values(s.by)[0]}_${l}`)] : [s.from],
|
|
1241
|
+
),
|
|
1242
|
+
),
|
|
1243
|
+
].sort();
|
|
918
1244
|
|
|
919
1245
|
/** The tables a document's collections are, and their tables of pairs. */
|
|
920
1246
|
const tablesRead = (collections: Record<string, Coll>) =>
|
|
@@ -942,7 +1268,7 @@ const named = (changes: Change[]) =>
|
|
|
942
1268
|
.map((c) => [c.change.replace(/[A-Z].*/, ""), c.key, "column" in c ? c.column : ""].filter(Boolean).join("-"))
|
|
943
1269
|
.join("-and-") + (changes.length > 2 ? "-and-more" : "");
|
|
944
1270
|
|
|
945
|
-
/** What a story can't show, each a header line of an app's stories, after the cast (
|
|
1271
|
+
/** What a story can't show, each a header line of an app's stories, after the cast (the skill's §15). */
|
|
946
1272
|
const DEFINED = [
|
|
947
1273
|
"links",
|
|
948
1274
|
"roles",
|
|
@@ -952,6 +1278,7 @@ const DEFINED = [
|
|
|
952
1278
|
"timed",
|
|
953
1279
|
"unique",
|
|
954
1280
|
"noOverlap",
|
|
1281
|
+
"mine",
|
|
955
1282
|
"window",
|
|
956
1283
|
"order",
|
|
957
1284
|
"copies",
|
|
@@ -965,6 +1292,9 @@ const DEFINED = [
|
|
|
965
1292
|
"frozen",
|
|
966
1293
|
"waits",
|
|
967
1294
|
"visitors",
|
|
1295
|
+
"addedWith",
|
|
1296
|
+
"within",
|
|
1297
|
+
"apart",
|
|
968
1298
|
];
|
|
969
1299
|
|
|
970
1300
|
/** Why a definition is checked by no story, or null: each must be, or it's a guess. A kind, by a story on an
|
|
@@ -1003,6 +1333,10 @@ export function unchecked(
|
|
|
1003
1333
|
for (const f of Object.keys(defs.links ?? {}))
|
|
1004
1334
|
if (!filledMe(f) && !by.some((u) => u.stories.some((s) => ops(s).some((o) => gives(o, f)))))
|
|
1005
1335
|
return `links names ${f}, which no story writes`;
|
|
1336
|
+
// a link that's part of what it points at is one id: a thing is part of one thing
|
|
1337
|
+
for (const [f, l] of Object.entries(defs.links ?? {}))
|
|
1338
|
+
if (typeof l === "object" && l.part && !l.one)
|
|
1339
|
+
return `links' ${f} is part of what it points at, so it's one id (one: true)`;
|
|
1006
1340
|
// a link that goes with what it points at: a story removes one of those
|
|
1007
1341
|
for (const [f, l] of Object.entries(defs.links ?? {}))
|
|
1008
1342
|
if (typeof l === "object" && l.gone === "with") {
|
|
@@ -1043,6 +1377,15 @@ export function unchecked(
|
|
|
1043
1377
|
] as const)
|
|
1044
1378
|
for (const coll of Object.keys(colls ?? {}))
|
|
1045
1379
|
if (!on(coll)) return `${key} names ${coll}, but no cannot story writes it`;
|
|
1380
|
+
for (const [coll, fs] of Object.entries(defs.mine ?? {})) {
|
|
1381
|
+
for (const f of listsOf(fs).flat())
|
|
1382
|
+
if (!stories.some((s) => s.cannot?.ops.some((o) => parts(o.path).coll === coll && gives(o, f))))
|
|
1383
|
+
return `mine names ${coll}' ${f}, but no cannot story writes it`;
|
|
1384
|
+
// values each member's own: a cannot story changes or removes one (another's)
|
|
1385
|
+
const anothers = (o: Op) => parts(o.path).coll === coll && parts(o.path).key !== "-" && o.op !== "add";
|
|
1386
|
+
if (!Array.isArray(fs) && !stories.some((s) => s.cannot?.ops.some(anothers)))
|
|
1387
|
+
return `mine names ${coll}, but no cannot story changes another's`;
|
|
1388
|
+
}
|
|
1046
1389
|
for (const [coll, { from }] of Object.entries(defs.copies ?? {}))
|
|
1047
1390
|
if (!stories.some((s) => s.does?.ops.some((o) => parts(o.path).coll === coll && o.op === "add" && gives(o, from))))
|
|
1048
1391
|
return `copies names ${coll}' ${from}, but no story adds one by it`;
|
|
@@ -1061,6 +1404,16 @@ export function unchecked(
|
|
|
1061
1404
|
return `${rule} names ${coll}, but no cannot story is refused in its words`;
|
|
1062
1405
|
// a kind's settings, each by a cannot refused in its words; a guest's level: a guest's does on an item of one, and a
|
|
1063
1406
|
// guest's cannot on one of another
|
|
1407
|
+
// what holds an item or its lists, each by a cannot refused in its words
|
|
1408
|
+
const refusing = (words: string) => stories.some((s) => s.cannot && s.refused === words);
|
|
1409
|
+
for (const [key, all] of [
|
|
1410
|
+
["addedWith", Object.values(defs.addedWith ?? {}).flatMap((l) => Object.values(l))],
|
|
1411
|
+
["frozen", Object.values(itemsFrozen(defs)).map((x) => x.refused)],
|
|
1412
|
+
["within", Object.values(defs.within ?? {}).flatMap((rs) => rs.map((r) => r.refused))],
|
|
1413
|
+
["apart", Object.values(defs.apart ?? {}).flatMap((rs) => rs.map((r) => r.refused))],
|
|
1414
|
+
] as const)
|
|
1415
|
+
for (const words of all)
|
|
1416
|
+
if (!refusing(words)) return `${key} is checked by no story: none cannot, refused in "${words}"`;
|
|
1064
1417
|
return settingsUnchecked(defs, stories, grants) ?? visitorsRefused(defs, stories, rolesOfTeller);
|
|
1065
1418
|
}
|
|
1066
1419
|
|
|
@@ -1221,9 +1574,8 @@ export function modelOf(stories: Story[], links: Definitions["links"] = {}): Mod
|
|
|
1221
1574
|
`${where}: ${model.doc}'s own fields are kept as a collection named for it, so it has no collection called ${name}: name one otherwise`,
|
|
1222
1575
|
);
|
|
1223
1576
|
const typed = (f: string, v: unknown): void => {
|
|
1224
|
-
|
|
1225
|
-
if (
|
|
1226
|
-
throw new Error(`${where}: ${f} cannot be an attribute (a-z, 0-9 and -, and not key or may)`);
|
|
1577
|
+
const not = unnamed(f);
|
|
1578
|
+
if (not) throw new Error(`${where}: ${not}`);
|
|
1227
1579
|
// an object of fields: each of its fields a field of the item, named for both (terms.days), one value each
|
|
1228
1580
|
if (isObject(v)) {
|
|
1229
1581
|
if (f.includes(".")) throw new Error(`${where}: ${f} is an object in an object: one value each`);
|
|
@@ -1321,7 +1673,7 @@ export function compile(
|
|
|
1321
1673
|
fixed = false,
|
|
1322
1674
|
readOnly?: Query,
|
|
1323
1675
|
prefix = "",
|
|
1324
|
-
): { rules: string; elements: string } {
|
|
1676
|
+
): { rules: string; elements: string; rows: string | null } {
|
|
1325
1677
|
const HEAD = `// Written by src/compile.ts from ${from}: change ${derivedFrom ? "it and its example" : fixed ? "its seed" : "the stories"}, not this.\n`;
|
|
1326
1678
|
for (const name of Object.keys(collections)) {
|
|
1327
1679
|
if (OWN_TABLES.includes(name) || /^sqlite/.test(name))
|
|
@@ -1353,6 +1705,18 @@ export function compile(
|
|
|
1353
1705
|
.filter(([f, t]) => t === "ids" || (t === "integer" && link(f).one))
|
|
1354
1706
|
.map(([f]) => [f, link(f).to]),
|
|
1355
1707
|
),
|
|
1708
|
+
// what each field pops, tapped where a page says it (docs/design.md, "A peek"): a value with a form, that
|
|
1709
|
+
// form's view; a reference, the collection it points at. A plain value pops nothing, nor a link that is
|
|
1710
|
+
// structure (part), which a page places the item by
|
|
1711
|
+
pops: Object.fromEntries(
|
|
1712
|
+
Object.entries(c.fields).flatMap(([f, t]): [string, { form: Type } | { to: string }][] =>
|
|
1713
|
+
VIEWED.includes(t)
|
|
1714
|
+
? [[f, { form: t }]]
|
|
1715
|
+
: (t === "ids" || (t === "integer" && link(f).one)) && !link(f).part
|
|
1716
|
+
? [[f, { to: link(f).to }]]
|
|
1717
|
+
: [],
|
|
1718
|
+
),
|
|
1719
|
+
),
|
|
1356
1720
|
// its links that go with what they point at, in this document: the item removed with it
|
|
1357
1721
|
...goes(c, link, doc),
|
|
1358
1722
|
...(defs.order?.[name] ? { order: defs.order[name] } : {}),
|
|
@@ -1363,17 +1727,15 @@ export function compile(
|
|
|
1363
1727
|
]),
|
|
1364
1728
|
);
|
|
1365
1729
|
for (const [f, l] of Object.entries(defs.links ?? {})) {
|
|
1366
|
-
const
|
|
1367
|
-
|
|
1368
|
-
if (t !== (one ? "integer" : "ids"))
|
|
1369
|
-
throw new Error(`${from}: links names ${f}, but no story gives ${one ? "one id" : "a list of ids"} as ${f}`);
|
|
1730
|
+
const said = linkGiven(f, l, Object.entries(collections), from);
|
|
1731
|
+
if (said) throw new Error(said);
|
|
1370
1732
|
}
|
|
1371
1733
|
|
|
1372
1734
|
let rules = `${HEAD}
|
|
1373
1735
|
export type Op = { op: "add" | "replace" | "remove"; path: string; value?: any };
|
|
1374
1736
|
|
|
1375
1737
|
/** The document, and each collection: its element, the element its items are, their fields, those an add needs, and
|
|
1376
|
-
* what each link is to. */
|
|
1738
|
+
* what each link is to and what each field pops (a value with a form, its view; a reference, what it points at). */
|
|
1377
1739
|
export const doc = "${doc}";
|
|
1378
1740
|
export const collections = ${JSON.stringify(shape, null, 2)} as const;
|
|
1379
1741
|
/** Whether every story is anonymous, none told as a member: where every app's is, the project has no members. */
|
|
@@ -1437,21 +1799,23 @@ const send = (from: Element, ops: object[], why?: string) => new Promise<Answer>
|
|
|
1437
1799
|
|
|
1438
1800
|
// a link to one member is cleared as they're removed (its column set null): needed to add, yet, read, maybe gone
|
|
1439
1801
|
const cleared = (f: string) => link(f).one && link(f).to === GROUP;
|
|
1802
|
+
// what's always there as it's read: a field every add gives, or where nothing adds one, every seed item has
|
|
1803
|
+
const there = (f: string) => c.required.includes(f) || (!!c.seedHas?.includes(f) && !cleared(f));
|
|
1440
1804
|
const read = (f: string, t: Type) =>
|
|
1441
1805
|
t === "boolean"
|
|
1442
1806
|
? `this.hasAttribute("${f}")`
|
|
1443
1807
|
: t === "integer" || t === "decimal"
|
|
1444
|
-
?
|
|
1808
|
+
? there(f) && !cleared(f)
|
|
1445
1809
|
? `Number(this.getAttribute("${f}"))`
|
|
1446
1810
|
: `this.hasAttribute("${f}") ? Number(this.getAttribute("${f}")) : undefined`
|
|
1447
1811
|
: t === "ids"
|
|
1448
1812
|
? `Object.freeze((this.getAttribute("${f}") ?? "").split(" ").filter(Boolean).map(Number))`
|
|
1449
|
-
:
|
|
1813
|
+
: there(f)
|
|
1450
1814
|
? `this.getAttribute("${f}") ?? ""`
|
|
1451
1815
|
: `this.getAttribute("${f}") ?? undefined`;
|
|
1452
1816
|
// as read, a boolean or a list is always there: missing is false, or none
|
|
1453
1817
|
const always = fields
|
|
1454
|
-
.filter(([f, t]) => (t === "boolean" || t === "ids") && !
|
|
1818
|
+
.filter(([f, t]) => (t === "boolean" || t === "ids") && !there(f))
|
|
1455
1819
|
.map(([f]) => JSON.stringify(f));
|
|
1456
1820
|
const gone = fields.filter(([f]) => c.required.includes(f) && cleared(f)).map(([f]) => JSON.stringify(f));
|
|
1457
1821
|
const data = `${gone.length ? `Omit<${Class}Data, ${gone.join(" | ")}> & Partial<Pick<${Class}Data, ${gone.join(" | ")}>>` : `${Class}Data`}${always.length ? ` & Required<Pick<${Class}Data, ${always.join(" | ")}>>` : ""}`;
|
|
@@ -1470,7 +1834,7 @@ const send = (from: Element, ops: object[], why?: string) => new Promise<Answer>
|
|
|
1470
1834
|
.join("; ");
|
|
1471
1835
|
elements += `
|
|
1472
1836
|
/** ${an(item)[0]!.toUpperCase() + an(item).slice(1)}'s fields, as the stories give them. */
|
|
1473
|
-
export type ${Class}Data = { ${fields.map(([f, t]) => `${JSON.stringify(f)}${
|
|
1837
|
+
export type ${Class}Data = { ${fields.map(([f, t]) => `${JSON.stringify(f)}${there(f) ? "" : "?"}: ${tsOf(t)}`).join("; ")} };
|
|
1474
1838
|
/** An add of ${an(item)}, as it's sent: its fields as the stories write them. */
|
|
1475
1839
|
export type ${Class}Value = { ${value} };
|
|
1476
1840
|
|
|
@@ -1499,7 +1863,7 @@ export class ${Class} extends HTMLElement {
|
|
|
1499
1863
|
: `map(([field, value]) => (${one}))`;
|
|
1500
1864
|
elements += ` /** Replace some of its fields (${c.replace.join(", ")}): one write for all of them${byId.length ? `; a list of ids (${byId.join(", ")}) by the ids added to it and taken from it` : ""}. */\n replace(fields: Partial<Pick<${Class}Data, ${F}>>, why?: string) {\n return send(this, Object.entries(fields).filter(([, value]) => value !== undefined).${each}, why);\n }\n`;
|
|
1501
1865
|
if (byId.length)
|
|
1502
|
-
elements += ` /** A list's ids, as the ids added to it and taken from it. */\n #ids(field: ${byId.map((f) => JSON.stringify(f)).join(" | ")}, ids: number[]) {\n const had = this.data[field];\n return [...ids.filter((id) => !had.includes(id)).map((id) => ({ op: "add", path: \`\${this.path}/\${field}/-\`, value: id })), ...had.filter((id) => !ids.includes(id)).map((id) => ({ op: "remove", path: \`\${this.path}/\${field}/\${id}\` }))];\n }\n`;
|
|
1866
|
+
elements += ` /** A list's ids, as the ids added to it and taken from it; emptied, where this person may take the whole list and not\n * its ids one by one (an answer taken back), the list taken. */\n #ids(field: ${byId.map((f) => JSON.stringify(f)).join(" | ")}, ids: number[]) {\n const had = this.data[field], may = (this.getAttribute("may") ?? "").split(" ");\n if (!ids.length && had.length && may.includes(\`remove:\${field}\`) && !may.includes(\`remove:\${field}[]\`)) return [{ op: "remove", path: \`\${this.path}/\${field}\` }];\n return [...ids.filter((id) => !had.includes(id)).map((id) => ({ op: "add", path: \`\${this.path}/\${field}/-\`, value: id })), ...had.filter((id) => !ids.includes(id)).map((id) => ({ op: "remove", path: \`\${this.path}/\${field}/\${id}\` }))];\n }\n`;
|
|
1503
1867
|
}
|
|
1504
1868
|
if (c.link.length)
|
|
1505
1869
|
elements += ` /** Link it to one more id in a list (${c.link.join(", ")}): the ids there already are left as they are. */\n link(field: ${c.link.map((f) => JSON.stringify(f)).join(" | ")}, id: number, why?: string) { return send(this, [{ op: "add", path: \`\${this.path}/\${field}/-\`, value: id }], why); }\n`;
|
|
@@ -1534,11 +1898,141 @@ export type ${Doc}Op =${ops.length ? `\n | ${ops.join("\n | ")}` : " never"};
|
|
|
1534
1898
|
* whole by its rules, and made whole or not at all. */
|
|
1535
1899
|
export function write(from: Element, ops: ${Doc}Op[], why?: string) { return send(from, ops, why); }
|
|
1536
1900
|
`;
|
|
1537
|
-
rules += heldBy(collections, defs, from);
|
|
1901
|
+
rules += heldBy(collections, defs, from) + listsHeldBy(collections, defs, from);
|
|
1902
|
+
const theirs = theirsOf(collections, defs, from);
|
|
1903
|
+
if (theirs)
|
|
1904
|
+
rules += `\n/** The collections of values that are each member's own (its definitions' mine): the link to the item each is of, and to its member, who alone changes or removes it. */\nexport const own: Record<string, { of: string; by: string }> = ${JSON.stringify(theirs)};\n`;
|
|
1905
|
+
const own = mineOf(collections, defs, from);
|
|
1906
|
+
if (own)
|
|
1907
|
+
rules += `\n/** The lists that are each member's own (its definitions' mine), or links to one: a member gives and takes only their own id. */\nexport const mine: Record<string, string[]> = ${JSON.stringify(own)};\n${Object.keys(mineSaid(defs)).length ? `/** The words a write giving or taking another's is refused in, where its mine says them. */\nexport const mineSaid: Record<string, Record<string, string>> = ${JSON.stringify(mineSaid(defs))};\n` : ""}`;
|
|
1538
1908
|
// a derived document is derived for each page, its member's: it has no roles, and is published to no topic
|
|
1539
1909
|
if (!derivedFrom && !readOnly)
|
|
1540
1910
|
rules += whoRules(doc, Object.keys(collections), defs, grants, defs.kind && holders[defs.kind.root]);
|
|
1541
|
-
|
|
1911
|
+
// a derived or read-only document is rows too, which none may change: the kit reads it as any other (a timeline of
|
|
1912
|
+
// a sequence derived from starts, results as rows)
|
|
1913
|
+
const rows = fixed ? null : rowsOf({ doc, collections }, defs, link, HEAD, prefix, grants);
|
|
1914
|
+
return { rules, elements, rows };
|
|
1915
|
+
}
|
|
1916
|
+
|
|
1917
|
+
/** generated/rows.ts: each collection of a document as rows, drawn by joined/rows (src/rows.ts) as this says it, so
|
|
1918
|
+
* nothing is guessed as it runs. What names an item: the first words an add needs, else its first words. What each
|
|
1919
|
+
* field is called: its name. How each is held: each member's own (mine), moved by its steps (moves), the server's
|
|
1920
|
+
* (fills, copies, totals), a link to one item, a list of ids; else asked for by its type. None, of a document with
|
|
1921
|
+
* no collection (its own fields are its page's). */
|
|
1922
|
+
export function rowsOf(
|
|
1923
|
+
{ doc, collections }: Model,
|
|
1924
|
+
defs: Definitions,
|
|
1925
|
+
link: LinkOf,
|
|
1926
|
+
head: string,
|
|
1927
|
+
prefix: string,
|
|
1928
|
+
grants: Record<string, string[]> = {},
|
|
1929
|
+
) {
|
|
1930
|
+
const filled = (coll: string, f: string) =>
|
|
1931
|
+
f in (collections[coll]!.fills ?? {}) ||
|
|
1932
|
+
!!collections[coll]!.copies?.fields.includes(f) ||
|
|
1933
|
+
f in (defs.totals?.[coll] ?? {});
|
|
1934
|
+
const titleOf = (coll: string) =>
|
|
1935
|
+
[...collections[coll]!.required, ...Object.keys(collections[coll]!.fields)].find(
|
|
1936
|
+
(f) => collections[coll]!.fields[f] === "string" && !filled(coll, f) && defs.moves?.[coll] !== f,
|
|
1937
|
+
);
|
|
1938
|
+
// a link to one that an add needs, to a collection of this document: the item it's of, and drawn under
|
|
1939
|
+
const underOf = (name: string) =>
|
|
1940
|
+
collections[name]!.required.filter((f) => collections[name]!.fields[f] === "integer" && link(f).one)
|
|
1941
|
+
.map((f) => ({ field: f, in: link(f).to }))
|
|
1942
|
+
.find((u) => u.in !== name && link(u.field).at === doc && collections[u.in] && !collections[u.in]!.own);
|
|
1943
|
+
const toOf = (f: string) => ({
|
|
1944
|
+
tag: `${PREFIX}-${itemOf(link(f).to)}`,
|
|
1945
|
+
...(link(f).at === doc && collections[link(f).to] ? { title: titleOf(link(f).to) } : {}),
|
|
1946
|
+
...(link(f).to === "members" ? { people: true } : {}),
|
|
1947
|
+
});
|
|
1948
|
+
// where a field that moves may be, in the order the stories take it there: the step an add gives it, then each
|
|
1949
|
+
// step a story moves it from or to (the grants' "replace questions.status:open>settled")
|
|
1950
|
+
const stepsOf = (coll: string, f: string) => {
|
|
1951
|
+
const first = defs.fills?.[coll]?.[f],
|
|
1952
|
+
step = new RegExp(`^(?:add|replace) ${coll}\\.${f}:(.*)>(.*)$`);
|
|
1953
|
+
const told = Object.keys(grants).flatMap((g) => step.exec(g)?.slice(1) ?? []);
|
|
1954
|
+
return [...new Set([...(typeof first === "string" ? [first] : []), ...told].filter(Boolean))];
|
|
1955
|
+
};
|
|
1956
|
+
const specs = Object.entries(collections)
|
|
1957
|
+
.filter(([, c]) => !c.own)
|
|
1958
|
+
.map(([name, c]) => ({
|
|
1959
|
+
doc,
|
|
1960
|
+
name,
|
|
1961
|
+
place: `${PREFIX}-${name}`,
|
|
1962
|
+
tag: `${PREFIX}-${itemOf(name)!}`,
|
|
1963
|
+
one: itemOf(name)!,
|
|
1964
|
+
title: titleOf(name),
|
|
1965
|
+
under: underOf(name),
|
|
1966
|
+
// values each member's own, of the item they're under: never rows of their own
|
|
1967
|
+
...(defs.mine?.[name] && !Array.isArray(defs.mine[name]) ? { mine: defs.mine[name] } : {}),
|
|
1968
|
+
// (one of an object's fields, terms.days, isn't drawn yet)
|
|
1969
|
+
fields: Object.entries(c.fields)
|
|
1970
|
+
.filter(([f]) => !f.includes("."))
|
|
1971
|
+
.map(([f, type]) => {
|
|
1972
|
+
const own =
|
|
1973
|
+
type === "ids" &&
|
|
1974
|
+
listsOf(defs.mine?.[name])
|
|
1975
|
+
.map((g) => [g].flat())
|
|
1976
|
+
.find((g) => g.includes(f));
|
|
1977
|
+
return {
|
|
1978
|
+
name: f,
|
|
1979
|
+
type,
|
|
1980
|
+
...(c.required.includes(f) ? { needed: true } : {}),
|
|
1981
|
+
...(own
|
|
1982
|
+
? { how: "mine", with: own.filter((o) => o !== f) }
|
|
1983
|
+
: defs.moves?.[name] === f
|
|
1984
|
+
? { how: "moves", steps: stepsOf(name, f) }
|
|
1985
|
+
: filled(name, f)
|
|
1986
|
+
? { how: "filled" }
|
|
1987
|
+
: type === "ids"
|
|
1988
|
+
? { how: "ids" }
|
|
1989
|
+
: type === "integer" && link(f).one
|
|
1990
|
+
? { how: "one" }
|
|
1991
|
+
: {}),
|
|
1992
|
+
...(type === "ids" || (type === "integer" && link(f).one) ? { to: toOf(f) } : {}),
|
|
1993
|
+
// a reference, which pops what it points at: any link that isn't structure (part)
|
|
1994
|
+
...((type === "ids" || (type === "integer" && link(f).one)) && !link(f).part ? { ref: true as const } : {}),
|
|
1995
|
+
};
|
|
1996
|
+
}),
|
|
1997
|
+
}));
|
|
1998
|
+
if (!specs.length) return null;
|
|
1999
|
+
const Item = (tag: string) => Pascal(tag);
|
|
2000
|
+
const inner = (s: (typeof specs)[number]) => specs.filter((n) => n.under?.in === s.name);
|
|
2001
|
+
return `${head}
|
|
2002
|
+
// Each collection of ${doc} as rows (joined/rows, the skill's §3): a page defines the ones it shows, as they are or
|
|
2003
|
+
// extended, by define() from joined/rows, and holds each collection's element in its rows:
|
|
2004
|
+
// ${specs.map((s) => `<${s.place}-rows>${[s, ...inner(s)].map((n) => `<${n.place}></${n.place}>`).join("")}</${s.place}-rows>`).join("\n// ")}
|
|
2005
|
+
// A collection whose items are each of one of another's is drawn under it there, where its element is put in too.
|
|
2006
|
+
// To change what one says or does, extend its class and override a part (label, named, says, facts, deeds, row,
|
|
2007
|
+
// step, asks); to change how it looks, style its parts (::part(row), title, state, facts, deeds, mine, step, add,
|
|
2008
|
+
// under, drawer). Its sheet, a search of it and how a field reads are joined/kit's, given these collections by
|
|
2009
|
+
// use(specs) (define() gives a display's own). To draw something else altogether, don't define it: a display of the app's own holds the same
|
|
2010
|
+
// elements. Never change this file: change the stories.
|
|
2011
|
+
|
|
2012
|
+
import { rows, type Spec } from "joined/rows";
|
|
2013
|
+
import { ${specs.map((s) => Item(s.tag)).join(", ")} } from "./${prefix}elements.ts";
|
|
2014
|
+
${specs.map((s) => `\nconst ${s.name.replaceAll("-", "_")}: Spec = ${JSON.stringify(s)};`).join("")}
|
|
2015
|
+
|
|
2016
|
+
/** Each collection as joined's kit (joined/kit) is given it, by name: use(specs). */
|
|
2017
|
+
export const specs = { ${specs.map((s) => `"${s.name}": ${s.name.replaceAll("-", "_")}`).join(", ")} };
|
|
2018
|
+
${specs
|
|
2019
|
+
.map(
|
|
2020
|
+
(s) => `
|
|
2021
|
+
/** <${s.place}-rows>: ${doc}'s ${s.name} as rows${s.title ? `, each named by its ${s.title}` : ""}${
|
|
2022
|
+
inner(s).length
|
|
2023
|
+
? `, its ${inner(s)
|
|
2024
|
+
.map((n) => n.name)
|
|
2025
|
+
.join(" and ")} under it`
|
|
2026
|
+
: ""
|
|
2027
|
+
}. */
|
|
2028
|
+
export class ${Item(s.place)}Rows extends rows(${s.name.replaceAll("-", "_")}, { ${[s, ...inner(s)].map((n) => `"${n.tag}": ${Item(n.tag)}`).join(", ")} }${inner(
|
|
2029
|
+
s,
|
|
2030
|
+
)
|
|
2031
|
+
.map((n) => `, ${n.name.replaceAll("-", "_")}`)
|
|
2032
|
+
.join("")}) {}
|
|
2033
|
+
`,
|
|
2034
|
+
)
|
|
2035
|
+
.join("")}`;
|
|
1542
2036
|
}
|
|
1543
2037
|
|
|
1544
2038
|
/** The element of a document's own fields (<j-hello greeting>): it holds its document, and is its one item, each
|
|
@@ -1548,16 +2042,20 @@ function ownElement(doc: string, c: Coll) {
|
|
|
1548
2042
|
Class = Pascal(tag),
|
|
1549
2043
|
fields = Object.entries(c.fields);
|
|
1550
2044
|
const ts = (t: Type) => (t === "integer" || t === "decimal" ? "number" : t === "boolean" ? t : "string");
|
|
2045
|
+
// what its seed always has, no story removing it: there as it's read
|
|
2046
|
+
const there = (f: string) => !!c.seedHas?.includes(f);
|
|
1551
2047
|
const read = (f: string, t: Type) =>
|
|
1552
2048
|
t === "boolean"
|
|
1553
2049
|
? `this.hasAttribute("${f}")`
|
|
1554
2050
|
: t === "integer" || t === "decimal"
|
|
1555
|
-
?
|
|
1556
|
-
|
|
2051
|
+
? there(f)
|
|
2052
|
+
? `Number(this.getAttribute("${f}"))`
|
|
2053
|
+
: `this.hasAttribute("${f}") ? Number(this.getAttribute("${f}")) : undefined`
|
|
2054
|
+
: `this.getAttribute("${f}") ?? ${there(f) ? '""' : "undefined"}`;
|
|
1557
2055
|
const F = c.replace.map((f) => JSON.stringify(f)).join(" | ");
|
|
1558
2056
|
return `
|
|
1559
2057
|
/** ${doc}'s own fields, as the stories give them. */
|
|
1560
|
-
export type ${Class}Data = { ${fields.map(([f, t]) => `${JSON.stringify(f)}
|
|
2058
|
+
export type ${Class}Data = { ${fields.map(([f, t]) => `${JSON.stringify(f)}${there(f) || t === "boolean" ? "" : "?"}: ${ts(t)}`).join("; ")} };
|
|
1561
2059
|
|
|
1562
2060
|
/** <${tag}>: ${doc} itself, holding it, its own fields each an attribute. */
|
|
1563
2061
|
export class ${Class} extends HTMLElement {
|
|
@@ -1591,8 +2089,11 @@ export const OWN = `${import.meta.dir}/members`;
|
|
|
1591
2089
|
/** Where what the pages change is kept, apart from the code: JOINED_DATA (on Railway, a volume), or .data/ in
|
|
1592
2090
|
* the folder joined runs in. */
|
|
1593
2091
|
export const DATA = Bun.env.JOINED_DATA || `${process.cwd()}/.data`;
|
|
1594
|
-
/** The store's engine (src/engine.ts): JOINED_ENGINE, sqlite or postgres
|
|
1595
|
-
|
|
2092
|
+
/** The store's engine (src/engine.ts): JOINED_ENGINE, sqlite or postgres; else Postgres where JOINED_POSTGRES names
|
|
2093
|
+
* a server, else SQLite. */
|
|
2094
|
+
export const engineOf = (env: Record<string, string | undefined>) =>
|
|
2095
|
+
(env.JOINED_ENGINE || (env.JOINED_POSTGRES ? "postgres" : "sqlite")) as Dialect;
|
|
2096
|
+
export const ENGINE = engineOf(Bun.env);
|
|
1596
2097
|
/** The store's file, or Postgres's folder (src/store.ts): JOINED_DB, or joined.sqlite (postgres/) in the data folder. */
|
|
1597
2098
|
export const DB = Bun.env.JOINED_DB || `${DATA}/${ENGINE === "postgres" ? "postgres" : "joined.sqlite"}`;
|
|
1598
2099
|
|
|
@@ -1678,7 +2179,7 @@ export async function compiled(dir = APPS) {
|
|
|
1678
2179
|
// each a unit of its own, its files named for it
|
|
1679
2180
|
const told = toldBy[app]!,
|
|
1680
2181
|
cast = lines.find((l) => l.cast)?.cast ?? {},
|
|
1681
|
-
defined: Definitions = Object.assign({}, ...header);
|
|
2182
|
+
defined: Definitions = implied(Object.assign({}, ...header));
|
|
1682
2183
|
const bases = [...new Set(told.map((s) => (s.does ?? s.cannot)!.doc.split(/[:?]/)[0]!))];
|
|
1683
2184
|
const several = bases.length > 1 || !!defined.documents;
|
|
1684
2185
|
for (const unit of several ? bases : [app]) {
|
|
@@ -1692,11 +2193,20 @@ export async function compiled(dir = APPS) {
|
|
|
1692
2193
|
throw new Error(`${app}'s stories: a kind's root, ${defs[unit]!.kind!.root}, is another document's collection`);
|
|
1693
2194
|
filled(models[unit]!, defs[unit]!, `${app}'s definitions`);
|
|
1694
2195
|
}
|
|
2196
|
+
// a link's name is the link's in every document the app tells (a race's crossings' boat, and an entry's)
|
|
2197
|
+
if (several)
|
|
2198
|
+
for (const [f, l] of Object.entries(defined.links ?? {})) {
|
|
2199
|
+
const colls = bases.flatMap((u) =>
|
|
2200
|
+
Object.entries(models[u]!.collections).map(([n, c]): [string, Coll] => [`${u}'s ${n}`, c]),
|
|
2201
|
+
);
|
|
2202
|
+
const said = colls.some(([, c]) => c.fields[f]) && linkGiven(f, l, colls, `${app}'s stories`);
|
|
2203
|
+
if (said) throw new Error(said);
|
|
2204
|
+
}
|
|
1695
2205
|
entities(several ? bases : [app], models, defs, defined);
|
|
1696
2206
|
// a read-only document over the app's entities, a query its definition says (each side's books)
|
|
1697
2207
|
for (const [name, d] of Object.entries(defined.documents ?? {})) {
|
|
1698
2208
|
dirs[name] = at;
|
|
1699
|
-
split.add(name);
|
|
2209
|
+
if (name !== app) split.add(name);
|
|
1700
2210
|
stories[name] = [];
|
|
1701
2211
|
casts[name] = cast;
|
|
1702
2212
|
defs[name] = {};
|
|
@@ -1704,7 +2214,7 @@ export async function compiled(dir = APPS) {
|
|
|
1704
2214
|
readOnly[name] = d;
|
|
1705
2215
|
anonymousOf[name] = told.every((s) => s.as === undefined);
|
|
1706
2216
|
}
|
|
1707
|
-
if (several && !bases.includes(app)) delete dirs[app];
|
|
2217
|
+
if (several && !bases.includes(app) && !readOnly[app]) delete dirs[app];
|
|
1708
2218
|
}
|
|
1709
2219
|
// the seeds, as the documents begin: who each name in the stories is, and who that is to each document
|
|
1710
2220
|
const seeds: Record<string, Record<string, unknown>> = {};
|
|
@@ -1740,6 +2250,22 @@ export async function compiled(dir = APPS) {
|
|
|
1740
2250
|
}
|
|
1741
2251
|
c.copies = { from, fields, to: link.to, at: at.doc };
|
|
1742
2252
|
}
|
|
2253
|
+
// where no story adds an item, the seed's are all there are: a field every one has is typed there, as a needed one
|
|
2254
|
+
for (const [app, m] of Object.entries(models)) {
|
|
2255
|
+
if (derivedFrom[app] || readOnly[app] || defs[app]!.kind) continue;
|
|
2256
|
+
for (const [name, c] of Object.entries(m.collections)) {
|
|
2257
|
+
const added = Object.values(models).some((o) => dirs[o.doc] === dirs[app] && o.collections[name]?.adds);
|
|
2258
|
+
const items = (c.own ? [ownOf(seeds[m.doc]!)] : (seeds[m.doc]![name] ?? [])) as Record<string, unknown>[];
|
|
2259
|
+
if (added || c.copies || !items.length) continue;
|
|
2260
|
+
const has = Object.keys(c.fields).filter(
|
|
2261
|
+
(f) =>
|
|
2262
|
+
!c.required.includes(f) &&
|
|
2263
|
+
!c.removeField.includes(f) &&
|
|
2264
|
+
items.every((i) => (f.includes(".") ? (i[f.split(".")[0]!] as any)?.[f.split(".")[1]!] : i[f]) != null),
|
|
2265
|
+
);
|
|
2266
|
+
if (has.length) c.seedHas = has;
|
|
2267
|
+
}
|
|
2268
|
+
}
|
|
1743
2269
|
const members = (seeds.members?.members ?? []) as { id: number; name: string }[];
|
|
1744
2270
|
// an element's name is one per page: no two documents have a collection of one name
|
|
1745
2271
|
// across apps; an app's documents may each hold one of its tables (the till's orders, the kitchen's)
|
|
@@ -1898,12 +2424,20 @@ export async function compiled(dir = APPS) {
|
|
|
1898
2424
|
model: Model;
|
|
1899
2425
|
rules: string;
|
|
1900
2426
|
elements: string;
|
|
2427
|
+
rows: string | null;
|
|
1901
2428
|
queries: string | null;
|
|
1902
2429
|
tables: Record<string, Table>;
|
|
1903
2430
|
/** Its files' names: a unit of an app of several documents, named for it (catalogue.rules.ts). */
|
|
1904
2431
|
prefix: string;
|
|
1905
2432
|
}
|
|
1906
2433
|
> = {};
|
|
2434
|
+
/** Whether a document is anyone's, every story of it told by nobody: one with guests is held by them as guests,
|
|
2435
|
+
* and a derived one is anyone's only where all it's derived from is. */
|
|
2436
|
+
const anonymous = (app: string): boolean =>
|
|
2437
|
+
anonymousOf[app] ??
|
|
2438
|
+
(derivedFrom[app]
|
|
2439
|
+
? derivedFrom[app].every((d) => anonymous(Object.keys(models).find((a) => models[a]!.doc === d)!))
|
|
2440
|
+
: stories[app]!.every((s) => s.as === undefined) && !defs[app]!.guests);
|
|
1907
2441
|
for (const app of Object.keys(models).sort()) {
|
|
1908
2442
|
const holders = holdersOf(app);
|
|
1909
2443
|
// named from joined's own folder for its members, and from the apps' folder's for theirs
|
|
@@ -1953,8 +2487,7 @@ export async function compiled(dir = APPS) {
|
|
|
1953
2487
|
holders,
|
|
1954
2488
|
defs[app],
|
|
1955
2489
|
checkedFor(app),
|
|
1956
|
-
|
|
1957
|
-
anonymousOf[app] ?? (stories[app]!.every((s) => s.as === undefined) && !defs[app]!.guests),
|
|
2490
|
+
anonymous(app),
|
|
1958
2491
|
derivedFrom[app],
|
|
1959
2492
|
statics.has(app),
|
|
1960
2493
|
readOnly[app],
|
|
@@ -1989,10 +2522,11 @@ export async function writeAll(dir = APPS) {
|
|
|
1989
2522
|
ofDir.set(dir, had);
|
|
1990
2523
|
}
|
|
1991
2524
|
const migrated = new Set<string>();
|
|
1992
|
-
for (const { dir, doc, model, rules, elements, queries, prefix } of Object.values(out)) {
|
|
2525
|
+
for (const { dir, doc, model, rules, elements, rows, queries, prefix } of Object.values(out)) {
|
|
1993
2526
|
said.push(...loosened(await neededBefore(`${dir}/generated/${prefix}rules.ts`), model));
|
|
1994
2527
|
await changed(`${dir}/generated/${prefix}rules.ts`, rules);
|
|
1995
2528
|
await changed(`${dir}/generated/${prefix}elements.ts`, elements);
|
|
2529
|
+
if (rows) await changed(`${dir}/generated/${prefix}rows.ts`, rows);
|
|
1996
2530
|
if (queries) await changed(`${dir}/generated/${prefix}queries.ts`, queries);
|
|
1997
2531
|
if (migrated.has(dir)) continue;
|
|
1998
2532
|
migrated.add(dir);
|