@fabrika/db 0.9.0-dev.13 → 0.9.0-dev.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/policy.ts CHANGED
@@ -103,6 +103,11 @@ interface WritablePolicy<Col extends string, A extends string, V extends Col, Ac
103
103
  readonly confers?: { readonly [relation: string]: ConfersPolicy<Col> }
104
104
  /** Conditions that narrow an action of this table: a row the condition does not admit is out of that action's reach. */
105
105
  readonly when?: { readonly [action in A]?: ConditionPolicy<Col> }
106
+ /**
107
+ * Relation → action → a condition on the rows that relation reaches for that action: it narrows the relation's terms
108
+ * only, never an IAM grant, a member reveal or a path grant of the action.
109
+ */
110
+ readonly relationWhen?: { readonly [relation: string]: { readonly [action in A]?: ConditionPolicy<Col> } }
106
111
  /** Relation of this table's own dimension, or of one it reads through → the ancestor node a row the relation reaches reveals. */
107
112
  readonly reveals?: { readonly [relation: string]: RevealsPolicy<Col, A> }
108
113
  /**
@@ -112,10 +117,31 @@ interface WritablePolicy<Col extends string, A extends string, V extends Col, Ac
112
117
  */
113
118
  readonly readThrough?: { readonly [dimension: string]: Col | `${Col}.${string}` }
114
119
  /**
115
- * This table's own dimension → the ancestor node a row reveals to its members: a grant of one of `actions` at the
116
- * row's own node holds that action at its node in `to` too, for a `Within`'s reads, while `when` admits the row.
120
+ * Dimension of `readThrough` → the actions a `Ref` held through that path holds on the table's rows, writes included:
121
+ * a grant of one of them at the node the path names, or above it, reaches the row for that action. Whoever inserts
122
+ * the path's column must hold every listed action at the node it names.
123
+ */
124
+ readonly writeThrough?: { readonly [dimension: string]: readonly A[] }
125
+ /**
126
+ * This table's own dimension, or one it reads through (`readThrough`) → the ancestor node a row reveals to its
127
+ * members: a grant of one of `actions` at the row's own node, or at the node its path names, holds that action at its
128
+ * node in `to` too, while `when` admits the row.
117
129
  */
118
130
  readonly memberReveals?: { readonly [dimension: string]: MemberRevealsPolicy<Col, A> }
131
+ /**
132
+ * Relation → an access view that lists the rows the relation reaches: the escape path for a rule no structured
133
+ * declaration expresses. The view's SQL decides; the layer binds only the caller's principal and the nodes it holds
134
+ * the relation inside, never a handler value.
135
+ */
136
+ readonly access?: { readonly [relation: string]: AccessPolicy }
137
+ }
138
+
139
+ /** An access view: `row` names this table's key; `principal` is compared to the caller, `member` to the nodes it holds the relation inside. */
140
+ export interface AccessPolicy {
141
+ readonly view: string
142
+ readonly row: string
143
+ readonly principal?: string
144
+ readonly member?: string
119
145
  }
120
146
 
121
147
  /** What a row reveals to a caller with a grant at its node: its node in `to`, for `actions`, while `when` admits the row. */
@@ -124,6 +150,8 @@ export interface MemberRevealsPolicy<Col extends string, A extends string> {
124
150
  readonly actions: readonly A[]
125
151
  /** A condition on the revealing row, under the same path rule as an action's `when`. */
126
152
  readonly when?: ConditionPolicy<Col>
153
+ /** Through a path, on a table with an insert action: the writer picks the node, and `self` lets it pick its own. */
154
+ readonly assign?: { readonly self?: true }
127
155
  }
128
156
 
129
157
  /**
@@ -323,12 +351,27 @@ export interface TableModel {
323
351
  readonly confers: readonly RelationSource[]
324
352
  /** The condition each action of the table holds a row under; an action without one is not narrowed. */
325
353
  readonly when: Readonly<Record<string, ConditionModel>>
354
+ /** Relation → action → the condition the rows that relation reaches hold for that action under; ANDed onto its terms only. */
355
+ readonly relationWhen: Readonly<Record<string, Readonly<Record<string, ConditionModel>>>>
326
356
  /** The ancestor nodes this table's rows reveal to a relation of its own dimension or of one it reads through. */
327
357
  readonly reveals: readonly RevealModel[]
328
358
  /** The secondary read paths: a dimension outside the chain, by the column that holds the row's node in it. */
329
359
  readonly readThrough: readonly SecondaryPath[]
330
360
  /** The ancestor nodes this table's rows reveal to a grant at their own node. */
331
361
  readonly memberReveals: readonly MemberRevealModel[]
362
+ /** The access views of the relations whose reach a view's SQL decides. */
363
+ readonly access: readonly AccessModel[]
364
+ }
365
+
366
+ /** A relation's access view over this table: `T.<key> in (select V.<row> from V where <bindings>)`. */
367
+ export interface AccessModel {
368
+ readonly relation: string
369
+ /** The relation's dimension: a hold inside a node of it alone binds `member`. */
370
+ readonly dimension: string
371
+ readonly view: string
372
+ readonly row: string
373
+ readonly principal: string | undefined
374
+ readonly member: string | undefined
332
375
  }
333
376
 
334
377
  /**
@@ -346,6 +389,8 @@ export interface SecondaryPath {
346
389
  * then the container's column, and the path is read as `T.<via column> in (select C.<key> from C where …)`.
347
390
  */
348
391
  readonly via: ContainerHop | undefined
392
+ /** The actions a `Ref` held through the path holds on the table's rows (`writeThrough`); a `Within` reads through every path. */
393
+ readonly writes: readonly string[]
349
394
  }
350
395
 
351
396
  /** The row's nearest containment column, and the table and key it names. */
@@ -366,7 +411,7 @@ export interface RevealModel {
366
411
  readonly key: string
367
412
  /**
368
413
  * The secondary path whose node, or an ancestor of it, the relation's nodes match. A reveal matched through one holds
369
- * its actions for a `Within`'s reads only, as the path itself does.
414
+ * a `Ref` of an action only where the path does (`writeThrough`).
370
415
  */
371
416
  readonly path: SecondaryPath | undefined
372
417
  /** The bound on an insert's delegation through `path`: `undefined` when no handler inserts a row of the table. */
@@ -386,7 +431,12 @@ export interface RevealModel {
386
431
  export interface MemberRevealModel {
387
432
  readonly source: string
388
433
  readonly table: string
434
+ /** The column a grant's nodes match: the table's key, or `path`'s column. */
389
435
  readonly key: string
436
+ /** The secondary path whose node a grant names; a `Ref` holds an action through it only where the path does. */
437
+ readonly path: SecondaryPath | undefined
438
+ /** The bound on an insert's pick of the node through `path`: `undefined` when no handler inserts a row of the table. */
439
+ readonly assign: { readonly self: boolean } | undefined
390
440
  readonly dimension: string
391
441
  readonly column: string
392
442
  readonly actions: readonly string[]
@@ -426,14 +476,19 @@ type ParsedScope =
426
476
  readonly inherit: readonly string[] | undefined
427
477
  }
428
478
 
429
- type ParsedTable = Omit<TableModel, 'chain' | 'scope' | 'confers' | 'when' | 'reveals' | 'readThrough' | 'memberReveals'> & {
430
- readonly scope: ParsedScope
431
- readonly confersInput: unknown
432
- readonly whenInput: unknown
433
- readonly revealsInput: unknown
434
- readonly readThroughInput: unknown
435
- readonly memberRevealsInput: unknown
436
- }
479
+ type ParsedTable =
480
+ & Omit<TableModel, 'chain' | 'scope' | 'confers' | 'when' | 'relationWhen' | 'reveals' | 'readThrough' | 'memberReveals' | 'access'>
481
+ & {
482
+ readonly scope: ParsedScope
483
+ readonly confersInput: unknown
484
+ readonly whenInput: unknown
485
+ readonly relationWhenInput: unknown
486
+ readonly revealsInput: unknown
487
+ readonly readThroughInput: unknown
488
+ readonly writeThroughInput: unknown
489
+ readonly memberRevealsInput: unknown
490
+ readonly accessInput: unknown
491
+ }
437
492
 
438
493
  let readModels: <DB, P>(tables: Tables<DB, P>) => TableModels
439
494
  let hasModels: (value: object) => boolean = () => false
@@ -570,12 +625,15 @@ const POLICY_FIELDS = nameSet([
570
625
  'files',
571
626
  'confers',
572
627
  'when',
628
+ 'relationWhen',
573
629
  'reveals',
574
630
  'readThrough',
631
+ 'writeThrough',
575
632
  'memberReveals',
633
+ 'access',
576
634
  ])
577
635
  const REVEALS_FIELDS = nameSet(['to', 'actions', 'when', 'assign'])
578
- const MEMBER_REVEALS_FIELDS = nameSet(['to', 'actions', 'when'])
636
+ const MEMBER_REVEALS_FIELDS = nameSet(['to', 'actions', 'when', 'assign'])
579
637
  const REVEAL_ASSIGN_FIELDS = nameSet(['self'])
580
638
  const CONFERS_FIELDS = nameSet(['principal', 'node', 'expires', 'when', 'assign'])
581
639
  const ASSIGN_FIELDS = nameSet(['maxDays', 'self'])
@@ -613,10 +671,15 @@ function parseTables(columnsInput: unknown, policiesInput: unknown, vocabulary:
613
671
  }
614
672
  }
615
673
  const parsed: ParsedTable[] = []
674
+ const views: Record<string, ViewColumns> = {}
616
675
  for (let index = 0; index < tables.length; index++) {
617
676
  const table = tables[index] ?? ''
618
677
  const policy = ownEntry(policies, table)
619
678
  const entry = ownEntry(columns, table)
679
+ if (isViewEntry(table, entry)) {
680
+ const names = parseList(ownEntry(plainRecord(entry, `${table}: the column list entry`), 'columns'), (name) => String(name))
681
+ setOwn(views, table, freeze({ columns: nameSet(names ?? []), policy: policy !== undefined }))
682
+ }
620
683
  // A view without a policy stays what it was before codegen read views: only ctx.sql reads it.
621
684
  if (policy === undefined && isViewEntry(table, entry)) {
622
685
  continue
@@ -626,7 +689,7 @@ function parseTables(columnsInput: unknown, policiesInput: unknown, vocabulary:
626
689
  }
627
690
  parsed[parsed.length] = parseTable(table, entry, policy, vocabulary)
628
691
  }
629
- return linkTables(parsed, vocabulary)
692
+ return linkTables(parsed, vocabulary, views)
630
693
  }
631
694
 
632
695
  /** Whether a column list entry marks a view: its `view` is `true`, or absent for a table. */
@@ -808,9 +871,12 @@ function parseTable(table: string, columnList: unknown, policyInput: unknown, vo
808
871
  view,
809
872
  confersInput: ownEntry(policy, 'confers'),
810
873
  whenInput: ownEntry(policy, 'when'),
874
+ relationWhenInput: ownEntry(policy, 'relationWhen'),
811
875
  revealsInput: ownEntry(policy, 'reveals'),
812
876
  readThroughInput: ownEntry(policy, 'readThrough'),
877
+ writeThroughInput: ownEntry(policy, 'writeThrough'),
813
878
  memberRevealsInput: ownEntry(policy, 'memberReveals'),
879
+ accessInput: ownEntry(policy, 'access'),
814
880
  }
815
881
  }
816
882
 
@@ -1288,7 +1354,7 @@ export function includes(names: readonly string[], name: unknown): boolean {
1288
1354
  }
1289
1355
 
1290
1356
  /** Resolves every table's containment chain and refuses a shape the layer cannot enforce. */
1291
- function linkTables(parsed: readonly ParsedTable[], vocabulary: VocabularyModel): TableModels {
1357
+ function linkTables(parsed: readonly ParsedTable[], vocabulary: VocabularyModel, views: Readonly<Record<string, ViewColumns>>): TableModels {
1292
1358
  const containment = vocabulary.dimensions
1293
1359
  const byName: Record<string, ParsedTable> = {}
1294
1360
  const owners: Record<string, ParsedTable> = {}
@@ -1343,7 +1409,7 @@ function linkTables(parsed: readonly ParsedTable[], vocabulary: VocabularyModel)
1343
1409
  const linkedTable = (name: string): LinkedTable | undefined => {
1344
1410
  const target = ownEntry(byName, name)
1345
1411
  if (target === undefined) return undefined
1346
- const { confersInput, whenInput, revealsInput, readThroughInput, memberRevealsInput, ...rest } = target
1412
+ const { confersInput, whenInput, relationWhenInput, revealsInput, readThroughInput, writeThroughInput, memberRevealsInput, ...rest } = target
1347
1413
  return { ...rest, ...resolve(target, []) }
1348
1414
  }
1349
1415
  const hopRule: HopRule = (from, column, to, key, backward) => {
@@ -1380,12 +1446,14 @@ function linkTables(parsed: readonly ParsedTable[], vocabulary: VocabularyModel)
1380
1446
  return true
1381
1447
  }
1382
1448
  const sources: Record<string, string> = {}
1449
+ const viewed: Record<string, string> = {}
1383
1450
  const models: Record<string, TableModel> = {}
1384
1451
  for (let index = 0; index < parsed.length; index++) {
1385
1452
  const table = parsed[index]
1386
1453
  if (table !== undefined) {
1387
1454
  const { scope, chain } = resolve(table, [])
1388
- const { confersInput, whenInput, revealsInput, readThroughInput, memberRevealsInput, ...rest } = table
1455
+ const { confersInput, whenInput, relationWhenInput, revealsInput, readThroughInput, writeThroughInput, memberRevealsInput, accessInput, ...rest } =
1456
+ table
1389
1457
  const linked = { ...rest, scope, chain }
1390
1458
  const ownerOf = (dimension: string): ResolvedOwner | undefined => {
1391
1459
  const owner = ownEntry(owners, dimension)
@@ -1399,7 +1467,12 @@ function linkTables(parsed: readonly ParsedTable[], vocabulary: VocabularyModel)
1399
1467
  if (target === undefined || typeof column !== 'string' || splitPath(column).length !== 1) return undefined
1400
1468
  return parseReadThrough(target, { [dimension]: column }, vocabulary, ownerOf, () => undefined)[0]
1401
1469
  }
1402
- const readThrough = parseReadThrough(linked, readThroughInput, vocabulary, ownerOf, containerPath)
1470
+ const readThrough = parseWriteThrough(
1471
+ linked,
1472
+ parseReadThrough(linked, readThroughInput, vocabulary, ownerOf, containerPath),
1473
+ writeThroughInput,
1474
+ vocabulary,
1475
+ )
1403
1476
  const confers = parseConfers(linked, confersInput, vocabulary.relations, conditionTable, linkedTable, hopRule)
1404
1477
  for (let position = 0; position < confers.length; position++) {
1405
1478
  const relation = confers[position]?.relation ?? ''
@@ -1409,23 +1482,116 @@ function linkTables(parsed: readonly ParsedTable[], vocabulary: VocabularyModel)
1409
1482
  }
1410
1483
  setOwn(sources, relation, table.name)
1411
1484
  }
1412
- const when = parseWhen(linked, conditionRoot(linked, byName), whenInput, conditionTable, hopRule)
1485
+ const root = conditionRoot(linked, byName)
1486
+ const when = parseWhen(linked, root, whenInput, conditionTable, hopRule)
1487
+ const relationWhen = parseRelationWhen(linked, root, relationWhenInput, vocabulary, conditionTable, hopRule)
1413
1488
  const reveals = parseReveals(linked, readThrough, revealsInput, vocabulary, conditionTable, hopRule)
1414
- const memberReveals = parseMemberReveals(linked, memberRevealsInput, vocabulary, conditionTable, hopRule)
1415
- setOwn(models, table.name, freeze({ ...linked, confers, when, reveals, readThrough, memberReveals }))
1489
+ const memberReveals = parseMemberReveals(linked, readThrough, memberRevealsInput, vocabulary, conditionTable, hopRule)
1490
+ const access = parseAccess(linked, accessInput, vocabulary, views)
1491
+ for (let position = 0; position < access.length; position++) {
1492
+ const relation = access[position]?.relation ?? ''
1493
+ if (!hasOwn(viewed, relation)) setOwn(viewed, relation, table.name)
1494
+ }
1495
+ setOwn(models, table.name, freeze({ ...linked, confers, when, relationWhen, reveals, readThrough, memberReveals, access }))
1416
1496
  }
1417
1497
  }
1498
+ checkRelationWhenReach(models, vocabulary)
1418
1499
  const relations = ownKeys(vocabulary.relations)
1419
1500
  for (let index = 0; index < relations.length; index++) {
1420
1501
  const relation = relations[index] ?? ''
1421
- if (!hasOwn(sources, relation)) {
1422
- throw new PolicyError(`the vocabulary relation ${relation} has no source: declare confers.${relation} on the table whose rows hold it`)
1502
+ if (!hasOwn(sources, relation) && !hasOwn(viewed, relation)) {
1503
+ throw new PolicyError(
1504
+ `the vocabulary relation ${relation} has no source: declare confers.${relation} on the table whose rows hold it, or name an access view for it`,
1505
+ )
1423
1506
  }
1424
1507
  }
1508
+ checkRevealSources(models, sources)
1425
1509
  return freeze(models)
1426
1510
  }
1427
1511
 
1428
- type LinkedTable = Omit<TableModel, 'confers' | 'when' | 'reveals' | 'readThrough' | 'memberReveals'>
1512
+ /** A reveal is matched through the relation's source rows, so a relation that access views alone serve reveals nothing. */
1513
+ function checkRevealSources(models: Readonly<Record<string, TableModel>>, sources: Readonly<Record<string, string>>): void {
1514
+ const tables = ownKeys(models)
1515
+ for (let index = 0; index < tables.length; index++) {
1516
+ const model = ownEntry(models, tables[index] ?? '')
1517
+ const reveals = model?.reveals ?? []
1518
+ for (let position = 0; position < reveals.length; position++) {
1519
+ const relation = reveals[position]?.relation ?? ''
1520
+ if (model !== undefined && !hasOwn(sources, relation)) {
1521
+ throw new PolicyError(
1522
+ `${model.name}: reveals.${relation} is matched through the rows that confer it, and only access views serve it; declare confers.${relation}`,
1523
+ )
1524
+ }
1525
+ }
1526
+ }
1527
+ }
1528
+
1529
+ type LinkedTable = Omit<TableModel, 'confers' | 'when' | 'relationWhen' | 'reveals' | 'readThrough' | 'memberReveals' | 'access'>
1530
+
1531
+ /** A view of the column list: its columns, and whether a policy makes it a table of the layer. */
1532
+ interface ViewColumns {
1533
+ readonly columns: NameSet
1534
+ readonly policy: boolean
1535
+ }
1536
+
1537
+ const ACCESS_FIELDS = nameSet(['view', 'row', 'principal', 'member'])
1538
+
1539
+ /**
1540
+ * `access`: relation → the view whose rows name the table's rows the relation reaches. The view is one the column list
1541
+ * marks, `row` and each binding are its columns, and at least one binding is stated, so the subquery never reads every
1542
+ * row: `principal` is compared to the caller, `member` to the nodes of the relation's dimension it holds the relation in.
1543
+ */
1544
+ function parseAccess(
1545
+ table: LinkedTable,
1546
+ input: unknown,
1547
+ vocabulary: VocabularyModel,
1548
+ views: Readonly<Record<string, ViewColumns>>,
1549
+ ): readonly AccessModel[] {
1550
+ if (input === undefined) {
1551
+ return freeze([])
1552
+ }
1553
+ if (table.scope.mode !== 'dimension' && table.scope.mode !== 'child') {
1554
+ throw new PolicyError(`${table.name}: only a dimension or child table, or a child view, takes access; its rows are what a relation reaches`)
1555
+ }
1556
+ const declared = plainRecord(input, `${table.name}: access`)
1557
+ const relations = ownKeys(declared)
1558
+ const parsed: AccessModel[] = []
1559
+ for (let index = 0; index < relations.length; index++) {
1560
+ const relation = relations[index] ?? ''
1561
+ const what = `${table.name}: access.${relation}`
1562
+ if (!hasOwn(vocabulary.relations, relation)) {
1563
+ throw new PolicyError(`${what} names a relation the vocabulary does not declare (${ownKeys(vocabulary.relations).join(', ')})`)
1564
+ }
1565
+ const entry = plainRecord(ownEntry(declared, relation), what)
1566
+ onlyFields(entry, ACCESS_FIELDS, what)
1567
+ const view = ownEntry(entry, 'view')
1568
+ const named = typeof view === 'string' ? ownEntry(views, view) : undefined
1569
+ if (typeof view !== 'string' || named === undefined) {
1570
+ throw new PolicyError(`${what}.view names ${String(view)}, which the column list does not mark as a view`)
1571
+ }
1572
+ // Read directly, the view would show the caller who else reaches which row.
1573
+ if (named.policy) {
1574
+ throw new PolicyError(`${what}.view names ${view}, which has a policy; an access view takes none, so only the relation's term reads it`)
1575
+ }
1576
+ const columns = named.columns
1577
+ const viewColumn = (field: string, required: boolean): string | undefined => {
1578
+ const name = ownEntry(entry, field)
1579
+ if (name === undefined && !required) return undefined
1580
+ if (typeof name !== 'string' || !inSet(columns, name)) {
1581
+ throw new PolicyError(`${what}.${field} names ${String(name)}, which is not a column of ${view}`)
1582
+ }
1583
+ return name
1584
+ }
1585
+ const row = viewColumn('row', true) ?? ''
1586
+ const principal = viewColumn('principal', false)
1587
+ const member = viewColumn('member', false)
1588
+ if (principal === undefined && member === undefined) {
1589
+ throw new PolicyError(`${what} binds neither principal nor member: an access view is read for the caller, never whole`)
1590
+ }
1591
+ parsed[parsed.length] = freeze({ relation, dimension: ownEntry(vocabulary.relations, relation) ?? '', view, row, principal, member })
1592
+ }
1593
+ return freeze(parsed)
1594
+ }
1429
1595
 
1430
1596
  /** Every action a table names: its operations', `appRead`, `appCount` and its columns' own. */
1431
1597
  function tableActionNames(table: LinkedTable): NameSet {
@@ -1486,6 +1652,120 @@ function parseWhen(
1486
1652
  return freeze(when)
1487
1653
  }
1488
1654
 
1655
+ /**
1656
+ * `relationWhen`: relation → action → a condition on the rows the relation reaches for the action, parsed as `when` is
1657
+ * (the same root, hop rule and insert rule). The relation must be the vocabulary's and the action the table's; whether
1658
+ * the relation reaches the table at all is checked once every table is parsed (`checkRelationWhenReach`).
1659
+ */
1660
+ function parseRelationWhen(
1661
+ table: LinkedTable,
1662
+ root: ConditionTable,
1663
+ input: unknown,
1664
+ vocabulary: VocabularyModel,
1665
+ tables: (name: string) => ConditionTable | undefined,
1666
+ hops: HopRule,
1667
+ ): Readonly<Record<string, Readonly<Record<string, ConditionModel>>>> {
1668
+ if (input === undefined) {
1669
+ return freeze({})
1670
+ }
1671
+ if (table.scope.mode !== 'dimension' && table.scope.mode !== 'child') {
1672
+ throw new PolicyError(`${table.name}: a ${table.scope.mode} table takes no relationWhen; no relation reaches its rows`)
1673
+ }
1674
+ const declared = plainRecord(input, `${table.name}: relationWhen`)
1675
+ const relations = ownKeys(declared)
1676
+ const named = tableActionNames(table)
1677
+ const parsed: Record<string, Readonly<Record<string, ConditionModel>>> = {}
1678
+ for (let index = 0; index < relations.length; index++) {
1679
+ const relation = relations[index] ?? ''
1680
+ if (!hasOwn(vocabulary.relations, relation)) {
1681
+ throw new PolicyError(
1682
+ `${table.name}: relationWhen.${relation} names a relation the vocabulary does not declare (${ownKeys(vocabulary.relations).join(', ')})`,
1683
+ )
1684
+ }
1685
+ const perAction = plainRecord(ownEntry(declared, relation), `${table.name}: relationWhen.${relation}`)
1686
+ const actions = ownKeys(perAction)
1687
+ if (actions.length === 0) {
1688
+ throw new PolicyError(`${table.name}: relationWhen.${relation} names no action`)
1689
+ }
1690
+ const conditions: Record<string, ConditionModel> = {}
1691
+ for (let position = 0; position < actions.length; position++) {
1692
+ const action = actions[position] ?? ''
1693
+ const what = `${table.name}: relationWhen.${relation}.${action}`
1694
+ if (!inSet(named, action)) {
1695
+ throw new PolicyError(`${what} names an action the table does not name (${namesOf(named).join(', ')})`)
1696
+ }
1697
+ const condition = parseCondition(ownEntry(perAction, action), root, tables, what, hops)
1698
+ const problem = includes(table.actions.insert, action) ? insertProblem(condition, table.name) : undefined
1699
+ if (problem !== undefined) {
1700
+ throw new PolicyError(`${what}: ${problem}`)
1701
+ }
1702
+ setOwn(conditions, action, condition)
1703
+ }
1704
+ setOwn(parsed, relation, freeze(conditions))
1705
+ }
1706
+ return freeze(parsed)
1707
+ }
1708
+
1709
+ /**
1710
+ * Every `relationWhen` names a relation that reaches the table for its action: the relation's dimension is the table's
1711
+ * or on its chain, a secondary path reaches it, or a reveal of the relation for the action reveals a node the table
1712
+ * lies at. A child view is reached through its parent row, so its parent is what the relation must reach.
1713
+ */
1714
+ function checkRelationWhenReach(models: Readonly<Record<string, TableModel>>, vocabulary: VocabularyModel): void {
1715
+ const names = ownKeys(models)
1716
+ const reveals: RevealModel[] = []
1717
+ for (let index = 0; index < names.length; index++) {
1718
+ const model = ownEntry(models, names[index] ?? '')
1719
+ for (let position = 0; model !== undefined && position < model.reveals.length; position++) {
1720
+ const reveal = model.reveals[position]
1721
+ if (reveal !== undefined) reveals[reveals.length] = reveal
1722
+ }
1723
+ }
1724
+ for (let index = 0; index < names.length; index++) {
1725
+ const model = ownEntry(models, names[index] ?? '')
1726
+ if (model === undefined) continue
1727
+ const reached = model.view && model.scope.mode === 'child' ? ownEntry(models, model.scope.parent) ?? model : model
1728
+ const relations = ownKeys(model.relationWhen)
1729
+ for (let position = 0; position < relations.length; position++) {
1730
+ const relation = relations[position] ?? ''
1731
+ const dimension = ownEntry(vocabulary.relations, relation) ?? ''
1732
+ const actions = ownKeys(ownEntry(model.relationWhen, relation) ?? {})
1733
+ for (let next = 0; next < actions.length; next++) {
1734
+ const action = actions[next] ?? ''
1735
+ if (!relationReaches(reached, relation, dimension, action, reveals)) {
1736
+ throw new PolicyError(
1737
+ `${model.name}: relationWhen.${relation}.${action} names a relation that reaches no row of ${reached.name} for ${action}: neither its chain nor a secondary path holds ${dimension}, and no reveal of ${relation} for ${action} names a node it lies at`,
1738
+ )
1739
+ }
1740
+ }
1741
+ }
1742
+ }
1743
+ }
1744
+
1745
+ /** Whether `relation` of `dimension` reaches a row of `model` for `action`, as the relation terms of the store find one. */
1746
+ function relationReaches(model: TableModel, relation: string, dimension: string, action: string, reveals: readonly RevealModel[]): boolean {
1747
+ if (nodeColumnOf(model, dimension) !== undefined || model.access.some((access) => access.relation === relation)) {
1748
+ return true
1749
+ }
1750
+ for (let index = 0; index < model.readThrough.length; index++) {
1751
+ const path = model.readThrough[index]
1752
+ if (path === undefined) continue
1753
+ if (path.dimension === dimension) return true
1754
+ for (let link = 0; link < path.chain.length; link++) {
1755
+ if (path.chain[link]?.dimension === dimension) return true
1756
+ }
1757
+ }
1758
+ for (let index = 0; index < reveals.length; index++) {
1759
+ const reveal = reveals[index]
1760
+ if (
1761
+ reveal !== undefined && reveal.relation === relation && includes(reveal.actions, action) && nodeColumnOf(model, reveal.dimension) !== undefined
1762
+ ) {
1763
+ return true
1764
+ }
1765
+ }
1766
+ return false
1767
+ }
1768
+
1489
1769
  /**
1490
1770
  * The table a `when` of `table` starts at. A child view's parent column is a foreign key to its parent's key that no
1491
1771
  * column list declares, so a path follows it to the parent row, and the parent's own foreign keys from there.
@@ -1583,7 +1863,7 @@ function parseReadThrough(
1583
1863
  )
1584
1864
  }
1585
1865
  requireNotNullAncestors(what, table, pairs)
1586
- paths[paths.length] = freeze({ dimension, column, table: owner.name, key: owner.scope.key, chain: owner.chain, via: undefined })
1866
+ paths[paths.length] = freeze({ dimension, column, table: owner.name, key: owner.scope.key, chain: owner.chain, via: undefined, writes: freeze([]) })
1587
1867
  }
1588
1868
  return freeze(paths)
1589
1869
  }
@@ -1624,7 +1904,64 @@ function throughContainer(
1624
1904
  if (path.column !== column) {
1625
1905
  throw new PolicyError(`${what}: ${container.name} reads ${dimension} through ${path.column}, not ${column}`)
1626
1906
  }
1627
- return freeze({ ...path, via: freeze({ column: link, table: container.name, key: container.scope.key }) })
1907
+ return freeze({ ...path, via: freeze({ column: link, table: container.name, key: container.scope.key }), writes: freeze([]) })
1908
+ }
1909
+
1910
+ /**
1911
+ * `writeThrough`: a dimension the table reads through → the actions a `Ref` held through that path holds. Each is an
1912
+ * action of the vocabulary: the table's own, or one a reveal matched through the path or a table below reaches the row
1913
+ * for. The path's column is the delegation: whoever inserts it must hold every listed action at the node it names, and
1914
+ * for a path through the container, whoever inserts the container's column (`pathWriters`).
1915
+ */
1916
+ function parseWriteThrough(
1917
+ table: LinkedTable,
1918
+ readThrough: readonly SecondaryPath[],
1919
+ input: unknown,
1920
+ vocabulary: VocabularyModel,
1921
+ ): readonly SecondaryPath[] {
1922
+ if (input === undefined) {
1923
+ return readThrough
1924
+ }
1925
+ const declared = plainRecord(input, `${table.name}: writeThrough`)
1926
+ const dimensions = ownKeys(declared)
1927
+ const writes: Record<string, readonly string[]> = {}
1928
+ for (let index = 0; index < dimensions.length; index++) {
1929
+ const dimension = dimensions[index] ?? ''
1930
+ const what = `${table.name}: writeThrough.${dimension}`
1931
+ let declaredPath = false
1932
+ for (let position = 0; position < readThrough.length; position++) {
1933
+ if (readThrough[position]?.dimension === dimension) declaredPath = true
1934
+ }
1935
+ if (!declaredPath) {
1936
+ throw new PolicyError(`${what}: ${table.name} reads no path to ${dimension}; declare readThrough.${dimension} first`)
1937
+ }
1938
+ const actions = parseList(ownEntry(declared, dimension), (action) => {
1939
+ if (typeof action !== 'string' || !inSet(vocabulary.actions, action)) {
1940
+ throw new PolicyError(`${what} names ${String(action)}, which is not an action of the vocabulary`)
1941
+ }
1942
+ return action
1943
+ })
1944
+ if (actions === undefined) {
1945
+ throw new PolicyError(`${what} takes a plain array of the vocabulary's actions`)
1946
+ }
1947
+ const listed: string[] = []
1948
+ for (let position = 0; position < actions.length; position++) {
1949
+ const action = actions[position] ?? ''
1950
+ if (!includes(listed, action)) listed[listed.length] = action
1951
+ }
1952
+ if (listed.length === 0) {
1953
+ throw new PolicyError(`${what} names no action; leave the dimension out to hold nothing through the path`)
1954
+ }
1955
+ setOwn(writes, dimension, freeze(listed))
1956
+ }
1957
+ const paths: SecondaryPath[] = []
1958
+ for (let index = 0; index < readThrough.length; index++) {
1959
+ const path = readThrough[index]
1960
+ if (path === undefined) continue
1961
+ const listed = ownEntry(writes, path.dimension)
1962
+ paths[paths.length] = listed === undefined ? path : freeze({ ...path, writes: listed })
1963
+ }
1964
+ return freeze(paths)
1628
1965
  }
1629
1966
 
1630
1967
  /**
@@ -1743,12 +2080,15 @@ function parseRevealed(
1743
2080
  }
1744
2081
 
1745
2082
  /**
1746
- * `memberReveals`: the table's own dimension → the ancestor its rows reveal to a grant at their node. Only a dimension
1747
- * table reveals so: its key is the node an IAM grant names, and the layer generates it, so no writer picks a node
1748
- * someone already holds a grant at. A queue table's rows move on every claim.
2083
+ * `memberReveals`: the table's own dimension, or one it reads through, → the ancestor its rows reveal to a grant at the
2084
+ * node. By its own key only a dimension table reveals so: the layer generates the key, so no writer picks a node someone
2085
+ * already holds a grant at. Through a direct path the writer picks it, so an insertable table states `assign`, and the
2086
+ * row reveals its nearest container only, whose column no move of an ancestor rewrites. A queue table's rows move on
2087
+ * every claim.
1749
2088
  */
1750
2089
  function parseMemberReveals(
1751
2090
  table: LinkedTable,
2091
+ readThrough: readonly SecondaryPath[],
1752
2092
  input: unknown,
1753
2093
  vocabulary: VocabularyModel,
1754
2094
  tables: (name: string) => ConditionTable | undefined,
@@ -1757,29 +2097,48 @@ function parseMemberReveals(
1757
2097
  if (input === undefined) {
1758
2098
  return freeze([])
1759
2099
  }
1760
- if (table.scope.mode !== 'dimension') {
2100
+ if (table.scope.mode !== 'dimension' && table.scope.mode !== 'child') {
1761
2101
  throw new PolicyError(
1762
- `${table.name}: a ${table.scope.mode} table reveals nothing to members; only a dimension table's rows are nodes a grant names`,
2102
+ `${table.name}: a ${table.scope.mode} table reveals nothing to members; only a dimension or child table's rows name a node a grant is at`,
1763
2103
  )
1764
2104
  }
1765
2105
  if (table.queue !== undefined) {
1766
2106
  throw new PolicyError(`${table.name}: a queue table reveals nothing; the layer rewrites its rows on every claim`)
1767
2107
  }
1768
- const own = table.scope.dimension
1769
- const key = table.scope.key
2108
+ const own = table.scope.mode === 'dimension' ? table.scope.dimension : undefined
1770
2109
  const declared = plainRecord(input, `${table.name}: memberReveals`)
1771
2110
  const names = ownKeys(declared)
1772
2111
  const reveals: MemberRevealModel[] = []
1773
2112
  for (let index = 0; index < names.length; index++) {
1774
2113
  const source = names[index] ?? ''
1775
2114
  const what = `${table.name}: memberReveals.${source}`
1776
- if (source !== own) {
1777
- throw new PolicyError(`${what}: ${source} is not ${table.name}'s own dimension (${own}); a row reveals to a grant at its own node only`)
2115
+ let path: SecondaryPath | undefined
2116
+ for (let position = 0; source !== own && position < readThrough.length; position++) {
2117
+ if (readThrough[position]?.dimension === source) path = readThrough[position]
2118
+ }
2119
+ if (source !== own && path === undefined) {
2120
+ throw new PolicyError(
2121
+ `${what}: ${source} is neither the own dimension of ${table.name} (${
2122
+ own ?? 'none'
2123
+ }) nor one it reads through; a row reveals to a grant at its own node or at the node its path names`,
2124
+ )
1778
2125
  }
1779
2126
  const policy = plainRecord(ownEntry(declared, source), what)
1780
2127
  onlyFields(policy, MEMBER_REVEALS_FIELDS, what)
1781
2128
  const { to, column, actions, when } = parseRevealed(table, policy, what, vocabulary, tables, hops)
1782
- reveals[reveals.length] = freeze({ source, table: table.name, key, dimension: to, column, actions, when })
2129
+ if (path?.via !== undefined) {
2130
+ throw new PolicyError(
2131
+ `${what}: ${table.name} reads ${source} through its container ${path.via.table}; declare the member reveal on ${path.via.table}`,
2132
+ )
2133
+ }
2134
+ if (path !== undefined && table.chain[0]?.dimension !== to) {
2135
+ throw new PolicyError(
2136
+ `${what}.to: ${to} lies above ${table.name}'s nearest container; a move of an ancestor rewrites ${column}, so a member reveal through a path reveals the nearest container only`,
2137
+ )
2138
+ }
2139
+ const assign = parseRevealAssign(table, path, ownEntry(policy, 'assign'), what)
2140
+ const key = path === undefined ? table.scope.key : path.column
2141
+ reveals[reveals.length] = freeze({ source, table: table.name, key, path, assign, dimension: to, column, actions, when })
1783
2142
  }
1784
2143
  return freeze(reveals)
1785
2144
  }
@@ -1828,16 +2187,17 @@ export function delegates(model: TableModel): boolean {
1828
2187
 
1829
2188
  /**
1830
2189
  * The tables whose rows hand out access, which no raw statement writes, lowercased as the tag compares names: a relation
1831
- * source or a path reveal, whose writer the layer bounds; a member-revealing table, whose key the layer generates so no
1832
- * write revives a node a stale grant still names; and the tables a principal path or a relation's condition reads, whose
1833
- * columns decide whom a source row names and what it confers.
2190
+ * source, a path reveal or a path column that holds actions (`writeThrough`), whose writer the layer bounds; a
2191
+ * member-revealing table, whose key the layer generates so no write revives a node a stale grant still names; and the
2192
+ * tables a principal path or a relation's condition reads, whose columns decide whom a source row names and what it
2193
+ * confers.
1834
2194
  */
1835
2195
  export function accessTablesOf(models: TableModels): readonly string[] {
1836
2196
  const tables: string[] = []
1837
2197
  const names = ownKeys(models)
1838
2198
  for (let index = 0; index < names.length; index++) {
1839
2199
  const model = modelOf(models, names[index] ?? '')
1840
- if (model !== undefined && delegates(model)) {
2200
+ if (model !== undefined && (delegates(model) || pathWriters(models, model).length > 0)) {
1841
2201
  tables[tables.length] = model.name.toLowerCase()
1842
2202
  }
1843
2203
  for (let position = 0; model !== undefined && position < model.confers.length; position++) {
@@ -1854,6 +2214,45 @@ export function accessTablesOf(models: TableModels): readonly string[] {
1854
2214
  return tables
1855
2215
  }
1856
2216
 
2217
+ /** A path column whose insert hands out the listed actions at the node it names: the bound its writer is held to. */
2218
+ export interface PathWriter {
2219
+ readonly path: SecondaryPath
2220
+ /** The path's own `writes` and those of every table that reads the same dimension through this table's column. */
2221
+ readonly actions: readonly string[]
2222
+ }
2223
+
2224
+ /**
2225
+ * The columns of `model` whose insert hands out actions through a secondary path: its own paths that hold actions, and
2226
+ * its paths that a table below follows through it (`'<link>.<column>'`) with actions of its own. Such a table is written
2227
+ * only through the handles, so no write sets the column past the bound.
2228
+ */
2229
+ export function pathWriters(models: Readonly<Record<string, TableModel>>, model: TableModel): readonly PathWriter[] {
2230
+ const writers: PathWriter[] = []
2231
+ const names = ownKeys(models)
2232
+ for (let index = 0; index < model.readThrough.length; index++) {
2233
+ const path = model.readThrough[index]
2234
+ if (path === undefined || path.via !== undefined) continue
2235
+ const actions: string[] = []
2236
+ for (let position = 0; position < path.writes.length; position++) {
2237
+ const action = path.writes[position] ?? ''
2238
+ if (!includes(actions, action)) actions[actions.length] = action
2239
+ }
2240
+ for (let position = 0; position < names.length; position++) {
2241
+ const below = ownEntry(models, names[position] ?? '')
2242
+ for (let at = 0; below !== undefined && at < below.readThrough.length; at++) {
2243
+ const through = below.readThrough[at]
2244
+ if (through?.via?.table !== model.name || through.dimension !== path.dimension) continue
2245
+ for (let written = 0; written < through.writes.length; written++) {
2246
+ const action = through.writes[written] ?? ''
2247
+ if (!includes(actions, action)) actions[actions.length] = action
2248
+ }
2249
+ }
2250
+ }
2251
+ if (actions.length > 0) writers[writers.length] = freeze({ path, actions: freeze(actions) })
2252
+ }
2253
+ return freeze(writers)
2254
+ }
2255
+
1857
2256
  function containsLink(chain: readonly Container[], dimension: string): boolean {
1858
2257
  for (let index = 0; index < chain.length; index++) {
1859
2258
  if (chain[index]?.dimension === dimension) return true