viewkitjs 0.19.0 → 0.20.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.
Files changed (85) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/MIGRATION.md +116 -0
  3. package/README.md +7 -3
  4. package/fields/README.md +3 -3
  5. package/fields/dist/derived.d.ts +4 -0
  6. package/fields/dist/derived.js +39 -0
  7. package/fields/dist/entities.js +30 -30
  8. package/fields/dist/formula.js +2 -2
  9. package/fields/dist/query/evaluate.js +5 -5
  10. package/fields/dist/query/figures.js +5 -5
  11. package/fields/dist/query/filter.d.ts +15 -0
  12. package/fields/dist/query/filter.js +85 -0
  13. package/fields/dist/query/hold.js +2 -2
  14. package/fields/dist/query/option-counts.js +2 -2
  15. package/fields/dist/query/read-rule.js +2 -2
  16. package/fields/dist/query/search.js +2 -2
  17. package/fields/dist/query/set-aside.js +4 -4
  18. package/fields/dist/query/settings.d.ts +2 -15
  19. package/fields/dist/query/settings.js +3 -81
  20. package/fields/dist/query/sort.js +4 -4
  21. package/fields/dist/registry.d.ts +19 -5
  22. package/fields/dist/registry.js +371 -50
  23. package/fields/dist/types.d.ts +1 -1
  24. package/fields/docs/getting-started.md +2 -2
  25. package/fields/docs/guides/checking-definitions.md +17 -4
  26. package/fields/docs/guides/derived-fields.md +5 -5
  27. package/fields/docs/guides/queries.md +1 -1
  28. package/fields/docs/reference/design-category-colors.md +1 -1
  29. package/fields/docs/reference/design-status-slots.md +1 -1
  30. package/fields/docs/reference/fields.md +3 -3
  31. package/fields/docs/reference/query.md +1 -0
  32. package/fields/skills/viewkit/SKILL.md +2 -2
  33. package/package.json +13 -6
  34. package/postgres/README.md +2 -2
  35. package/postgres/dist/access.d.ts +12 -4
  36. package/postgres/dist/access.js +3 -0
  37. package/postgres/dist/calendar.js +3 -2
  38. package/postgres/dist/check.d.ts +6 -4
  39. package/postgres/dist/check.js +27 -16
  40. package/postgres/dist/counts.js +3 -2
  41. package/postgres/dist/definitions/build.js +7 -0
  42. package/postgres/dist/definitions/formula.d.ts +1 -1
  43. package/postgres/dist/definitions/formula.js +5 -3
  44. package/postgres/dist/definitions/lookup.js +5 -1
  45. package/postgres/dist/definitions/money.js +6 -1
  46. package/postgres/dist/definitions/place.d.ts +7 -0
  47. package/postgres/dist/definitions/place.js +8 -0
  48. package/postgres/dist/definitions/relations.js +36 -3
  49. package/postgres/dist/definitions/rollup.d.ts +1 -1
  50. package/postgres/dist/definitions/rollup.js +11 -6
  51. package/postgres/dist/field-values.js +11 -8
  52. package/postgres/dist/figures.js +4 -2
  53. package/postgres/dist/filled.js +2 -2
  54. package/postgres/dist/groups.d.ts +2 -1
  55. package/postgres/dist/groups.js +5 -3
  56. package/postgres/dist/index.d.ts +1 -0
  57. package/postgres/dist/index.js +1 -0
  58. package/postgres/dist/kept.js +8 -4
  59. package/postgres/dist/lanes.js +5 -3
  60. package/postgres/dist/phone.js +2 -2
  61. package/postgres/dist/predicates.js +2 -2
  62. package/postgres/dist/readable.d.ts +40 -0
  63. package/postgres/dist/readable.js +84 -0
  64. package/postgres/dist/records.js +7 -4
  65. package/postgres/dist/refusals.d.ts +8 -1
  66. package/postgres/dist/refusals.js +7 -0
  67. package/postgres/dist/relations.js +5 -4
  68. package/postgres/dist/search-indexes.js +2 -1
  69. package/postgres/dist/search.js +6 -4
  70. package/postgres/dist/several.js +8 -8
  71. package/postgres/dist/sort.d.ts +5 -3
  72. package/postgres/dist/sort.js +13 -8
  73. package/postgres/dist/value-rows.js +2 -2
  74. package/postgres/dist/values.js +4 -2
  75. package/postgres/docs/guides/definitions.md +2 -2
  76. package/postgres/docs/guides/field-map.md +43 -18
  77. package/postgres/docs/guides/test-kit.md +2 -0
  78. package/postgres/docs/reference/postgres.md +10 -4
  79. package/views/README.md +3 -2
  80. package/views/dist/styles.css +3165 -0
  81. package/views/dist/styles.unlayered.css +3164 -0
  82. package/views/docs/contract.md +13 -7
  83. package/views/docs/getting-started.md +1 -1
  84. package/views/docs/guides/the-grid.md +1 -1
  85. package/views/docs/install.md +35 -8
@@ -18,10 +18,11 @@ export const placeRelation = (at) => {
18
18
  return { leftOut: RELATION_REASONS.noStorage(farKind) };
19
19
  const key = at.relationKey(table, def.key);
20
20
  const form = Object.hasOwn(kind.columns, def.key) ? kind.columns[def.key] : def.key;
21
- const placed = (map, reads) => ({
21
+ const placed = (map, reads, fields) => ({
22
22
  mapping: { value: { relation: key }, stored: far.keyStored },
23
23
  reads,
24
24
  relation: { key, map },
25
+ ...(fields === undefined ? {} : { fields }),
25
26
  });
26
27
  if (def.cardinality !== "many") {
27
28
  const column = form;
@@ -30,10 +31,13 @@ export const placeRelation = (at) => {
30
31
  if (typeof form === "object" && "through" in form) {
31
32
  const to = at.farTable(farKind);
32
33
  const link = at.farTable(form.through.kind);
34
+ const linkKind = Object.hasOwn(input.kinds, form.through.kind) ? input.kinds[form.through.kind] : undefined;
35
+ const ends = [form.through.from, form.through.to].flatMap((column) => fieldsKeptIn(linkKind, column).map((key) => ({ table: link, key })));
36
+ const read = [...ends, ...ownReads(def, to)];
33
37
  return placed({ from: table, to, through: { table: link, from: form.through.from, to: form.through.to } }, [
34
38
  { table: link, column: form.through.from },
35
39
  { table: link, column: form.through.to },
36
- ]);
40
+ ], read.length > 0 ? read : undefined);
37
41
  }
38
42
  if (def.inverse === undefined)
39
43
  return { leftOut: RELATION_REASONS.noInverse };
@@ -44,5 +48,34 @@ export const placeRelation = (at) => {
44
48
  if (!inverseOfOne || typeof back !== "string" || far.notKept.has(inverseField))
45
49
  return { leftOut: RELATION_REASONS.inverseNotColumn };
46
50
  const to = at.farTable(farKind);
47
- return placed({ from: table, to, back }, [{ table: to, column: back }]);
51
+ return placed({ from: table, to, back }, [{ table: to, column: back }], [{ table: to, key: inverseField }, ...ownReads(def, to)]);
48
52
  };
53
+ function fieldsKeptIn(kind, column) {
54
+ if (kind === undefined || kind.entity === undefined)
55
+ return [];
56
+ return Object.keys(kind.entity.fields).filter((key) => {
57
+ if (kind.notKept.has(key))
58
+ return false;
59
+ const form = Object.hasOwn(kind.columns, key) ? kind.columns[key] : key;
60
+ const kept = typeof form === "string" ? form : typeof form === "object" && form !== null ? Reflect.get(form, "column") : undefined;
61
+ return kept === column;
62
+ });
63
+ }
64
+ function ownReads(def, to) {
65
+ const keys = new Set();
66
+ const walk = (node) => {
67
+ if (typeof node !== "object" || node === null)
68
+ return;
69
+ const key = Reflect.get(node, "fieldKey");
70
+ if (typeof key === "string")
71
+ keys.add(key);
72
+ const items = Reflect.get(node, "items");
73
+ if (Array.isArray(items))
74
+ items.forEach(walk);
75
+ };
76
+ walk(Reflect.get(def, "where"));
77
+ const within = Reflect.get(def, "within");
78
+ if (typeof within === "string")
79
+ keys.add(within);
80
+ return [...keys].map((key) => ({ table: to, key }));
81
+ }
@@ -1,4 +1,4 @@
1
- import type { Place } from "./place.js";
1
+ import { type Place } from "./place.js";
2
2
  /** Why a rollup is left out. */
3
3
  export declare const ROLLUP_REASONS: {
4
4
  readonly breakdown: "a rollup that breaks down by an option or a time is not built yet";
@@ -4,6 +4,7 @@ import { checkFarWhere } from "../check.js";
4
4
  import { farOrderSql } from "../sort.js";
5
5
  import { join, sql, TRUE } from "../sql.js";
6
6
  import { hasParts, mappingReadsZone, oneKeyOf, readsWhole, readsZone, valueSql } from "../values.js";
7
+ import { workedOut } from "./place.js";
7
8
  export const ROLLUP_REASONS = {
8
9
  breakdown: "a rollup that breaks down by an option or a time is not built yet",
9
10
  range: "a rollup that follows a page's range is not built yet",
@@ -121,9 +122,11 @@ export const placeRollup = (at) => {
121
122
  const hops = typeof def.over === "string" ? [def.over] : def.over;
122
123
  const reads = [];
123
124
  const relations = [];
125
+ const fields = [];
124
126
  let table = at.table;
125
127
  let kind = at.kind;
126
128
  for (const hop of hops) {
129
+ fields.push({ table, key: hop });
127
130
  const placed = at.placed(table, hop);
128
131
  if ("leftOut" in placed)
129
132
  return { leftOut: ROLLUP_REASONS.hop(hop, placed.leftOut) };
@@ -157,6 +160,7 @@ export const placeRollup = (at) => {
157
160
  into = () => fixed;
158
161
  }
159
162
  else if (typeof code.field === "string") {
163
+ fields.push({ table: at.table, key: code.field });
160
164
  const own = at.placed(at.table, code.field);
161
165
  if ("leftOut" in own)
162
166
  return { leftOut: ROLLUP_REASONS.farField(code.field, own.leftOut) };
@@ -179,6 +183,7 @@ export const placeRollup = (at) => {
179
183
  if ("leftOut" in placed)
180
184
  return { leftOut: ROLLUP_REASONS.farField(key, placed.leftOut) };
181
185
  mappings.set(key, placed.mapping);
186
+ fields.push({ table: farTable, key });
182
187
  relationOf.set(key, "relation" in placed ? placed.relation : undefined);
183
188
  if (key === of && into !== undefined)
184
189
  convertOf = placed.convert;
@@ -240,10 +245,10 @@ export const placeRollup = (at) => {
240
245
  if (def.where === undefined)
241
246
  return [];
242
247
  const path = `${at.table === scope.map.table ? `fields.${def.key}` : `tables.${at.table}.fields.${def.key}`}.where`;
243
- const checked = checkFarWhere(scope.map, farTable, def.where, path, scope.context);
248
+ const checked = checkFarWhere(scope, farTable, def.where, path, scope.context);
244
249
  return checked === null ? [] : [scope.walk(far, checked)];
245
250
  };
246
- const value = (row) => {
251
+ const value = workedOut((row) => {
247
252
  const { scope, near } = nearOf(row);
248
253
  const far = open(scope, farTable, "f");
249
254
  const conditions = [reachedSql(scope, near, relations, far), ...ownWhere(scope, far)];
@@ -280,10 +285,10 @@ export const placeRollup = (at) => {
280
285
  default:
281
286
  throw new TypeError(`${def.key}: ${String(fn)} is no rollup function`);
282
287
  }
283
- };
288
+ });
284
289
  const whole = !WHOLE.has(fn) || into !== undefined
285
290
  ? undefined
286
- : (row) => {
291
+ : workedOut((row) => {
287
292
  const { scope, near } = nearOf(row);
288
293
  const { made, read } = joinWorkedOut(near, `\u0000rollup\u0000${def.key}`, (named) => {
289
294
  const far = open(scope, farTable, "f");
@@ -327,7 +332,7 @@ export const placeRollup = (at) => {
327
332
  default:
328
333
  return read(sql `${made.n}`);
329
334
  }
330
- };
335
+ });
331
336
  if (whole !== undefined)
332
337
  readsWhole(value, whole);
333
338
  const seen = new Set();
@@ -339,5 +344,5 @@ export const placeRollup = (at) => {
339
344
  readsZone(value);
340
345
  readsZone(whole);
341
346
  }
342
- return { mapping: { value, sort: whole ?? value, stored }, reads: columns };
347
+ return { mapping: { value, sort: whole ?? value, stored }, reads: columns, fields };
343
348
  };
@@ -2,6 +2,7 @@ import { isOpenVocabulary, presentedDef } from "viewkitjs/fields";
2
2
  import { kept } from "./kept.js";
3
3
  import { farKeys, keyText, oneRelationValues, rowKeys } from "./keyed.js";
4
4
  import { fieldsOf } from "./map.js";
5
+ import { readableField } from "./readable.js";
5
6
  import { REFUSALS } from "./refusals.js";
6
7
  import { render, sql, TRUE } from "./sql.js";
7
8
  export const FIELD_VALUES_LIMIT = 1000;
@@ -12,15 +13,15 @@ function offered(field, at) {
12
13
  if (!open && !NAMED.has(field.type))
13
14
  throw REFUSALS.noValues(at);
14
15
  }
15
- function farPathOf(map, path) {
16
+ function farPathOf(reader, path) {
17
+ const { map } = reader;
16
18
  const parts = path.split(PATH_SEPARATOR);
17
19
  if (parts.length < 2 || parts.length > 3)
18
20
  throw REFUSALS.unknownField(path);
19
21
  const hops = parts.slice(0, -1);
20
22
  let table = map.table;
21
23
  for (const hop of hops) {
22
- const own = fieldsOf(map, table);
23
- const field = own !== undefined && Object.hasOwn(own.fields, hop) ? own.fields[hop] : undefined;
24
+ const field = readableField(reader, table, hop);
24
25
  const value = field?.mapping.value;
25
26
  if (field === undefined || field.type !== "relation" || typeof value !== "object" || value === null)
26
27
  throw REFUSALS.unknownField(path);
@@ -30,16 +31,18 @@ function farPathOf(map, path) {
30
31
  const far = fieldsOf(map, table);
31
32
  if (far === undefined)
32
33
  throw REFUSALS.noFields(path, table);
33
- if (!Object.hasOwn(far.fields, farKey))
34
+ const field = readableField(reader, table, farKey);
35
+ if (field === undefined)
34
36
  throw REFUSALS.unknownFarField(table, farKey);
35
- return { hops, farKey, field: far.fields[farKey] };
37
+ return { hops, farKey, field };
36
38
  }
37
39
  export function fieldValuesQuery(request, fieldKey) {
38
40
  const { scope, main, where } = kept(request);
39
41
  const filtered = where === TRUE ? sql `` : sql ` WHERE ${where}`;
40
42
  let keys;
41
- if (Object.hasOwn(scope.map.fields, fieldKey)) {
42
- const field = scope.map.fields[fieldKey];
43
+ const own = readableField(scope, scope.map.table, fieldKey);
44
+ if (own !== undefined) {
45
+ const field = own;
43
46
  offered(field, fieldKey);
44
47
  const one = oneRelationValues(scope, main, fieldKey, field);
45
48
  if (one !== null)
@@ -47,7 +50,7 @@ export function fieldValuesQuery(request, fieldKey) {
47
50
  keys = rowKeys(scope, main, fieldKey, field, "count");
48
51
  }
49
52
  else {
50
- const { hops, farKey, field } = farPathOf(scope.map, fieldKey);
53
+ const { hops, farKey, field } = farPathOf(scope, fieldKey);
51
54
  offered(field, fieldKey);
52
55
  keys = farKeys(scope, main, hops, farKey, field);
53
56
  }
@@ -7,6 +7,7 @@ import { numberValue } from "./rules.js";
7
7
  import { join, render, sql, TRUE } from "./sql.js";
8
8
  import { countOf } from "./steps.js";
9
9
  import { valueSql } from "./values.js";
10
+ import { readableField } from "./readable.js";
10
11
  const NUMBERS = new Set(["number", "percent", "currency"]);
11
12
  const numberOf = (value) => (value == null ? null : Number(String(value)));
12
13
  const sumOf = (value) => {
@@ -31,9 +32,10 @@ const sumTerm = (x) => {
31
32
  export function figureColumns(scope, main, keys) {
32
33
  const asked = [...new Set(keys)];
33
34
  const values = asked.map((key) => {
34
- if (!Object.hasOwn(scope.map.fields, key))
35
+ const field = readableField(scope, scope.map.table, key);
36
+ if (field === undefined)
35
37
  throw REFUSALS.unknownField(key);
36
- const { def, mapping } = scope.map.fields[key];
38
+ const { def, mapping } = field;
37
39
  const filled = sql `count(CASE WHEN ${filledSql(scope, main, key)} THEN 1 END)`;
38
40
  if (!NUMBERS.has(valueTypeOf(def)))
39
41
  return { sum: sql `NULL::text`, count: sql `0::bigint`, min: sql `NULL::text`, max: sql `NULL::text`, filled };
@@ -1,17 +1,17 @@
1
1
  import { valueTypeOf } from "viewkitjs/fields";
2
2
  import { actorKeySql } from "./actor.js";
3
3
  import { trimmedSql } from "./fold.js";
4
- import { fieldsOf } from "./map.js";
5
4
  import { nameSql, rangeSql } from "./parts.js";
6
5
  import { phoneAt, phoneRuleSql } from "./phone.js";
7
6
  import { manyOptionSql } from "./relations.js";
8
7
  import { severalSql } from "./several.js";
9
8
  import { join, sql } from "./sql.js";
10
9
  import { partSql, readSql, valueSql, wholeOf } from "./values.js";
10
+ import { readableField } from "./readable.js";
11
11
  const PLACE_PARTS = ["line1", "line2", "postalCode", "city", "region", "countryCode"];
12
12
  const asItIs = (value, stored = "text") => stored === "text" ? sql `(NULLIF((${value})::text, '') IS NOT NULL)` : sql `((${value}) IS NOT NULL)`;
13
13
  export function filledSql(scope, at, key) {
14
- const { def, mapping } = fieldsOf(scope.map, at.table).fields[key];
14
+ const { def, mapping } = readableField(scope, at.table, key);
15
15
  const type = valueTypeOf(def);
16
16
  const checked = {
17
17
  key,
@@ -59,7 +59,8 @@ type Held = {
59
59
  readonly far?: FarOrder;
60
60
  };
61
61
  /** The group field of `request`, held to what this package answers; refused by name where it can't answer yet. A
62
- * set-aside group (fields' `readListSettings`) splits nothing: one group of every row, its field unchecked. */
62
+ * set-aside group (fields' `readListSettings`) splits nothing: one group of every row, its field unchecked but for
63
+ * whether the reader may read it. */
63
64
  export declare function heldGroup(scope: Scope, request: GroupsRequest): Held;
64
65
  /** The predicate of one group's rows, two-valued: the rows `groupsQuery` counts under `key`. A key the field's type
65
66
  * can't hold selects no row. */
@@ -12,13 +12,15 @@ import { checkFarOrder, farOrderSql, sortText } from "./sort.js";
12
12
  import { FALSE, join, render, sql, TRUE } from "./sql.js";
13
13
  import { countOf } from "./steps.js";
14
14
  import { labelOf } from "./values.js";
15
+ import { readableField } from "./readable.js";
15
16
  export function heldGroup(scope, request) {
17
+ const key = request.group.fieldKey;
18
+ const named = typeof key === "string" ? readableField(scope, scope.map.table, key) : undefined;
16
19
  if (request.group.setAside)
17
20
  return { kind: "none" };
18
- const key = request.group.fieldKey;
19
- if (!Object.hasOwn(scope.map.fields, key))
21
+ if (named === undefined)
20
22
  throw REFUSALS.unknownField(key);
21
- const { type, def } = scope.map.fields[key];
23
+ const { type, def } = named;
22
24
  const by = request.group.by;
23
25
  const rule = bucketRuleFor(def, by);
24
26
  if (rule === null)
@@ -2,6 +2,7 @@ export { identifier, join, render, Sql, sql, type SqlQuery, type SqlValue } from
2
2
  export { REQUEST_LIMITS, type RequestLimits } from "./limits.js";
3
3
  export { defineFieldMap, FieldMapError, type FieldMap, type FieldMapInput, type FieldMapping, type KeyStored, type MappedField, type Read, type RelationMap, type Row, type RowPick, type Several, type Stored, type TableFields, type TableInput, type TableMap, type TextSort, type ValueRow, } from "./map.js";
4
4
  export { EVERY_ROW, type Access, type Condition, type KeysRequest } from "./access.js";
5
+ export { EVERY_FIELD, type EveryField, type Readable } from "./readable.js";
5
6
  export { RefusedRequest, type RefusalCode } from "./refusals.js";
6
7
  export { keysQuery } from "./statement.js";
7
8
  export { textOrderQuery } from "./sort.js";
@@ -2,6 +2,7 @@ export { identifier, join, render, Sql, sql } from "./sql.js";
2
2
  export { REQUEST_LIMITS } from "./limits.js";
3
3
  export { defineFieldMap, FieldMapError, } from "./map.js";
4
4
  export { EVERY_ROW } from "./access.js";
5
+ export { EVERY_FIELD } from "./readable.js";
5
6
  export { RefusedRequest } from "./refusals.js";
6
7
  export { keysQuery } from "./statement.js";
7
8
  export { textOrderQuery } from "./sort.js";
@@ -1,6 +1,7 @@
1
1
  import { listHoldSchema, withHold } from "viewkitjs/fields/query";
2
2
  import { open, scopeOf } from "./access.js";
3
3
  import { checkTree } from "./check.js";
4
+ import { readableField } from "./readable.js";
4
5
  import { REFUSALS } from "./refusals.js";
5
6
  import { searchSql } from "./search.js";
6
7
  import { join, sql, TRUE } from "./sql.js";
@@ -22,11 +23,14 @@ export function underHold(map, request, filter, hold = holdOf(request)) {
22
23
  }
23
24
  export function kept(request, filter, limited) {
24
25
  const scope = scopeOf(request, (inner, at, node) => walkerSql(inner, at, node, request.context));
25
- const own = underHold(scope.map, request, request.filter);
26
- const whole = own === undefined ? null : checkTree(scope.map, own, request.context);
26
+ const hold = holdOf(request);
27
+ for (const held of hold ?? [])
28
+ readableField(scope, scope.map.table, held.fieldKey);
29
+ const own = underHold(scope.map, request, request.filter, hold);
30
+ const whole = own === undefined ? null : checkTree(scope, own, request.context);
27
31
  const used = filter === undefined || filter === own
28
32
  ? whole
29
- : checkTree(scope.map, filter, request.context);
33
+ : checkTree(scope, filter, request.context);
30
34
  const main = open(scope, scope.map.table, "r");
31
35
  const parts = [];
32
36
  if (used !== null)
@@ -38,6 +42,6 @@ export function kept(request, filter, limited) {
38
42
  return { scope, main, where, checked: whole };
39
43
  }
40
44
  export function filterSql(rows, request, filter) {
41
- const checked = checkTree(rows.scope.map, filter, request.context);
45
+ const checked = checkTree(rows.scope, filter, request.context);
42
46
  return checked === null ? TRUE : walkerSql(rows.scope, rows.main, checked, request.context);
43
47
  }
@@ -3,6 +3,7 @@ import { groupsQuery } from "./groups.js";
3
3
  import { groupWhere, outerOrder, pagedOf } from "./page.js";
4
4
  import { REFUSALS } from "./refusals.js";
5
5
  import { join, render, sql } from "./sql.js";
6
+ import { readableField } from "./readable.js";
6
7
  export const LANES_FILLED = 10;
7
8
  export const LANE_ROWS = 25;
8
9
  const MOST = 1000;
@@ -15,11 +16,12 @@ export function lanesQuery(request) {
15
16
  throw new RangeError(`lanesQuery: fill is a whole number from 0 to ${MOST}`);
16
17
  const { board } = request;
17
18
  const paged = pagedOf({ ...request, page: undefined, size }, { group: board.fieldKey });
18
- const { map } = paged.scope;
19
+ const { scope } = paged;
20
+ const boarded = typeof board.fieldKey === "string" ? readableField(scope, scope.map.table, board.fieldKey) : undefined;
19
21
  if (board.setAside === undefined) {
20
- if (!Object.hasOwn(map.fields, board.fieldKey))
22
+ if (boarded === undefined)
21
23
  throw REFUSALS.unknownField(board.fieldKey);
22
- if (!canBoardBy(map.fields[board.fieldKey].def))
24
+ if (!canBoardBy(boarded.def))
23
25
  throw REFUSALS.noLayout(board.fieldKey, "board");
24
26
  }
25
27
  const heads = groupsQuery({ ...request, group: board });
@@ -1,9 +1,9 @@
1
1
  import { phoneFilterOperand } from "viewkitjs/fields";
2
2
  import { trimmedSql } from "./fold.js";
3
- import { fieldsOf } from "./map.js";
4
3
  import { textSql } from "./rules.js";
5
4
  import { sql } from "./sql.js";
6
5
  import { readSql, rowJoinOf } from "./values.js";
6
+ import { readableField } from "./readable.js";
7
7
  const E164 = "^\\+[1-9][0-9]{1,14}$";
8
8
  const EXTENSION = "^[0-9]{1,20}$";
9
9
  export function phoneOf(row, parts, typed, key) {
@@ -23,7 +23,7 @@ export function phoneOnRow(opened, row, key) {
23
23
  return phoneOf(opened, row.parts, row.value === undefined ? null : opened.row.column(row.value), key);
24
24
  }
25
25
  export function phoneAt(scope, at, key) {
26
- const { mapping } = fieldsOf(scope.map, at.table).fields[key];
26
+ const { mapping } = readableField(scope, at.table, key);
27
27
  const row = mapping.row;
28
28
  if (row !== undefined) {
29
29
  const joined = rowJoinOf(scope, at, key);
@@ -1,4 +1,3 @@
1
- import { fieldsOf } from "./map.js";
2
1
  import { actorKeySql } from "./actor.js";
3
2
  import { nameSql, placeSql, rangeSql } from "./parts.js";
4
3
  import { phoneAt, phoneRuleSql } from "./phone.js";
@@ -7,9 +6,10 @@ import { severalSql } from "./several.js";
7
6
  import { ruleSql } from "./rules.js";
8
7
  import { partSql, readSql, valueSql, wholeOf } from "./values.js";
9
8
  import { rowRuleSql } from "./value-rows.js";
9
+ import { readableField } from "./readable.js";
10
10
  export function predicateSql(scope, at, checked, context, fenced) {
11
11
  const { key, type } = checked;
12
- const { mapping } = fieldsOf(scope.map, at.table).fields[key];
12
+ const { mapping } = readableField(scope, at.table, key);
13
13
  if (mapping.row !== undefined)
14
14
  return rowRuleSql(scope, at, checked, context, fenced === "page");
15
15
  const value = mapping.value;
@@ -0,0 +1,40 @@
1
+ import { type FieldMap, type MappedField } from "./map.js";
2
+ declare const everyField: unique symbol;
3
+ /** EVERY_FIELD's own type: a symbol no other symbol passes for, which a request built apart from its call keeps as it
4
+ * is (a plain `unique symbol` widens to `symbol` in an object literal). */
5
+ export type EveryField = symbol & {
6
+ readonly [everyField]: true;
7
+ };
8
+ /** Said instead of a list, for a reader who may read every field: of one table, or of every table. */
9
+ export declare const EVERY_FIELD: EveryField;
10
+ /** The fields the reader may read: EVERY_FIELD for every field of every table, or, per table of the map by its table
11
+ * keys, a list of field keys or EVERY_FIELD. */
12
+ export type Readable = EveryField | Readonly<Record<string, readonly string[] | EveryField>>;
13
+ /** A request's readable fields, checked once and frozen. Internal. */
14
+ export type ReadableFields = EveryField | Readonly<Record<string, ReadonlySet<string> | EveryField>>;
15
+ /** A field of one of the map's tables. */
16
+ export type FieldAt = {
17
+ readonly table: string;
18
+ readonly key: string;
19
+ };
20
+ /** What a read is held to: the request's map and its readable fields. */
21
+ export type Reader = {
22
+ readonly map: FieldMap;
23
+ readonly readable: ReadableFields;
24
+ };
25
+ /** `given` held to `map`: refused by name when it is no list of readable fields; read once, copied and frozen. */
26
+ export declare function readableOf(map: FieldMap, given: unknown): ReadableFields;
27
+ /** Where field `key` of `table` sits in the map, as a refusal names it. */
28
+ export declare function placeOf(map: FieldMap, table: string, key: string): string;
29
+ /** Records, for a map buildFieldMap made, the fields each of its worked-out fields reads. Internal. */
30
+ export declare function fieldsReadBy(map: FieldMap, reads: ReadonlyMap<string, readonly FieldAt[]>): void;
31
+ /** `read` run as a worked-out field's own read on `reader`'s request. Internal. */
32
+ export declare function readingWorkedOut<T>(reader: Reader, read: () => T): T;
33
+ /**
34
+ * Field `key` of `table` as the reader may read it, or undefined when the map doesn't place it there (each caller
35
+ * refuses that as it always has). Refused as `hidden-field` at its place when the reader's list for `table` doesn't hold
36
+ * it, or doesn't hold a field it reads (a built map's worked-out fields), named at the end of the chain. The one door
37
+ * every field a statement reads passes.
38
+ */
39
+ export declare function readableField(reader: Reader, table: string, key: string): MappedField | undefined;
40
+ export {};
@@ -0,0 +1,84 @@
1
+ import { fieldsOf } from "./map.js";
2
+ import { REFUSALS } from "./refusals.js";
3
+ export const EVERY_FIELD = Symbol("EVERY_FIELD");
4
+ const isEveryField = (value) => value === EVERY_FIELD;
5
+ export function readableOf(map, given) {
6
+ if (isEveryField(given))
7
+ return EVERY_FIELD;
8
+ if (typeof given !== "object" || given === null || Array.isArray(given))
9
+ throw REFUSALS.noFieldList();
10
+ const entries = Object.entries(given);
11
+ for (const [table] of entries) {
12
+ if (!Object.hasOwn(map.tables, table))
13
+ throw REFUSALS.unknownReadableTable(table);
14
+ }
15
+ const readable = Object.create(null);
16
+ for (const [table, keys] of entries) {
17
+ if (isEveryField(keys)) {
18
+ readable[table] = EVERY_FIELD;
19
+ continue;
20
+ }
21
+ const copy = Array.isArray(keys) ? Array.from(keys) : [];
22
+ if (!Array.isArray(keys) || !copy.every((key) => typeof key === "string"))
23
+ throw REFUSALS.notAFieldList(table);
24
+ readable[table] = new Set(copy);
25
+ }
26
+ return Object.freeze(readable);
27
+ }
28
+ export function placeOf(map, table, key) {
29
+ return table === map.table ? `fields.${key}` : `tables.${table}.fields.${key}`;
30
+ }
31
+ function mayRead(reader, table, key) {
32
+ const { readable } = reader;
33
+ if (isEveryField(readable))
34
+ return true;
35
+ if (!Object.hasOwn(readable, table))
36
+ throw REFUSALS.noFieldListFor(table);
37
+ const keys = readable[table];
38
+ return isEveryField(keys) || keys.has(key);
39
+ }
40
+ const FIELDS_READ = new WeakMap();
41
+ export function fieldsReadBy(map, reads) {
42
+ FIELDS_READ.set(map, reads);
43
+ }
44
+ function hiddenRead(reader, table, key, seen) {
45
+ for (const read of FIELDS_READ.get(reader.map)?.get(`${table}\u0000${key}`) ?? []) {
46
+ const address = `${read.table}\u0000${read.key}`;
47
+ if (seen.has(address))
48
+ continue;
49
+ seen.add(address);
50
+ const own = fieldsOf(reader.map, read.table);
51
+ if (own === undefined || !Object.hasOwn(own.fields, read.key))
52
+ continue;
53
+ if (!mayRead(reader, read.table, read.key))
54
+ return read;
55
+ const deeper = hiddenRead(reader, read.table, read.key, seen);
56
+ if (deeper !== null)
57
+ return deeper;
58
+ }
59
+ return null;
60
+ }
61
+ const WORKED_OUT = new WeakMap();
62
+ export function readingWorkedOut(reader, read) {
63
+ WORKED_OUT.set(reader, (WORKED_OUT.get(reader) ?? 0) + 1);
64
+ try {
65
+ return read();
66
+ }
67
+ finally {
68
+ WORKED_OUT.set(reader, WORKED_OUT.get(reader) - 1);
69
+ }
70
+ }
71
+ export function readableField(reader, table, key) {
72
+ const own = fieldsOf(reader.map, table);
73
+ if (own === undefined || !Object.hasOwn(own.fields, key))
74
+ return undefined;
75
+ if ((WORKED_OUT.get(reader) ?? 0) > 0)
76
+ return own.fields[key];
77
+ const at = placeOf(reader.map, table, key);
78
+ if (!mayRead(reader, table, key))
79
+ throw REFUSALS.hiddenField(at, key);
80
+ const hidden = hiddenRead(reader, table, key, new Set([`${table}\u0000${key}`]));
81
+ if (hidden !== null)
82
+ throw REFUSALS.hiddenField(at, key, placeOf(reader.map, hidden.table, hidden.key));
83
+ return own.fields[key];
84
+ }
@@ -13,6 +13,7 @@ import { elementsOn } from "./several.js";
13
13
  import { join, render, sql, TRUE } from "./sql.js";
14
14
  import { userReadOf } from "./users.js";
15
15
  import { labelOf, listSql, oneKeyOf, readSql, rowJoinOf, valueSql } from "./values.js";
16
+ import { readableField } from "./readable.js";
16
17
  export { readsUser } from "./users.js";
17
18
  export const VALUES_PAGE = 1000;
18
19
  const RUN = 100;
@@ -130,17 +131,19 @@ export function valuesQuery(request) {
130
131
  if (keyStored === undefined)
131
132
  throw REFUSALS.noKeyType(map.table, "a read by id");
132
133
  const fields = [...new Set(request.fields)];
133
- for (const key of fields) {
134
- if (typeof key !== "string" || !Object.hasOwn(map.fields, key))
134
+ const mapped = fields.map((key) => {
135
+ const field = typeof key === "string" ? readableField(scope, map.table, key) : undefined;
136
+ if (field === undefined)
135
137
  throw REFUSALS.unknownField(String(key));
136
- }
138
+ return field;
139
+ });
137
140
  const ids = [...new Set(request.ids.filter((id) => fitsKey(id, keyStored)).map((id) => spelledAsKept(id, keyStored)))];
138
141
  const chunks = [];
139
142
  for (let at = 0; at < ids.length; at += VALUES_PAGE)
140
143
  chunks.push(ids.slice(at, at + VALUES_PAGE));
141
144
  if (chunks.length === 0)
142
145
  chunks.push([]);
143
- const values = fields.map((key) => valueJson(scope, main, key, map.fields[key]));
146
+ const values = fields.map((key, index) => valueJson(scope, main, key, mapped[index]));
144
147
  if (values.length > RUN * RUN)
145
148
  throw new RangeError(`valuesQuery: at most ${RUN * RUN} fields a statement`);
146
149
  const runs = [];
@@ -1,6 +1,6 @@
1
1
  import type { RequestLimits } from "./limits.js";
2
2
  /** What a refused request names. */
3
- export type RefusalCode = "not-a-map" | "unknown-table" | "not-a-condition" | "no-condition" | "no-sql" | "unknown-field" | "unsafe-mapping" | "bad-rule" | "bad-hold" | "bad-sort" | "bad-cursor" | "no-label" | "no-fields" | "no-options" | "too-many" | "too-big" | "no-layout" | "not-yet";
3
+ export type RefusalCode = "not-a-map" | "unknown-table" | "not-a-condition" | "no-condition" | "no-sql" | "unknown-field" | "unsafe-mapping" | "bad-rule" | "bad-hold" | "bad-sort" | "bad-cursor" | "no-label" | "no-fields" | "no-options" | "too-many" | "too-big" | "no-layout" | "not-yet" | "no-field-list" | "not-a-field-list" | "hidden-field";
4
4
  /** A request the translator writes no SQL for: `code` says why. */
5
5
  export declare class RefusedRequest extends Error {
6
6
  readonly code: RefusalCode;
@@ -12,6 +12,13 @@ export declare const REFUSALS: {
12
12
  readonly unknownTable: (table: string) => RefusedRequest;
13
13
  readonly notACondition: (table: string) => RefusedRequest;
14
14
  readonly noCondition: (table: string) => RefusedRequest;
15
+ readonly noFieldList: () => RefusedRequest;
16
+ readonly noFieldListFor: (table: string) => RefusedRequest;
17
+ readonly notAFieldList: (table: string) => RefusedRequest;
18
+ readonly unknownReadableTable: (table: string) => RefusedRequest;
19
+ /** A field the reader may not read, or one it reads: `at` and `reads` are places in the map (`fields.k`,
20
+ * `tables.t.fields.k`), never a value. */
21
+ readonly hiddenField: (at: string, key: string, reads?: string) => RefusedRequest;
15
22
  readonly conditionNoSql: (table: string) => RefusedRequest;
16
23
  readonly expressionNoSql: (key: string) => RefusedRequest;
17
24
  readonly expressionNoSqlAt: (path: string) => RefusedRequest;
@@ -17,6 +17,13 @@ export const REFUSALS = {
17
17
  unknownTable: (table) => new RefusedRequest("unknown-table", `access: ${table} is not one of the map's tables`),
18
18
  notACondition: (table) => new RefusedRequest("not-a-condition", `access.${table}: a permission condition is a function of the row, or EVERY_ROW`),
19
19
  noCondition: (table) => new RefusedRequest("no-condition", `access.${table}: no permission condition; hand one in, or EVERY_ROW when every row of ${table} may be read`),
20
+ noFieldList: () => new RefusedRequest("no-field-list", "readable: no list of the fields the reader may read; hand one in, or EVERY_FIELD when the reader may read every field"),
21
+ noFieldListFor: (table) => new RefusedRequest("no-field-list", `readable.${table}: no list of the fields the reader may read; hand one in, or EVERY_FIELD when every field of ${table} may be read`),
22
+ notAFieldList: (table) => new RefusedRequest("not-a-field-list", `readable.${table}: a table's readable fields are a list of field keys, or EVERY_FIELD`),
23
+ unknownReadableTable: (table) => new RefusedRequest("unknown-table", `readable: ${table} is not one of the map's tables`),
24
+ hiddenField: (at, key, reads) => new RefusedRequest("hidden-field", reads === undefined
25
+ ? `${at}: this reader may not read ${key}`
26
+ : `${at}: this reader may not read ${key}, which reads ${reads}`),
20
27
  conditionNoSql: (table) => new RefusedRequest("no-sql", `access.${table}: the condition returned no SQL; build it with sql\`…\``),
21
28
  expressionNoSql: (key) => new RefusedRequest("no-sql", `fields.${key}: the expression returned no SQL; build it with sql\`…\``),
22
29
  expressionNoSqlAt: (path) => new RefusedRequest("no-sql", `${path}: the expression returned no SQL; build it with sql\`…\``),
@@ -1,18 +1,19 @@
1
1
  import { open } from "./access.js";
2
2
  import { checkOwnWhere } from "./check.js";
3
- import { fieldsOf, isOneRelation } from "./map.js";
3
+ import { isOneRelation } from "./map.js";
4
4
  import { leadOf, oneKeyOf, valueSql } from "./values.js";
5
5
  import { optionSql } from "./rules.js";
6
6
  import { castTo, join, sql } from "./sql.js";
7
+ import { readableField } from "./readable.js";
7
8
  const paren = (inner) => sql `(${inner})`;
8
9
  const not = (inner) => sql `(NOT ${inner})`;
9
10
  export function reachedFrom(scope, at, key, within = null) {
10
- const relation = fieldsOf(scope.map, at.table).fields[key].mapping.value.relation;
11
+ const relation = readableField(scope, at.table, key).mapping.value.relation;
11
12
  const reached = reachOf(scope, at, relation);
12
13
  if (isOneRelation(scope.map.relations[relation]))
13
14
  return reached;
14
15
  const narrowing = [];
15
- const own = checkOwnWhere(scope.map, at.table, key, scope.context);
16
+ const own = checkOwnWhere(scope, at.table, key, scope.context);
16
17
  if (own !== null)
17
18
  narrowing.push(scope.walk(reached.far, own));
18
19
  const since = scope.context?.since;
@@ -21,7 +22,7 @@ export function reachedFrom(scope, at, key, within = null) {
21
22
  return narrowing.length === 0 ? reached : { ...reached, ties: sql `${reached.ties} AND ${join(narrowing, sql ` AND `)}` };
22
23
  }
23
24
  function sinceSql(scope, far, within, since) {
24
- const stored = fieldsOf(scope.map, far.table).fields[within].mapping.stored;
25
+ const stored = readableField(scope, far.table, within).mapping.stored;
25
26
  const instant = since.toISOString();
26
27
  const bound = stored === "timestamp" ? sql `${instant.slice(0, 23)}::timestamp` : sql `${instant}::timestamptz`;
27
28
  const own = castTo(stored);
@@ -3,6 +3,7 @@ import { foldedSql } from "./fold.js";
3
3
  import { keyTextSql, searchedReadOf } from "./search.js";
4
4
  import { join, pickLiteral, render, sql } from "./sql.js";
5
5
  import { labelOf } from "./values.js";
6
+ import { EVERY_FIELD } from "./readable.js";
6
7
  export const SEARCH_INDEX_REASONS = {
7
8
  phone: "a phone is searched by its number's own rule, which no index serves",
8
9
  option: "an option is searched by its options' labels, which no index serves yet",
@@ -74,7 +75,7 @@ function statisticsOf(index, base) {
74
75
  export function searchIndexes(input) {
75
76
  const { map } = input;
76
77
  const access = Object.fromEntries(Object.keys(map.tables).map((table) => [table, EVERY_ROW]));
77
- const scope = scopeOf({ map, access });
78
+ const scope = scopeOf({ map, access, readable: EVERY_FIELD });
78
79
  const main = open(scope, map.table, "r");
79
80
  const mainAlias = aliasOf(main.row.key);
80
81
  const indexes = [];