@blueshed/joined 0.3.0 → 0.3.1
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/reference.md +57 -18
- package/docs/design.md +93 -38
- package/docs/introduction.md +9 -9
- package/docs/light-and-shadow.svg +1 -0
- package/docs/links-and-trust.svg +1 -0
- package/docs/stories-to-code.svg +2 -1
- package/docs/wire-op.svg +1 -0
- package/examples/app.css +9 -0
- package/examples/cafe/cafe.css +87 -69
- package/examples/cafe/generated/staff.elements.ts +7 -2
- package/examples/cafe/generated/staff.rules.ts +3 -0
- package/examples/cafe/generated/supplies.rules.ts +5 -0
- package/examples/cafe/kitchen.css +11 -19
- package/examples/cafe/kitchen.ts +6 -2
- package/examples/cafe/migrations/002-guard-supplies.ts +15 -0
- package/examples/cafe/order.html +2 -1
- package/examples/cafe/order.ts +1 -1
- package/examples/cafe/staff.json +5 -0
- package/examples/cafe/stories.jsonl +4 -2
- package/examples/cafe/supplies.html +1 -0
- package/examples/cafe/supplies.ts +45 -28
- package/examples/cafe/till.ts +14 -10
- package/examples/cafe/views.ts +7 -5
- package/examples/chart/arches.ts +5 -3
- package/examples/chart/chart.css +7 -2
- package/examples/desk/desk.css +6 -10
- package/examples/desk/generated/elements.ts +1 -1
- package/examples/desk/index.html +1 -1
- package/examples/desk/lanes.css +21 -19
- package/examples/desk/stories.jsonl +1 -1
- package/examples/desk/tally.css +1 -1
- package/examples/docs.ts +7 -4
- package/examples/index.html +16 -7
- package/examples/notes/generated/queries.ts +8 -2
- package/examples/notes/index.html +1 -1
- package/examples/notes/stories.jsonl +3 -0
- package/examples/page.ts +9 -2
- package/examples/people/links.ts +7 -9
- package/examples/people/people.css +1 -1
- package/examples/people/view.ts +1 -1
- package/examples/trip/generated/elements.ts +1 -1
- package/examples/trip/stays.css +1 -1
- package/examples/trip/stays.ts +1 -1
- package/examples/trips/generated/elements.ts +1 -1
- package/examples/trips/trips.css +24 -23
- package/examples/welcome.css +41 -33
- package/package.json +1 -1
- package/src/attachment.ts +17 -2
- package/src/board.css +2 -1
- package/src/board.ts +4 -4
- package/src/cli.ts +14 -2
- package/src/compile.ts +255 -77
- package/src/definitions.ts +62 -14
- package/src/demo.css +2 -5
- package/src/draft.ts +10 -4
- package/src/drawer.ts +30 -4
- package/src/engine.ts +14 -5
- package/src/fields.ts +88 -15
- package/src/file-value.ts +12 -2
- package/src/files.ts +113 -35
- package/src/icon-names.ts +4 -0
- package/src/keep.ts +16 -3
- package/src/map.css +23 -16
- package/src/map.ts +3 -3
- package/src/mcp.ts +24 -3
- package/src/members/group.ts +41 -17
- package/src/members/keys.ts +3 -3
- package/src/members/panel.css +17 -18
- package/src/members/panel.ts +9 -2
- package/src/migrate.ts +58 -21
- package/src/migrations/005-change-kept.ts +7 -0
- package/src/month.css +6 -2
- package/src/month.ts +5 -5
- package/src/ops.ts +40 -1
- package/src/page.css +3 -2
- package/src/page.ts +64 -16
- package/src/parts.css +178 -86
- package/src/parts.ts +60 -20
- package/src/plane.css +9 -4
- package/src/plane.ts +4 -2
- package/src/reading.css +1 -5
- package/src/rows.css +2 -21
- package/src/rows.ts +123 -39
- package/src/server.ts +157 -30
- package/src/sheet.ts +4 -2
- package/src/slots.css +0 -4
- package/src/specs.ts +1 -1
- package/src/starter.ts +15 -1
- package/src/store.ts +40 -18
- package/src/surface.ts +7 -2
- package/src/time.ts +2 -0
- package/src/timeline.ts +14 -5
- package/src/together.ts +15 -17
- package/src/tokens.css +12 -3
- package/src/tokens.json +38 -7
- package/src/tool.ts +6 -7
- package/src/upkeep.ts +31 -4
- package/src/views.css +8 -9
- package/src/views.ts +23 -6
- package/src/week.css +1 -2
- package/src/week.ts +11 -7
- package/src/write.ts +130 -15
- package/starter/CHANGELOG.md +8 -0
- package/starter/CLAUDE.md +7 -1
- package/starter/bunfig +2 -1
- package/starter/githooks/pre-commit +13 -0
- package/starter/prettierignore +1 -0
- package/starter/tests/records.test.ts +40 -0
- package/starter/todo.jsonl +0 -0
package/src/compile.ts
CHANGED
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
grantOf,
|
|
17
17
|
grantsOf,
|
|
18
18
|
whoCode,
|
|
19
|
+
pathSaid,
|
|
19
20
|
whoRules,
|
|
20
21
|
whoSql,
|
|
21
22
|
kindFrozen,
|
|
@@ -26,8 +27,8 @@ import {
|
|
|
26
27
|
type Where,
|
|
27
28
|
} from "./definitions.ts";
|
|
28
29
|
import { constraintSql, refuseSql, triggerSql, type Dialect, type Trigger } from "./engine.ts";
|
|
29
|
-
import { diff, fromFolder, stateOf, writeMigration, type Change, type Column, type Table } from "./migrate.ts";
|
|
30
|
-
import { docParts, parts, type Op, type Type } from "./ops.ts";
|
|
30
|
+
import { diff, fromFolder, isAdd, stateOf, writeMigration, type Change, type Column, type Table } from "./migrate.ts";
|
|
31
|
+
import { docParts, linksNamed, NTH, parts, type Linked, type Op, type Type } from "./ops.ts";
|
|
31
32
|
import { settingsCode, settingsSaid, settingsUnchecked } from "./settings.ts";
|
|
32
33
|
import { partsOf, told as toldOf, unwritten, writtenBefore } from "./together.ts";
|
|
33
34
|
import { guestRole, visitorsRefused } from "./visitors.ts";
|
|
@@ -61,6 +62,9 @@ type Coll = {
|
|
|
61
62
|
unlink: string[];
|
|
62
63
|
/** The fields the server fills (definitions' fills), with what: "now", or a value. */
|
|
63
64
|
fills?: Record<string, unknown>;
|
|
65
|
+
/** Where the zone of a wall clock it fills "now" is said, the first that says one: the item's own, its kind's
|
|
66
|
+
* root's, the document's own. */
|
|
67
|
+
clock?: ("item" | "root" | "document")[];
|
|
64
68
|
/** The field whose time, when it comes, the server clears (definitions' timed). */
|
|
65
69
|
timed?: string;
|
|
66
70
|
/** The document's own fields (hello's greeting, /greeting): a collection named for it, of one item. */
|
|
@@ -125,12 +129,13 @@ const isObject = (v: unknown): v is Record<string, unknown> => typeof v === "obj
|
|
|
125
129
|
const flatKeys = (value: Record<string, unknown>) =>
|
|
126
130
|
Object.entries(value).flatMap(([f, v]) => (isObject(v) ? Object.keys(v).map((k) => `${f}.${k}`) : [f]));
|
|
127
131
|
|
|
128
|
-
/** A value's type: a string's form, a number's fraction, or none it can be.
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
132
|
+
/** A value's type: a string's form, a number's fraction, or none it can be. Where a link is (`link`), "$n" is an id:
|
|
133
|
+
* the id an add before it in the same write was given; anywhere else it's words. */
|
|
134
|
+
const typeOf = (v: unknown, link = false): Type | undefined => {
|
|
135
|
+
const id = (x: unknown) => Number.isInteger(x) || (link && typeof x === "string" && NTH.test(x));
|
|
136
|
+
return typeof v === "string"
|
|
137
|
+
? id(v)
|
|
138
|
+
? "integer"
|
|
134
139
|
: ((Object.keys(FORMS) as (keyof typeof FORMS)[]).find((k) => FORMS[k].test(v)) ?? "string")
|
|
135
140
|
: typeof v === "number"
|
|
136
141
|
? Number.isInteger(v)
|
|
@@ -138,9 +143,11 @@ const typeOf = (v: unknown): Type | undefined =>
|
|
|
138
143
|
: "decimal"
|
|
139
144
|
: typeof v === "boolean"
|
|
140
145
|
? "boolean"
|
|
141
|
-
:
|
|
146
|
+
: // a list of ids is a link: any of them an add's before it in the same write ("$1")
|
|
147
|
+
Array.isArray(v) && v.every((x) => Number.isInteger(x) || (typeof x === "string" && NTH.test(x)))
|
|
142
148
|
? "ids"
|
|
143
149
|
: undefined;
|
|
150
|
+
};
|
|
144
151
|
/** The type two values of a field are together: strings of two forms are strings, and a fraction makes a decimal. */
|
|
145
152
|
const both = (a: Type, b: Type): Type | undefined =>
|
|
146
153
|
a === b
|
|
@@ -153,14 +160,28 @@ const both = (a: Type, b: Type): Type | undefined =>
|
|
|
153
160
|
|
|
154
161
|
/** A model with what its definitions say the server fills and clears: each a field of the server's, which no
|
|
155
162
|
* story gives (a "random" code a story may replace, the server making it new), its type its fill's ("now" an
|
|
156
|
-
* instant); a timed field an instant the stories give, in
|
|
157
|
-
* that isn't a kind. */
|
|
158
|
-
export function filled(
|
|
163
|
+
* instant, or a wall clock where the seed's items, `seed`, give it one); a timed field an instant the stories give, in
|
|
164
|
+
* a document that isn't a kind. */
|
|
165
|
+
export function filled(
|
|
166
|
+
model: Model,
|
|
167
|
+
defs: Definitions,
|
|
168
|
+
from: string,
|
|
169
|
+
seed: (coll: string) => Record<string, unknown>[] = () => [],
|
|
170
|
+
) {
|
|
159
171
|
const at = (coll: string, what: string) => {
|
|
160
172
|
const c = model.collections[coll];
|
|
161
173
|
if (!c) throw new Error(`${from}: ${what} names ${coll}, which no story has`);
|
|
162
174
|
return c;
|
|
163
175
|
};
|
|
176
|
+
const nowIs = (coll: string, f: string): Type => {
|
|
177
|
+
const given = new Set(seed(coll).flatMap((i) => (i[f] === undefined ? [] : [typeOf(i[f])])));
|
|
178
|
+
const [t = "instant", ...more] = given;
|
|
179
|
+
if (more.length || (t !== "instant" && t !== "date-time"))
|
|
180
|
+
throw new Error(
|
|
181
|
+
`${from}: ${coll}' ${f} is filled "now", so its seed gives it ${SAID.instant} or ${SAID["date-time"]}, each the same`,
|
|
182
|
+
);
|
|
183
|
+
return t;
|
|
184
|
+
};
|
|
164
185
|
for (const [coll, fills] of Object.entries(defs.fills ?? {})) {
|
|
165
186
|
const c = at(coll, "fills");
|
|
166
187
|
for (const [f, v] of Object.entries(fills)) {
|
|
@@ -171,7 +192,7 @@ export function filled(model: Model, defs: Definitions, from: string) {
|
|
|
171
192
|
if (c.fields[f] && !moved && !renewed)
|
|
172
193
|
throw new Error(`${from}: ${coll}' ${f} is the server's to fill, so no story gives it`);
|
|
173
194
|
// "me": the member who adds it, an id
|
|
174
|
-
const t = v === "now" ?
|
|
195
|
+
const t = v === "now" ? nowIs(coll, f) : v === "me" ? "integer" : typeOf(v);
|
|
175
196
|
if (!t || t === "ids")
|
|
176
197
|
throw new Error(`${from}: ${coll}' ${f} is filled with a string, a boolean, a number, "now" or "me"`);
|
|
177
198
|
c.fields[f] = t;
|
|
@@ -439,6 +460,9 @@ export function modelFrom(doc: string, example: Record<string, unknown>, from: s
|
|
|
439
460
|
items.forEach((item: Record<string, unknown>, i) => {
|
|
440
461
|
const { id, ...fields } = item;
|
|
441
462
|
for (const [f, v] of Object.entries(fields)) {
|
|
463
|
+
// its item's element's attribute, as a written document's field is: never a capital (likeName arrived likename)
|
|
464
|
+
const why = unnamed(f);
|
|
465
|
+
if (why) throw new Error(`${from}: ${name}' ${why}`);
|
|
442
466
|
const t = typeOf(v),
|
|
443
467
|
now = t && (c.fields[f] ? both(c.fields[f]!, t) : t);
|
|
444
468
|
if (!now) throw new Error(`${from}: ${name} ${id}'s ${f} isn't what the other items' ${f} is`);
|
|
@@ -621,6 +645,13 @@ export function linksOf(
|
|
|
621
645
|
};
|
|
622
646
|
};
|
|
623
647
|
}
|
|
648
|
+
/** A collection's links, each the collection it's to: a list of ids, or one id where its link says so. */
|
|
649
|
+
const linksIn = (c: Coll, link: LinkOf) =>
|
|
650
|
+
Object.fromEntries(
|
|
651
|
+
Object.entries(c.fields)
|
|
652
|
+
.filter(([f, t]) => t === "ids" || (t === "integer" && link(f).one))
|
|
653
|
+
.map(([f]) => [f, link(f).to]),
|
|
654
|
+
);
|
|
624
655
|
/** A collection's links that go with what they point at, in its own document (`goes`): removing that item removes
|
|
625
656
|
* this one first, in the same write. None, nothing said. */
|
|
626
657
|
const goes = (c: Coll, link: LinkOf, doc: string) => {
|
|
@@ -691,7 +722,10 @@ export function tablesOf(
|
|
|
691
722
|
};
|
|
692
723
|
continue;
|
|
693
724
|
}
|
|
694
|
-
|
|
725
|
+
// (an object's field, terms.days, in its object)
|
|
726
|
+
const [o, sub] = f.split(".") as [string, string?];
|
|
727
|
+
const has = (i: Record<string, unknown>) => (sub === undefined ? o in i : isObject(i[o]) && sub in i[o]);
|
|
728
|
+
const always = c.required.includes(f) && ((seed[name] ?? []) as Record<string, unknown>[]).every(has);
|
|
695
729
|
const one = t === "integer" && link(f).one;
|
|
696
730
|
const member = one && link(f).to === GROUP;
|
|
697
731
|
columns[f] = {
|
|
@@ -766,6 +800,9 @@ const POSTGRES: Sql = {
|
|
|
766
800
|
not: "IS DISTINCT FROM",
|
|
767
801
|
};
|
|
768
802
|
|
|
803
|
+
/** SQL as a template literal holds it: each backslash, backtick and ${ escaped, so it's the SQL as written. */
|
|
804
|
+
const templated = (sql: string) => sql.replace(/\\|`|\$\{/g, (c) => `\\${c}`);
|
|
805
|
+
|
|
769
806
|
/** generated/queries.ts: the document as one statement over its tables, in SQLite's JSON functions and Postgres's. Its version,
|
|
770
807
|
* then each collection's items by id: a field with nothing in it left out, as the dialect (`Sql`) writes it; a boolean
|
|
771
808
|
* true or false, and a list its ids, gathered from its pairs (none: left out). The page stays flat: a link's ids,
|
|
@@ -866,7 +903,7 @@ export function queriesOf(
|
|
|
866
903
|
export const items: Record<string, string> = ${JSON.stringify(lite.items, null, 2)};
|
|
867
904
|
|
|
868
905
|
/** ${doc}, whole, as one statement over its tables (SQLite): its parameters $doc${kind ? ", and $key, the instance (" + kind + ":1)" : ""}${params.length ? `, and its window's, ${params.map((p) => `$${p}`).join(", ")}` : ""}${Object.keys(owns).length ? ", and $member, who's asking (none: all of it)" : ""}. */
|
|
869
|
-
export const query = \`${lite.query}\`;
|
|
906
|
+
export const query = \`${templated(lite.query)}\`;
|
|
870
907
|
|
|
871
908
|
/** Its rules over values (definitions' takes, gives and totals), as triggers the store makes in its database: run in
|
|
872
909
|
* each write's transaction, and told as any change is. */
|
|
@@ -905,13 +942,15 @@ function itemSql(coll: string, c: Coll, d: Sql) {
|
|
|
905
942
|
/** A window's test of an item, as code: the same definition its query's WHERE is written from (each parameter given,
|
|
906
943
|
* a field equal to it, or a date and time in the days from it), so publish asks of a row what the read asks. */
|
|
907
944
|
function windowCode(window: NonNullable<Definitions["window"]>) {
|
|
945
|
+
// an item's field, as code: by its name, or in brackets where it's hyphened (set-by)
|
|
946
|
+
const at = (f: string) => (/^[a-z][a-z0-9]*$/.test(f) ? `item.${f}` : `item[${JSON.stringify(f)}]`);
|
|
908
947
|
const tests = Object.entries(window).map(([coll, params]) => {
|
|
909
948
|
const each = Object.entries(params).map(([p, by]) =>
|
|
910
949
|
typeof by === "string"
|
|
911
|
-
? `(p.${p} == null ||
|
|
950
|
+
? `(p.${p} == null || ${at(by)} === p.${p})`
|
|
912
951
|
: by.length
|
|
913
|
-
? `(p.${p} == null || (typeof
|
|
914
|
-
: `(p.${p} == null || (typeof
|
|
952
|
+
? `(p.${p} == null || (typeof ${at(by.from)} === "string" && ${at(by.from)} < days(p.${p}, ${days(by.for)}) && ends(${at(by.from)}, ${at(by.length)}) > days(p.${p}, 0)))`
|
|
953
|
+
: `(p.${p} == null || (typeof ${at(by.from)} === "string" && ${at(by.from)} >= p.${p} && ${at(by.from)} < days(p.${p}, ${days(by.for)})))`,
|
|
915
954
|
);
|
|
916
955
|
return `(coll !== ${JSON.stringify(coll)} || (${each.join(" && ")}))`;
|
|
917
956
|
});
|
|
@@ -1068,6 +1107,27 @@ export function takesSql(
|
|
|
1068
1107
|
}),
|
|
1069
1108
|
);
|
|
1070
1109
|
}
|
|
1110
|
+
// a field of words that's never none (definitions' notEmpty): an add's, and one changed (a row that's so already,
|
|
1111
|
+
// from before it was said, is left, and may be put back by an undo)
|
|
1112
|
+
for (const [coll, fields] of Object.entries(defs.notEmpty ?? {}))
|
|
1113
|
+
for (const f of fields) {
|
|
1114
|
+
if (collections[coll]?.fields[f] !== "string")
|
|
1115
|
+
throw new Error(`${from}: notEmpty names ${coll}' ${f}, which isn't a field of words`);
|
|
1116
|
+
const none = `trim(NEW.${q(f)}, ' \t\r\n') = ''`;
|
|
1117
|
+
for (const [event, cond] of [
|
|
1118
|
+
["INSERT", `${isAdd(coll)} AND ${none}`],
|
|
1119
|
+
["UPDATE", `NEW.${q(f)} ${d.not} OLD.${q(f)} AND ${none}`],
|
|
1120
|
+
] as const)
|
|
1121
|
+
out.push(
|
|
1122
|
+
trigger({
|
|
1123
|
+
name: `joined_empty_${coll}_${f}_${event.toLowerCase()}`,
|
|
1124
|
+
at: "BEFORE",
|
|
1125
|
+
event,
|
|
1126
|
+
table: coll,
|
|
1127
|
+
body: refuseSql(d.dialect, cond, said(emptySaid(f))),
|
|
1128
|
+
}),
|
|
1129
|
+
);
|
|
1130
|
+
}
|
|
1071
1131
|
// lists held as the write leaves it (definitions' within and apart): on Postgres, a trigger at its end (SQLite's are
|
|
1072
1132
|
// held by code, listsHold, as it has no trigger that waits)
|
|
1073
1133
|
// (checked of both, written of Postgres)
|
|
@@ -1153,6 +1213,7 @@ export function definedFor(d: Definitions, m: Model, stories: Story[]): Definiti
|
|
|
1153
1213
|
const links = Object.entries(d.links ?? {}).filter(([f]) => fields.has(f));
|
|
1154
1214
|
if (links.length) out.links = Object.fromEntries(links);
|
|
1155
1215
|
for (const k of [
|
|
1216
|
+
"title",
|
|
1156
1217
|
"copies",
|
|
1157
1218
|
"unique",
|
|
1158
1219
|
"noOverlap",
|
|
@@ -1170,6 +1231,7 @@ export function definedFor(d: Definitions, m: Model, stories: Story[]): Definiti
|
|
|
1170
1231
|
"addedWith",
|
|
1171
1232
|
"within",
|
|
1172
1233
|
"apart",
|
|
1234
|
+
"notEmpty",
|
|
1173
1235
|
] as const) {
|
|
1174
1236
|
const mine = has(d[k] as Record<string, unknown> | undefined);
|
|
1175
1237
|
if (mine && Object.keys(mine).length) out[k] = mine;
|
|
@@ -1195,7 +1257,7 @@ export function queryOf({ doc }: Model, from: string, spec: Query) {
|
|
|
1195
1257
|
const colls = Object.entries(spec).map(([coll, s]) => {
|
|
1196
1258
|
if ("count" in s) return counted(coll, s);
|
|
1197
1259
|
const sides = Object.entries(s.sides);
|
|
1198
|
-
const side = `CASE ${sides.map(([f, w]) => `WHEN o.${q(f)} = $member THEN '${w}'`).join(" ")} END`;
|
|
1260
|
+
const side = `CASE ${sides.map(([f, w]) => `WHEN o.${q(f)} = $member THEN '${w.replaceAll("'", "''")}'`).join(" ")} END`;
|
|
1199
1261
|
// the sum of the rows linking to it, or of its own fields
|
|
1200
1262
|
const total = `o.${q(s.total.sum)}${s.total.times ? ` * o.${q(s.total.times)}` : ""}`;
|
|
1201
1263
|
const where = `${s.when ? `o.${q(s.when)} = 1 AND ` : ""}(${sides.map(([f]) => `o.${q(f)} = $member`).join(" OR ")})`;
|
|
@@ -1207,7 +1269,7 @@ export function queryOf({ doc }: Model, from: string, spec: Query) {
|
|
|
1207
1269
|
return `// Written by src/compile.ts from ${from}: change the stories, not this.
|
|
1208
1270
|
|
|
1209
1271
|
/** ${doc}, as a member has it, one statement over the app's tables (SQLite): its parameters $doc and $member. */
|
|
1210
|
-
export const query = \`${query(SQLITE)}\`;
|
|
1272
|
+
export const query = \`${templated(query(SQLITE))}\`;
|
|
1211
1273
|
|
|
1212
1274
|
/** Its query, as Postgres says it. */
|
|
1213
1275
|
export const postgres = ${JSON.stringify({ query: query(POSTGRES) }, null, 2)};
|
|
@@ -1268,10 +1330,14 @@ const named = (changes: Change[]) =>
|
|
|
1268
1330
|
.map((c) => [c.change.replace(/[A-Z].*/, ""), c.key, "column" in c ? c.column : ""].filter(Boolean).join("-"))
|
|
1269
1331
|
.join("-and-") + (changes.length > 2 ? "-and-more" : "");
|
|
1270
1332
|
|
|
1333
|
+
/** What a field of words that's never none (definitions' notEmpty) is refused with. */
|
|
1334
|
+
export const emptySaid = (f: string) => `${f} cannot be empty`;
|
|
1335
|
+
|
|
1271
1336
|
/** What a story can't show, each a header line of an app's stories, after the cast (the skill's §15). */
|
|
1272
1337
|
const DEFINED = [
|
|
1273
1338
|
"links",
|
|
1274
1339
|
"roles",
|
|
1340
|
+
"title",
|
|
1275
1341
|
"owns",
|
|
1276
1342
|
"kind",
|
|
1277
1343
|
"fills",
|
|
@@ -1295,23 +1361,25 @@ const DEFINED = [
|
|
|
1295
1361
|
"addedWith",
|
|
1296
1362
|
"within",
|
|
1297
1363
|
"apart",
|
|
1364
|
+
"notEmpty",
|
|
1298
1365
|
];
|
|
1299
1366
|
|
|
1300
1367
|
/** 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
|
|
1301
1368
|
* instance; a link, by a story writing the field; a role, by a does granting it and a cannot by someone without
|
|
1302
1369
|
* it, or by owns naming it; owns, by a story someone mustn't hear (unseen); unique and noOverlap, by a cannot on the
|
|
1303
|
-
* collection; timed, by a story giving
|
|
1370
|
+
* collection; notEmpty, by a cannot emptying the field; timed, by a story giving it. What the server fills, no story gives: it's the server's. */
|
|
1304
1371
|
export function unchecked(
|
|
1305
1372
|
defs: Definitions,
|
|
1306
1373
|
stories: Story[],
|
|
1307
1374
|
grants: Record<string, string[]>,
|
|
1308
|
-
|
|
1375
|
+
/** Who a story's teller is, to the item an op is to (none: to the document). */
|
|
1376
|
+
rolesOfTeller: (s: Story, o?: Op) => string[],
|
|
1309
1377
|
/** The app's other documents: a role any of them checks is checked. */
|
|
1310
1378
|
siblings: {
|
|
1311
1379
|
defs: Definitions;
|
|
1312
1380
|
stories: Story[];
|
|
1313
1381
|
grants: Record<string, string[]>;
|
|
1314
|
-
rolesOfTeller: (s: Story) => string[];
|
|
1382
|
+
rolesOfTeller: (s: Story, o?: Op) => string[];
|
|
1315
1383
|
}[] = [],
|
|
1316
1384
|
): string | null {
|
|
1317
1385
|
const writes = (s: Story) => [
|
|
@@ -1356,13 +1424,13 @@ export function unchecked(
|
|
|
1356
1424
|
const holds = Object.values(u.defs.where ?? {}).flatMap((w) => (typeof w.holds === "string" ? [w.holds] : []));
|
|
1357
1425
|
const named = [...owns, ...holds].some((p) => p.split("|").some((t) => t.trim() === role));
|
|
1358
1426
|
const granted = Object.values(u.grants).some((g) => g.includes(role));
|
|
1359
|
-
const without = u.stories.some(
|
|
1360
|
-
(s)
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1427
|
+
const without = u.stories.some((s) =>
|
|
1428
|
+
// (who its teller is to the item each op is to: an @field role is the item's)
|
|
1429
|
+
s.cannot?.ops.some(
|
|
1430
|
+
(o) =>
|
|
1431
|
+
!u.rolesOfTeller(s, o).includes(role) &&
|
|
1364
1432
|
Object.entries(u.grants).some(([k, g]) => opOf(k) === grantOf(o) && g.includes(role)),
|
|
1365
|
-
|
|
1433
|
+
),
|
|
1366
1434
|
);
|
|
1367
1435
|
return named || (granted && without);
|
|
1368
1436
|
});
|
|
@@ -1377,6 +1445,15 @@ export function unchecked(
|
|
|
1377
1445
|
] as const)
|
|
1378
1446
|
for (const coll of Object.keys(colls ?? {}))
|
|
1379
1447
|
if (!on(coll)) return `${key} names ${coll}, but no cannot story writes it`;
|
|
1448
|
+
for (const [coll, fs] of Object.entries(defs.notEmpty ?? {}))
|
|
1449
|
+
for (const f of fs) {
|
|
1450
|
+
const emptied = (o: Op) => {
|
|
1451
|
+
const v = parts(o.path).field === f ? o.value : o.value?.[f];
|
|
1452
|
+
return parts(o.path).coll === coll && typeof v === "string" && !v.trim();
|
|
1453
|
+
};
|
|
1454
|
+
if (!stories.some((s) => s.refused === emptySaid(f) && s.cannot?.ops.some(emptied)))
|
|
1455
|
+
return `notEmpty names ${coll}' ${f}, but no cannot story empties it, refused in "${emptySaid(f)}"`;
|
|
1456
|
+
}
|
|
1380
1457
|
for (const [coll, fs] of Object.entries(defs.mine ?? {})) {
|
|
1381
1458
|
for (const f of listsOf(fs).flat())
|
|
1382
1459
|
if (!stories.some((s) => s.cannot?.ops.some((o) => parts(o.path).coll === coll && gives(o, f))))
|
|
@@ -1411,12 +1488,40 @@ export function unchecked(
|
|
|
1411
1488
|
["frozen", Object.values(itemsFrozen(defs)).map((x) => x.refused)],
|
|
1412
1489
|
["within", Object.values(defs.within ?? {}).flatMap((rs) => rs.map((r) => r.refused))],
|
|
1413
1490
|
["apart", Object.values(defs.apart ?? {}).flatMap((rs) => rs.map((r) => r.refused))],
|
|
1491
|
+
[
|
|
1492
|
+
"links' holds",
|
|
1493
|
+
Object.entries(defs.links ?? {}).flatMap(([f, l]) =>
|
|
1494
|
+
typeof l === "object" && l.holds ? [namesSaid(f, l.holds)] : [],
|
|
1495
|
+
),
|
|
1496
|
+
],
|
|
1414
1497
|
] as const)
|
|
1415
1498
|
for (const words of all)
|
|
1416
1499
|
if (!refusing(words)) return `${key} is checked by no story: none cannot, refused in "${words}"`;
|
|
1417
1500
|
return settingsUnchecked(defs, stories, grants) ?? visitorsRefused(defs, stories, rolesOfTeller);
|
|
1418
1501
|
}
|
|
1419
1502
|
|
|
1503
|
+
/** What a link that names only those a who-path finds is refused with, when it names another (links' holds). */
|
|
1504
|
+
export const namesSaid = (f: string, path: string) => `${f} names someone who isn't among ${pathSaid(path)}`;
|
|
1505
|
+
/** generated/rules.ts's names: each link of this document's fields that names only those a who-path of collections
|
|
1506
|
+
* finds (links' holds), as a test of the id named, and the words another is refused in. */
|
|
1507
|
+
const namesCode = (defs: Definitions, colls: { fields: Record<string, unknown> }[]) => {
|
|
1508
|
+
const names = Object.entries(defs.links ?? {}).flatMap(([f, l]) =>
|
|
1509
|
+
typeof l === "object" && l.holds && colls.some((c) => f in c.fields)
|
|
1510
|
+
? [
|
|
1511
|
+
` ${JSON.stringify(f)}: [(m, read, doc) => ${whoCode(l.holds, defs.roles ?? {}, `links' ${f} holds`)}, ${JSON.stringify(namesSaid(f, l.holds))}],`,
|
|
1512
|
+
]
|
|
1513
|
+
: [],
|
|
1514
|
+
);
|
|
1515
|
+
return names.length
|
|
1516
|
+
? `/** Each link whose every id names one a who-path finds, its test and the words another is refused in (its
|
|
1517
|
+
* definitions' links, holds). */
|
|
1518
|
+
export const names: Record<string, readonly [(m: number, read: (doc: string) => any, doc: string) => boolean, string]> = {
|
|
1519
|
+
${names.join("\n")}
|
|
1520
|
+
};
|
|
1521
|
+
`
|
|
1522
|
+
: "";
|
|
1523
|
+
};
|
|
1524
|
+
|
|
1420
1525
|
/** The stories language (docs/stories.md): the words a story, a write and an op may have, and no others. */
|
|
1421
1526
|
const STORY = ["story", "as", "does", "cannot", "seen", "unseen", "refused", "then", "premise"],
|
|
1422
1527
|
WRITE = ["doc", "ops", "why"],
|
|
@@ -1476,6 +1581,10 @@ export function premised(
|
|
|
1476
1581
|
/** What the server fills on an add to a document's collection, and who a name is: an add filled as it would be */
|
|
1477
1582
|
fillsOf: (doc: string, coll: string) => Record<string, unknown> | undefined,
|
|
1478
1583
|
memberOf: (name: string) => number | undefined,
|
|
1584
|
+
/** A document's collections, each its links: where a "$n" is read (src/ops.ts, linksNamed) */
|
|
1585
|
+
collectionsOf: (doc: string) => Linked | undefined,
|
|
1586
|
+
/** What a field is: whether "now" is an instant or a wall clock (none said, an instant) */
|
|
1587
|
+
typeIn: (doc: string, coll: string, field: string) => Type | undefined = () => undefined,
|
|
1479
1588
|
): Record<string, Record<string, unknown>> {
|
|
1480
1589
|
// each write with its teller (an anonymous story's, nobody)
|
|
1481
1590
|
const writes: [string | null, Write][] = !premise
|
|
@@ -1489,29 +1598,29 @@ export function premised(
|
|
|
1489
1598
|
const [name, n] = w.doc.split("?")[0]!.split(":") as [string, string?];
|
|
1490
1599
|
const doc = (state[name] ??= {});
|
|
1491
1600
|
const added: number[] = [];
|
|
1492
|
-
const
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
: Array.isArray(v)
|
|
1496
|
-
? v.map(given)
|
|
1497
|
-
: isObject(v)
|
|
1498
|
-
? Object.fromEntries(Object.entries(v).map(([f, x]) => [f, given(x)]))
|
|
1499
|
-
: v;
|
|
1500
|
-
for (const o of w.ops) {
|
|
1601
|
+
for (const op of w.ops) {
|
|
1602
|
+
// a later op's "$n" where a link is: the id its write's nth add was given
|
|
1603
|
+
const o = linksNamed(op, collectionsOf(name), (n) => added[n - 1]);
|
|
1501
1604
|
const [, coll = "", key, ...path] = o.path.split("/");
|
|
1502
1605
|
const rows = (doc[coll] ??= []) as Record<string, unknown>[];
|
|
1503
1606
|
if (!Array.isArray(rows)) continue;
|
|
1504
1607
|
if (o.op === "add" && key === "-") {
|
|
1505
1608
|
const id = Math.max(0, ...rows.map((r) => Number(r.id))) + 1;
|
|
1506
1609
|
added.push(id);
|
|
1507
|
-
// what the server fills, as it would: "me" its teller, "now" an instant, a code
|
|
1610
|
+
// what the server fills, as it would: "me" its teller, "now" an instant (or a wall clock), a code
|
|
1508
1611
|
const filled = Object.entries(fillsOf(name, coll) ?? {}).map(([f, v]) => [
|
|
1509
1612
|
f,
|
|
1510
|
-
v === "me"
|
|
1613
|
+
v === "me"
|
|
1614
|
+
? teller === null
|
|
1615
|
+
? null
|
|
1616
|
+
: (memberOf(teller) ?? null)
|
|
1617
|
+
: v === "now"
|
|
1618
|
+
? `2026-01-01T00:00:00${typeIn(name, coll, f) === "date-time" ? "" : "Z"}`
|
|
1619
|
+
: v,
|
|
1511
1620
|
]);
|
|
1512
1621
|
rows.push({
|
|
1513
1622
|
...Object.fromEntries(filled),
|
|
1514
|
-
...(
|
|
1623
|
+
...(o.value as object),
|
|
1515
1624
|
id,
|
|
1516
1625
|
...(n ? { [name]: Number(n) } : {}),
|
|
1517
1626
|
});
|
|
@@ -1523,13 +1632,16 @@ export function premised(
|
|
|
1523
1632
|
if (o.op === "remove") rows.splice(rows.indexOf(row), 1);
|
|
1524
1633
|
continue;
|
|
1525
1634
|
}
|
|
1526
|
-
// a field (terms/days, an object's), or an id added to a list (/people
|
|
1635
|
+
// a field (terms/days, an object's), or an id added to a list (/people/-: a list there yet, or a new one) or
|
|
1636
|
+
// taken from it (/people/2: the id 2, wherever it is in the list)
|
|
1527
1637
|
let at: any = row;
|
|
1528
|
-
for (const f of path.slice(0, -1)) at = at[f] ??= {};
|
|
1529
1638
|
const last = path.at(-1)!;
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1639
|
+
path.slice(0, -1).forEach((f, i) => (at = at[f] ??= last === "-" && i === path.length - 2 ? [] : {}));
|
|
1640
|
+
if (o.op === "remove" && Array.isArray(at)) {
|
|
1641
|
+
if (at.includes(Number(last))) at.splice(at.indexOf(Number(last)), 1);
|
|
1642
|
+
} else if (o.op === "remove") delete at[last];
|
|
1643
|
+
else if (last === "-") at.push?.(o.value);
|
|
1644
|
+
else at[last] = o.value;
|
|
1533
1645
|
}
|
|
1534
1646
|
}
|
|
1535
1647
|
return state;
|
|
@@ -1582,7 +1694,9 @@ export function modelOf(stories: Story[], links: Definitions["links"] = {}): Mod
|
|
|
1582
1694
|
return Object.entries(v).forEach(([k, x]) => typed(`${f}.${k}`, x));
|
|
1583
1695
|
}
|
|
1584
1696
|
if (Array.isArray(v) && !v.length) return void later.push({ where, c, f, why: "empty" });
|
|
1585
|
-
|
|
1697
|
+
// ("$n" is an add's id where a link is: a list of ids, or a link to one)
|
|
1698
|
+
const l = links[f];
|
|
1699
|
+
const t = typeOf(v, Array.isArray(v) || (typeof l === "object" && !!l.one));
|
|
1586
1700
|
if (!t) throw new Error(`${where}: ${f} is a string, a boolean, a number or a list of ids`);
|
|
1587
1701
|
const was = c.fields[f];
|
|
1588
1702
|
const now = was ? both(was, t) : t;
|
|
@@ -1642,10 +1756,22 @@ const Pascal = (s: string) =>
|
|
|
1642
1756
|
.split("-")
|
|
1643
1757
|
.map((w) => w[0]!.toUpperCase() + w.slice(1))
|
|
1644
1758
|
.join("");
|
|
1645
|
-
/**
|
|
1759
|
+
/** English's irregular plurals a domain keeps as its words (a dental chart's teeth): each, its singular. */
|
|
1760
|
+
const IRREGULAR: Record<string, string> = {
|
|
1761
|
+
people: "person",
|
|
1762
|
+
teeth: "tooth",
|
|
1763
|
+
feet: "foot",
|
|
1764
|
+
children: "child",
|
|
1765
|
+
men: "man",
|
|
1766
|
+
women: "woman",
|
|
1767
|
+
mice: "mouse",
|
|
1768
|
+
geese: "goose",
|
|
1769
|
+
};
|
|
1770
|
+
/** A collection's item, by English's plurals (its regular ones, and the irregular a domain keeps), or null if the
|
|
1771
|
+
* name isn't one. */
|
|
1646
1772
|
export const itemOf = (coll: string) =>
|
|
1647
|
-
coll
|
|
1648
|
-
?
|
|
1773
|
+
Object.hasOwn(IRREGULAR, coll)
|
|
1774
|
+
? IRREGULAR[coll]!
|
|
1649
1775
|
: /[^aeiou]ies$/.test(coll)
|
|
1650
1776
|
? coll.slice(0, -3) + "y"
|
|
1651
1777
|
: /(ss|x|ch|sh)es$/.test(coll)
|
|
@@ -1700,11 +1826,7 @@ export function compile(
|
|
|
1700
1826
|
...(c.own ? { own: true } : {}),
|
|
1701
1827
|
fields: c.fields,
|
|
1702
1828
|
needs: c.required,
|
|
1703
|
-
links:
|
|
1704
|
-
Object.entries(c.fields)
|
|
1705
|
-
.filter(([f, t]) => t === "ids" || (t === "integer" && link(f).one))
|
|
1706
|
-
.map(([f]) => [f, link(f).to]),
|
|
1707
|
-
),
|
|
1829
|
+
links: linksIn(c, link),
|
|
1708
1830
|
// what each field pops, tapped where a page says it (docs/design.md, "A peek"): a value with a form, that
|
|
1709
1831
|
// form's view; a reference, the collection it points at. A plain value pops nothing, nor a link that is
|
|
1710
1832
|
// structure (part), which a page places the item by
|
|
@@ -1721,6 +1843,7 @@ export function compile(
|
|
|
1721
1843
|
...goes(c, link, doc),
|
|
1722
1844
|
...(defs.order?.[name] ? { order: defs.order[name] } : {}),
|
|
1723
1845
|
...(c.fills ? { fills: c.fills } : {}),
|
|
1846
|
+
...(c.clock ? { clock: c.clock } : {}),
|
|
1724
1847
|
...(c.timed ? { timed: c.timed } : {}),
|
|
1725
1848
|
...(c.copies ? { copies: c.copies } : {}),
|
|
1726
1849
|
},
|
|
@@ -1764,7 +1887,7 @@ export const member = true;
|
|
|
1764
1887
|
: ""
|
|
1765
1888
|
}/** The parts of ${doc} a page may hold alone, by its query's parameters (its definitions' window): null, none. */
|
|
1766
1889
|
export const window = ${JSON.stringify(defs.window ?? null)};
|
|
1767
|
-
${defs.window && !defs.where?.[doc] ? windowCode(defs.window) : ""}${defs.moves ? `/** Each field whose value moves only as the stories move it, each step by its roles (its definitions' moves). */\nexport const moves = ${JSON.stringify(defs.moves)};\n` : ""}${defs.guests ? `/** The field holding an item's own code: a page holding ${doc} by it is the item's guest (its definitions' guests). */\nexport const guests = ${JSON.stringify(defs.guests)};\n` : ""}${settingsCode(doc, defs)}${defs.visitors ? `/** The field of its kind's root holding the level its guest is, the role guest: that level (its definitions' visitors). */\nexport const visitors = ${JSON.stringify(defs.visitors)};\n` : ""}${derivedFrom ? "" : `/** The document as one statement over its tables (generated/${prefix}queries.ts). */\nexport { query, postgres${readOnly ? "" : ", items, triggers"} } from "./${prefix}queries.ts";\n`}${derivedFrom ? `/** The documents this one is derived from (its derive.ts): what the server derives it again on a write to. */\nexport const derived = ${JSON.stringify(derivedFrom)};\n` : ""}`;
|
|
1890
|
+
${defs.window && !defs.where?.[doc] ? windowCode(defs.window) : ""}${namesCode(defs, Object.values(collections))}${defs.moves ? `/** Each field whose value moves only as the stories move it, each step by its roles (its definitions' moves). */\nexport const moves = ${JSON.stringify(defs.moves)};\n` : ""}${defs.guests ? `/** The field holding an item's own code: a page holding ${doc} by it is the item's guest (its definitions' guests). */\nexport const guests = ${JSON.stringify(defs.guests)};\n` : ""}${settingsCode(doc, defs)}${defs.visitors ? `/** The field of its kind's root holding the level its guest is, the role guest: that level (its definitions' visitors). */\nexport const visitors = ${JSON.stringify(defs.visitors)};\n` : ""}${derivedFrom ? "" : `/** The document as one statement over its tables (generated/${prefix}queries.ts). */\nexport { query, postgres${readOnly ? "" : ", items, triggers"} } from "./${prefix}queries.ts";\n`}${derivedFrom ? `/** The documents this one is derived from (its derive.ts): what the server derives it again on a write to. */\nexport const derived = ${JSON.stringify(derivedFrom)};\n` : ""}`;
|
|
1768
1891
|
let elements = `${HEAD}
|
|
1769
1892
|
// Each collection is an element, defined here: on a page, it holds the document, and its items
|
|
1770
1893
|
// are put inside it. Each item is an element, defined by the page: as it is, or by a view that
|
|
@@ -1819,10 +1942,10 @@ const send = (from: Element, ops: object[], why?: string) => new Promise<Answer>
|
|
|
1819
1942
|
.map(([f]) => JSON.stringify(f));
|
|
1820
1943
|
const gone = fields.filter(([f]) => c.required.includes(f) && cleared(f)).map(([f]) => JSON.stringify(f));
|
|
1821
1944
|
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(" | ")}>>` : ""}`;
|
|
1822
|
-
// what an add sends, as the stories write it: a dotted field nested ({ terms: { days } }), and a link to one item
|
|
1823
|
-
// may name an add before it in the same write ("$1", the id the store will give it)
|
|
1945
|
+
// what an add sends, as the stories write it: a dotted field nested ({ terms: { days } }), and a link, to one item
|
|
1946
|
+
// or a list's, may name an add before it in the same write ("$1", the id the store will give it)
|
|
1824
1947
|
const sent = (f: string, t: Type) =>
|
|
1825
|
-
`${JSON.stringify(f)}${c.required.includes(f) ? "" : "?"}: ${link(f).one ? REF : tsOf(t)}`;
|
|
1948
|
+
`${JSON.stringify(f)}${c.required.includes(f) ? "" : "?"}: ${link(f).one ? REF : t === "ids" ? `(${REF})[]` : tsOf(t)}`;
|
|
1826
1949
|
const heads = [...new Set(fields.map(([f]) => f.split(".")[0]!))];
|
|
1827
1950
|
const value = heads
|
|
1828
1951
|
.map((h) => {
|
|
@@ -1931,10 +2054,18 @@ export function rowsOf(
|
|
|
1931
2054
|
f in (collections[coll]!.fills ?? {}) ||
|
|
1932
2055
|
!!collections[coll]!.copies?.fields.includes(f) ||
|
|
1933
2056
|
f in (defs.totals?.[coll] ?? {});
|
|
1934
|
-
|
|
1935
|
-
|
|
2057
|
+
// what names an item: the field its title says (definitions' title), else its first field of words
|
|
2058
|
+
const titleOf = (coll: string) => {
|
|
2059
|
+
const said = defs.title?.[coll];
|
|
2060
|
+
if (said !== undefined) {
|
|
2061
|
+
if (!STRINGS.includes(collections[coll]!.fields[said] as Type))
|
|
2062
|
+
throw new Error(`${doc}: title names ${coll}' ${said}, which isn't a field of words its stories give`);
|
|
2063
|
+
return said;
|
|
2064
|
+
}
|
|
2065
|
+
return [...collections[coll]!.required, ...Object.keys(collections[coll]!.fields)].find(
|
|
1936
2066
|
(f) => collections[coll]!.fields[f] === "string" && !filled(coll, f) && defs.moves?.[coll] !== f,
|
|
1937
2067
|
);
|
|
2068
|
+
};
|
|
1938
2069
|
// a link to one that an add needs, to a collection of this document: the item it's of, and drawn under
|
|
1939
2070
|
const underOf = (name: string) =>
|
|
1940
2071
|
collections[name]!.required.filter((f) => collections[name]!.fields[f] === "integer" && link(f).one)
|
|
@@ -2182,6 +2313,10 @@ export async function compiled(dir = APPS) {
|
|
|
2182
2313
|
defined: Definitions = implied(Object.assign({}, ...header));
|
|
2183
2314
|
const bases = [...new Set(told.map((s) => (s.does ?? s.cannot)!.doc.split(/[:?]/)[0]!))];
|
|
2184
2315
|
const several = bases.length > 1 || !!defined.documents;
|
|
2316
|
+
// (an app's items are in any of its documents' seeds)
|
|
2317
|
+
const seedsOf = await Promise.all((several ? bases : [app]).map((u) => seedOf(`${at}/${u}.json`)));
|
|
2318
|
+
const seedIn = (coll: string) =>
|
|
2319
|
+
seedsOf.flatMap((s) => (Array.isArray(s[coll]) ? (s[coll] as Record<string, unknown>[]) : []));
|
|
2185
2320
|
for (const unit of several ? bases : [app]) {
|
|
2186
2321
|
dirs[unit] = at;
|
|
2187
2322
|
if (several) split.add(unit);
|
|
@@ -2191,7 +2326,9 @@ export async function compiled(dir = APPS) {
|
|
|
2191
2326
|
defs[unit] = several ? definedFor(defined, models[unit]!, stories[unit]!) : defined;
|
|
2192
2327
|
if (defs[unit]!.kind && defs[unit]!.kind!.root in models[unit]!.collections)
|
|
2193
2328
|
throw new Error(`${app}'s stories: a kind's root, ${defs[unit]!.kind!.root}, is another document's collection`);
|
|
2194
|
-
|
|
2329
|
+
if (defs[unit]!.kind?.files !== undefined && defs[unit]!.kind!.files !== "apart")
|
|
2330
|
+
throw new Error(`${app}'s stories: a kind's files are "apart", or not said`);
|
|
2331
|
+
filled(models[unit]!, defs[unit]!, `${app}'s definitions`, seedIn);
|
|
2195
2332
|
}
|
|
2196
2333
|
// a link's name is the link's in every document the app tells (a race's crossings' boat, and an entry's)
|
|
2197
2334
|
if (several)
|
|
@@ -2224,6 +2361,24 @@ export async function compiled(dir = APPS) {
|
|
|
2224
2361
|
seeds[m.doc] = await seedOf(file);
|
|
2225
2362
|
seeded(m, seeds[m.doc]!, relative(process.cwd(), file));
|
|
2226
2363
|
}
|
|
2364
|
+
// a wall clock filled "now" is on the clock of a zone, said by the field named for it: the item's own, its kind's
|
|
2365
|
+
// root's, the document's own
|
|
2366
|
+
for (const [app, m] of Object.entries(models))
|
|
2367
|
+
for (const [coll, c] of Object.entries(m.collections)) {
|
|
2368
|
+
const f = Object.entries(c.fills ?? {}).find(([f, v]) => v === "now" && c.fields[f] === "date-time")?.[0];
|
|
2369
|
+
if (!f) continue;
|
|
2370
|
+
const root = defs[app]!.kind?.root,
|
|
2371
|
+
says = (x?: Coll) => x?.fields.zone === "zone";
|
|
2372
|
+
c.clock = [
|
|
2373
|
+
...(says(c) ? ["item" as const] : []),
|
|
2374
|
+
...(root && Object.values(models).some((o) => says(o.collections[root])) ? ["root" as const] : []),
|
|
2375
|
+
...(Object.values(m.collections).some((o) => o.own && says(o)) ? ["document" as const] : []),
|
|
2376
|
+
];
|
|
2377
|
+
if (!c.clock.length)
|
|
2378
|
+
throw new Error(
|
|
2379
|
+
`${app}'s stories: ${coll}' ${f} is filled "now" as a wall clock, so a zone is said: a field named zone, a time zone, of the item, of its kind's root or of the document's own`,
|
|
2380
|
+
);
|
|
2381
|
+
}
|
|
2227
2382
|
// a copy: each field taken from what a one-id link points at, its type that field's, once, when it's added
|
|
2228
2383
|
for (const [app, d] of Object.entries(defs))
|
|
2229
2384
|
for (const [coll, { from, fields }] of Object.entries(d.copies ?? {})) {
|
|
@@ -2268,12 +2423,20 @@ export async function compiled(dir = APPS) {
|
|
|
2268
2423
|
}
|
|
2269
2424
|
const members = (seeds.members?.members ?? []) as { id: number; name: string }[];
|
|
2270
2425
|
// an element's name is one per page: no two documents have a collection of one name
|
|
2271
|
-
// across apps; an app's documents may each hold one of its tables (the till's orders, the kitchen's)
|
|
2426
|
+
// across apps; an app's documents may each hold one of its tables (the till's orders, the kitchen's); and two
|
|
2427
|
+
// derived documents may each derive one kind of item, its fields the same, one element for both
|
|
2272
2428
|
const held: Record<string, string> = {};
|
|
2273
2429
|
for (const [app, m] of Object.entries(models).sort(([a], [b]) => a.localeCompare(b)))
|
|
2274
2430
|
for (const c of Object.keys(m.collections)) {
|
|
2275
|
-
|
|
2276
|
-
|
|
2431
|
+
const other = held[c],
|
|
2432
|
+
derived = !!other && !!derivedFrom[app] && !!derivedFrom[other],
|
|
2433
|
+
alike =
|
|
2434
|
+
derived &&
|
|
2435
|
+
JSON.stringify(models[other!]!.collections[c]!.fields) === JSON.stringify(m.collections[c]!.fields);
|
|
2436
|
+
if (other && dirs[other] !== dirs[app] && !alike)
|
|
2437
|
+
throw new Error(
|
|
2438
|
+
`${other} and ${app} both have a collection named ${c}${derived ? ": two derived documents share one only where its items are the same, field for field" : ""}`,
|
|
2439
|
+
);
|
|
2277
2440
|
held[c] ??= app;
|
|
2278
2441
|
}
|
|
2279
2442
|
/** Which other document holds each collection an app's document doesn't. */
|
|
@@ -2303,11 +2466,12 @@ export async function compiled(dir = APPS) {
|
|
|
2303
2466
|
);
|
|
2304
2467
|
};
|
|
2305
2468
|
/** Each document's grants, and who its tellers are on the seeds: worked out once. */
|
|
2306
|
-
const granted: Record<string, { grants: Record<string, string[]>; rolesOfTeller: (s: Story) => string[] }> =
|
|
2469
|
+
const granted: Record<string, { grants: Record<string, string[]>; rolesOfTeller: (s: Story, o?: Op) => string[] }> =
|
|
2470
|
+
{};
|
|
2307
2471
|
const grantsFor = (app: string) => {
|
|
2308
2472
|
if (granted[app]) return granted[app]!;
|
|
2309
2473
|
const roles = Object.entries(defs[app]!.roles ?? {}).map(
|
|
2310
|
-
([r, path]) => [r, whoFn(whoCode(path, []
|
|
2474
|
+
([r, path]) => [r, whoFn(whoCode(path, defs[app]!.roles!, app, !!defs[app]!.kind, [r]))] as const,
|
|
2311
2475
|
);
|
|
2312
2476
|
const memberOf = (name: string) => {
|
|
2313
2477
|
const as = name in casts[app]! ? casts[app]![name] : name;
|
|
@@ -2345,6 +2509,19 @@ export async function compiled(dir = APPS) {
|
|
|
2345
2509
|
// writes the parts before its own (src/together.ts), which name its document: once a story (none, no premise: the
|
|
2346
2510
|
// seeds)
|
|
2347
2511
|
const fillsOf = (doc: string, coll: string) => defs[doc]?.fills?.[coll];
|
|
2512
|
+
const typeIn = (doc: string, coll: string, f: string) => models[doc]?.collections[coll]?.fields[f];
|
|
2513
|
+
// a document's collections, each its links: where a write's "$n" is read
|
|
2514
|
+
const collectionsOf = (doc: string): Linked | undefined => {
|
|
2515
|
+
const m = models[doc];
|
|
2516
|
+
if (!m) return undefined;
|
|
2517
|
+
const link = linksOf(m, defs[doc]!, {}, doc);
|
|
2518
|
+
return Object.fromEntries(
|
|
2519
|
+
Object.entries(m.collections).map(([c, x]) => [
|
|
2520
|
+
c,
|
|
2521
|
+
{ links: linksIn(x, link), ...(x.own ? { own: true } : {}) },
|
|
2522
|
+
]),
|
|
2523
|
+
);
|
|
2524
|
+
};
|
|
2348
2525
|
const states = new WeakMap<Story, ReturnType<typeof writtenBefore>>();
|
|
2349
2526
|
const writtenOf = (s: Story = { story: "" }) =>
|
|
2350
2527
|
states.get(s) ??
|
|
@@ -2353,9 +2530,11 @@ export async function compiled(dir = APPS) {
|
|
|
2353
2530
|
s,
|
|
2354
2531
|
writtenBefore(
|
|
2355
2532
|
s,
|
|
2356
|
-
premised(seeds, s.premise, fillsOf, memberOf),
|
|
2357
|
-
(state, w) =>
|
|
2533
|
+
premised(seeds, s.premise, fillsOf, memberOf, collectionsOf, typeIn),
|
|
2534
|
+
(state, w) =>
|
|
2535
|
+
premised(state, s.as === undefined ? [w] : { [s.as]: [w] }, fillsOf, memberOf, collectionsOf, typeIn),
|
|
2358
2536
|
(name) => defs[name]?.kind?.root,
|
|
2537
|
+
collectionsOf,
|
|
2359
2538
|
),
|
|
2360
2539
|
)
|
|
2361
2540
|
.get(s)!;
|
|
@@ -2368,9 +2547,7 @@ export async function compiled(dir = APPS) {
|
|
|
2368
2547
|
const named =
|
|
2369
2548
|
m === null
|
|
2370
2549
|
? []
|
|
2371
|
-
: roles
|
|
2372
|
-
.filter(([, is]) => is(m, readIn(state), [], at, d, k ? itemOf(d, o, state) : undefined))
|
|
2373
|
-
.map(([r]) => r);
|
|
2550
|
+
: roles.filter(([, is]) => is(m, readIn(state), at, d, k ? itemOf(d, o, state) : undefined)).map(([r]) => r);
|
|
2374
2551
|
// a guest: a page holding the document by the item's own code, or in a kind, its root's (as the runtime asks)
|
|
2375
2552
|
const root = k && guests?.[k.root],
|
|
2376
2553
|
code = o && (root || guests?.[parts(o.path).coll]),
|
|
@@ -2392,12 +2569,13 @@ export async function compiled(dir = APPS) {
|
|
|
2392
2569
|
: grantOf(o);
|
|
2393
2570
|
};
|
|
2394
2571
|
const grants = grantsOf(stories[app]!, rolesOf, memberOf, keyOf);
|
|
2395
|
-
|
|
2572
|
+
// who a story's teller is, to the item an op is to
|
|
2573
|
+
const rolesOfTeller = (s: Story, o?: Op) => {
|
|
2396
2574
|
const w = (s.does ?? s.cannot)!;
|
|
2397
2575
|
// an anonymous story's teller: a guest, where it holds the document by a code (as its first op finds it)
|
|
2398
|
-
if (s.as === undefined) return rolesOf(null, w.doc, w.ops[0], s);
|
|
2576
|
+
if (s.as === undefined) return rolesOf(null, w.doc, o ?? w.ops[0], s);
|
|
2399
2577
|
const m = memberOf(s.as);
|
|
2400
|
-
return m === undefined ? [] : rolesOf(m, w.doc,
|
|
2578
|
+
return m === undefined ? [] : rolesOf(m, w.doc, o, s);
|
|
2401
2579
|
};
|
|
2402
2580
|
return (granted[app] = { grants, rolesOfTeller });
|
|
2403
2581
|
};
|
|
@@ -2501,8 +2679,8 @@ export async function compiled(dir = APPS) {
|
|
|
2501
2679
|
/** A who-path's code as a function, to find who the stories' tellers are on the seeds as it compiles. */
|
|
2502
2680
|
const whoFn = (code: string) =>
|
|
2503
2681
|
new Function(
|
|
2504
|
-
`${new Bun.Transpiler({ loader: "ts" }).transformSync(`const is = (m: number, read: any,
|
|
2505
|
-
)() as (m: number, read: (doc: string) => any,
|
|
2682
|
+
`${new Bun.Transpiler({ loader: "ts" }).transformSync(`const is = (m: number, read: any, item: any, doc: string, at?: any) => ${code};`)} return is;`,
|
|
2683
|
+
)() as (m: number, read: (doc: string) => any, item: unknown, doc: string, at?: unknown) => boolean;
|
|
2506
2684
|
|
|
2507
2685
|
/** An app's seed, where its stories start (none: nothing seeded). */
|
|
2508
2686
|
const seedOf = async (file: string) => ((await Bun.file(file).exists()) ? await Bun.file(file).json() : {});
|