@fabrika/db 0.9.0-dev.19 → 0.9.0-dev.20
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/package.json +4 -4
- package/src/authority.ts +23 -2
- package/src/capability.ts +8 -2
- package/src/file-columns.ts +28 -10
- package/src/orm-types.ts +88 -28
- package/src/orm.ts +102 -71
- package/src/policy.ts +118 -7
- package/src/registry.ts +1 -1
- package/src/store.ts +392 -87
- package/src/tables.ts +2 -1
- package/src/testing-shared.ts +1 -1
- package/src/testing-sqlite.ts +1 -1
- package/src/vocabulary-tables.ts +2 -1
- package/types/src/authority.d.ts +6 -0
- package/types/src/capability.d.ts +8 -2
- package/types/src/file-columns.d.ts +3 -3
- package/types/src/orm-types.d.ts +54 -25
- package/types/src/policy.d.ts +54 -4
- package/types/src/registry.d.ts +1 -1
- package/types/src/store.d.ts +47 -18
- package/types/src/testing-shared.d.ts +1 -1
- package/types/src/testing-sqlite.d.ts +1 -1
- package/types/src/vocabulary-tables.d.ts +2 -2
package/src/orm.ts
CHANGED
|
@@ -42,18 +42,33 @@ import {
|
|
|
42
42
|
withCursorColumns,
|
|
43
43
|
} from './nodes'
|
|
44
44
|
import type { DataKind, TargetForm, UnscopedMode } from './orm-types'
|
|
45
|
-
import {
|
|
45
|
+
import {
|
|
46
|
+
accessTablesOf,
|
|
47
|
+
delegates,
|
|
48
|
+
modelOf,
|
|
49
|
+
namesAction,
|
|
50
|
+
type ReadReach,
|
|
51
|
+
soleReach,
|
|
52
|
+
type TableModels,
|
|
53
|
+
updatableBy,
|
|
54
|
+
visibleFor,
|
|
55
|
+
visibleThrough,
|
|
56
|
+
} from './policy'
|
|
46
57
|
import { type SqlSite, type SqlTag, sqlTag } from './sql'
|
|
47
58
|
import {
|
|
48
59
|
actionShape,
|
|
49
60
|
aggregateRows,
|
|
50
61
|
type AggregateSpec,
|
|
51
62
|
answer,
|
|
63
|
+
appGetScope,
|
|
52
64
|
appKeyPredicate,
|
|
53
65
|
appKeyScope,
|
|
54
66
|
appListPredicate,
|
|
55
|
-
|
|
67
|
+
appListRead,
|
|
68
|
+
type AppRead,
|
|
69
|
+
appRecordRead,
|
|
56
70
|
appRowPredicate,
|
|
71
|
+
appUpdate,
|
|
57
72
|
boundTerms,
|
|
58
73
|
boundValues,
|
|
59
74
|
childObjectPredicate,
|
|
@@ -85,7 +100,6 @@ import {
|
|
|
85
100
|
selfLinks,
|
|
86
101
|
type TableContext,
|
|
87
102
|
updateRow,
|
|
88
|
-
updatingApp,
|
|
89
103
|
updatingRef,
|
|
90
104
|
versionArgument,
|
|
91
105
|
} from './store'
|
|
@@ -343,7 +357,7 @@ function targetRow(
|
|
|
343
357
|
const onOnly = (operation: string) => `${operation} acts on one object: declare access with on`
|
|
344
358
|
const get = bound(site, async () => {
|
|
345
359
|
const grant = namedRef(context, capability, dimension)
|
|
346
|
-
return selectOne(context, run, targetPredicate(context, grant), visibleFor(context.model, grant.action), grant.action)
|
|
360
|
+
return selectOne(context, run, targetPredicate(context, grant), visibleFor(context.model, grant.action), soleReach(grant.action))
|
|
347
361
|
})
|
|
348
362
|
const lock = bound(site, async () => {
|
|
349
363
|
const grant = namedRef(context, capability, dimension)
|
|
@@ -362,7 +376,7 @@ function targetRow(
|
|
|
362
376
|
if (row === undefined) {
|
|
363
377
|
throw new NotFoundError(`${context.model.name} not found`)
|
|
364
378
|
}
|
|
365
|
-
await answerFiles(context.model, run.files, session, [row], grant.action)
|
|
379
|
+
await answerFiles(context.model, run.files, session, [row], soleReach(grant.action))
|
|
366
380
|
return row
|
|
367
381
|
})
|
|
368
382
|
const getForUpdate = site.kind === 'job'
|
|
@@ -395,14 +409,14 @@ function childRows(context: TableContext, site: DataSite, run: Runner, capabilit
|
|
|
395
409
|
return freeze({
|
|
396
410
|
list: bound(site, async (options?: unknown) => {
|
|
397
411
|
const grant = namedRef(context, capability, parent)
|
|
398
|
-
return listRows(context, run, parentPredicate(context, grant), visibleFor(context.model, grant.action), options, [key], grant.action)
|
|
412
|
+
return listRows(context, run, parentPredicate(context, grant), visibleFor(context.model, grant.action), options, [key], soleReach(grant.action))
|
|
399
413
|
}),
|
|
400
414
|
get: bound(site, async (id: unknown) => {
|
|
401
415
|
const grant = namedRef(context, capability, parent)
|
|
402
416
|
if (typeof id !== 'string' || id.length === 0) {
|
|
403
417
|
throw new QueryRefusedError('row_address', `a ${context.model.name} row is addressed by its ${key}, a non-empty string`)
|
|
404
418
|
}
|
|
405
|
-
return selectOne(context, run, parentPredicate(context, grant, id), visibleFor(context.model, grant.action), grant.action)
|
|
419
|
+
return selectOne(context, run, parentPredicate(context, grant, id), visibleFor(context.model, grant.action), soleReach(grant.action))
|
|
406
420
|
}),
|
|
407
421
|
insert: writing(
|
|
408
422
|
site,
|
|
@@ -429,7 +443,7 @@ function containedRows(context: TableContext, site: DataSite, run: Runner, capab
|
|
|
429
443
|
list: bound(site, async (options?: unknown) => {
|
|
430
444
|
const grant = namedRef(context, capability, dimension)
|
|
431
445
|
const scope = refScope(context, context.model, context.model.chain, grant, new Date())
|
|
432
|
-
return listRows(context, run, scope, visibleFor(context.model, grant.action), options, [keyOf(context.model)], grant.action)
|
|
446
|
+
return listRows(context, run, scope, visibleFor(context.model, grant.action), options, [keyOf(context.model)], soleReach(grant.action))
|
|
433
447
|
}),
|
|
434
448
|
insert: writing(
|
|
435
449
|
site,
|
|
@@ -486,7 +500,7 @@ function listedRows(
|
|
|
486
500
|
claim: claiming(parts, context, site, capability),
|
|
487
501
|
list: bound(site, async (options?: unknown) => {
|
|
488
502
|
const predicate = scope()
|
|
489
|
-
return listRows(context, run, predicate, visible(), options, key, heldAction(context, capability))
|
|
503
|
+
return listRows(context, run, predicate, visible(), options, key, soleReach(heldAction(context, capability)))
|
|
490
504
|
}),
|
|
491
505
|
count: bound(site, async (options?: unknown) => {
|
|
492
506
|
const predicate = scope()
|
|
@@ -516,31 +530,33 @@ function applicationTables(parts: DataLayerParts, site: DataSite, run: Runner, c
|
|
|
516
530
|
const rows = applicationRows(parts, context, site, run, capability)
|
|
517
531
|
setOwn(handle, model.name, site.public === true ? publicApplicationRows(context, site, run, capability, rows) : freeze(rows))
|
|
518
532
|
} else if (site.public !== true && (model.scope.mode === 'dimension' || model.scope.mode === 'child')) {
|
|
519
|
-
setOwn(handle, model.name, appRecordRows(context, site, run, capability
|
|
533
|
+
setOwn(handle, model.name, appRecordRows(context, site, run, capability))
|
|
520
534
|
}
|
|
521
535
|
}
|
|
522
536
|
return freeze(handle)
|
|
523
537
|
}
|
|
524
538
|
|
|
525
539
|
/**
|
|
526
|
-
* A dimension or child table under an `app` capability
|
|
527
|
-
*
|
|
540
|
+
* A dimension or child table under an `app` capability, read-only and every row, with the columns its reaching actions
|
|
541
|
+
* read alone (`visibleThrough`): listed and aggregated with its read action or an action of its `appRead`, counted with
|
|
528
542
|
* those or an action of its `appCount`.
|
|
529
543
|
*/
|
|
530
|
-
function appRecordRows(context: TableContext, site: DataSite, run: Runner, capability: unknown
|
|
531
|
-
const
|
|
532
|
-
const
|
|
533
|
-
const key = [keyOf(context.model)]
|
|
544
|
+
function appRecordRows(context: TableContext, site: DataSite, run: Runner, capability: unknown): object {
|
|
545
|
+
const model = context.model
|
|
546
|
+
const key = [keyOf(model)]
|
|
534
547
|
return freeze({
|
|
535
|
-
list: bound(
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
),
|
|
539
|
-
count: bound(site, async (options?: unknown) =>
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
)
|
|
548
|
+
list: bound(site, async (options?: unknown) => {
|
|
549
|
+
const { predicate, reach } = appRecordRead(context, capability, 'rows')
|
|
550
|
+
return listRows(context, run, predicate, visibleThrough(model, reach), options, key, reach)
|
|
551
|
+
}),
|
|
552
|
+
count: bound(site, async (options?: unknown) => {
|
|
553
|
+
const { predicate, reach } = appRecordRead(context, capability, 'counts')
|
|
554
|
+
return countRows(context, run, predicate, visibleThrough(model, reach), options)
|
|
555
|
+
}),
|
|
556
|
+
aggregate: bound(site, async <Row>(spec: AggregateSpec<Row>) => {
|
|
557
|
+
const { predicate, reach } = appRecordRead(context, capability, 'rows')
|
|
558
|
+
return aggregateRows(context, run, predicate, actionShape(model, visibleThrough(model, reach)), spec)
|
|
559
|
+
}),
|
|
544
560
|
})
|
|
545
561
|
}
|
|
546
562
|
|
|
@@ -564,31 +580,54 @@ function scopedApplicationRows(context: TableContext, site: DataSite, run: Runne
|
|
|
564
580
|
const key = [keyOf(model)]
|
|
565
581
|
const scope = () => scopedApplicationRead(context, capability)
|
|
566
582
|
return freeze({
|
|
567
|
-
list: bound(site, async (options?: unknown) => listRows(context, run, scope(), visible, options, key, heldAction(context, capability))),
|
|
583
|
+
list: bound(site, async (options?: unknown) => listRows(context, run, scope(), visible, options, key, soleReach(heldAction(context, capability)))),
|
|
568
584
|
count: bound(site, async (options?: unknown) => countRows(context, run, scope(), visible, options)),
|
|
569
585
|
aggregate: bound(site, async <Row>(spec: AggregateSpec<Row>) => aggregateRows(context, run, scope(), context.shape, spec)),
|
|
570
586
|
get: bound(
|
|
571
587
|
site,
|
|
572
588
|
async (id: unknown) =>
|
|
573
|
-
selectOne(
|
|
589
|
+
selectOne(
|
|
590
|
+
context,
|
|
591
|
+
run,
|
|
592
|
+
scopedApplicationRead(context, capability, appKeyPredicate(model, id)),
|
|
593
|
+
visible,
|
|
594
|
+
soleReach(heldAction(context, capability)),
|
|
595
|
+
),
|
|
574
596
|
),
|
|
575
597
|
})
|
|
576
598
|
}
|
|
577
599
|
|
|
578
|
-
/**
|
|
579
|
-
|
|
600
|
+
/**
|
|
601
|
+
* The row `key` names, for an `app` capability with an action the application table names, or any but a public
|
|
602
|
+
* procedure's on a table read with `'any'`: within the conditions of the actions it holds that the table names, any one
|
|
603
|
+
* admitting the row, and with the columns they read alone (`visibleThrough`).
|
|
604
|
+
*/
|
|
605
|
+
function namedAppRead(context: TableContext, capability: unknown, key: unknown): AppRead {
|
|
580
606
|
const grant = runtimeOf(context.connection).capabilities.resolve(capability)
|
|
581
|
-
const
|
|
607
|
+
const model = context.model
|
|
608
|
+
const table = model.name
|
|
582
609
|
if (grant.kind !== 'app') {
|
|
583
610
|
throw new CapabilityError('wrong_capability', `a read of ${table} here needs the capability of an app endpoint, got ${describeGrant(grant)}`)
|
|
584
611
|
}
|
|
585
|
-
if (
|
|
612
|
+
if (model.actions.readAny) {
|
|
613
|
+
// Refuses a public procedure's grant, which reads no table read with 'any'.
|
|
614
|
+
appListPredicate(context, capability)
|
|
615
|
+
return { predicate: appGetScope(model, grant.actions, key), reach: freeze({ actions: grant.actions, narrowed: false }) }
|
|
616
|
+
}
|
|
617
|
+
const named: string[] = []
|
|
618
|
+
for (let index = 0; index < grant.actions.length; index++) {
|
|
619
|
+
const action = grant.actions[index]
|
|
620
|
+
if (action !== undefined && namesAction(model, action)) {
|
|
621
|
+
named[named.length] = action
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
if (named.length === 0) {
|
|
586
625
|
throw new CapabilityError(
|
|
587
626
|
'wrong_action',
|
|
588
|
-
`${table} does not name the action '${grant.
|
|
627
|
+
`${table} does not name the action '${grant.actions.join("', '")}': read it from an app endpoint whose action ${table} names`,
|
|
589
628
|
)
|
|
590
629
|
}
|
|
591
|
-
return
|
|
630
|
+
return { predicate: appGetScope(model, named, key), reach: freeze({ actions: named, narrowed: true }) }
|
|
592
631
|
}
|
|
593
632
|
|
|
594
633
|
/**
|
|
@@ -604,39 +643,39 @@ function applicationRows(
|
|
|
604
643
|
): Readonly<Record<string, unknown>> {
|
|
605
644
|
const model = context.model
|
|
606
645
|
const table = model.name
|
|
607
|
-
const visible = () => visibleFor(model, heldAction(context, capability))
|
|
608
646
|
const key = [keyOf(model)]
|
|
609
647
|
const getForUpdate = site.kind === 'job'
|
|
610
648
|
? refused(site, 'read_only', 'a job writes inside ctx.transaction((tx) => …): use tx.db there')
|
|
611
649
|
: site.kind === 'query'
|
|
612
650
|
? refused(site, 'read_only', 'getForUpdate locks the row, so it runs only in a mutation: declare the endpoint with r.mutation')
|
|
613
651
|
: bound(site, async (id: unknown) => {
|
|
614
|
-
const
|
|
615
|
-
const
|
|
616
|
-
const node = lockedSelectNode(table, visibleFor(model, action), scope, model.dates)
|
|
652
|
+
const { predicate: scope, reach } = namedAppRead(context, capability, id)
|
|
653
|
+
const node = lockedSelectNode(table, visibleThrough(model, reach), scope, model.dates)
|
|
617
654
|
const session = await run.read()
|
|
618
655
|
const rows = await session.run(node, { root: 'SelectQueryNode', table, joins: {}, scope, lock: 'update', subqueries: context.subqueries })
|
|
619
656
|
const [row] = answerRows(dialectOf(context), table, rowColumnsOf(model), rows)
|
|
620
657
|
if (row === undefined) {
|
|
621
658
|
throw new NotFoundError(`${table} not found`)
|
|
622
659
|
}
|
|
623
|
-
await answerFiles(model, run.files, session, [row],
|
|
660
|
+
await answerFiles(model, run.files, session, [row], reach)
|
|
624
661
|
return row
|
|
625
662
|
})
|
|
626
663
|
return {
|
|
627
|
-
list: bound(
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
)
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
664
|
+
list: bound(site, async (options?: unknown) => {
|
|
665
|
+
const { predicate, reach } = appListRead(context, capability)
|
|
666
|
+
return listRows(context, run, predicate, visibleThrough(model, reach), options, key, reach)
|
|
667
|
+
}),
|
|
668
|
+
count: bound(site, async (options?: unknown) => {
|
|
669
|
+
const { predicate, reach } = appListRead(context, capability)
|
|
670
|
+
return countRows(context, run, predicate, visibleThrough(model, reach), options)
|
|
671
|
+
}),
|
|
672
|
+
aggregate: bound(site, async <Row>(spec: AggregateSpec<Row>) => {
|
|
673
|
+
const { predicate, reach } = appListRead(context, capability)
|
|
674
|
+
return aggregateRows(context, run, predicate, actionShape(model, visibleThrough(model, reach)), spec)
|
|
675
|
+
}),
|
|
637
676
|
get: bound(site, async (id: unknown) => {
|
|
638
|
-
const
|
|
639
|
-
return selectOne(context, run,
|
|
677
|
+
const { predicate, reach } = namedAppRead(context, capability, id)
|
|
678
|
+
return selectOne(context, run, predicate, visibleThrough(model, reach), reach)
|
|
640
679
|
}),
|
|
641
680
|
getForUpdate,
|
|
642
681
|
claim: claiming(parts, context, site, capability),
|
|
@@ -646,9 +685,8 @@ function applicationRows(
|
|
|
646
685
|
async (values: unknown, options?: unknown) => insertPrepared(context, run, prepareApplicationInsert(context, capability, values, options)),
|
|
647
686
|
),
|
|
648
687
|
update: writing(site, 'update', async (id: unknown, values: unknown, ...options: unknown[]) => {
|
|
649
|
-
const
|
|
650
|
-
|
|
651
|
-
return updateRow(context, run, { scope: appKeyScope(model, grant.action, id) }, changes, versionArgument(context, options))
|
|
688
|
+
const { scope, changes } = appUpdate(context, capability, id, values)
|
|
689
|
+
return updateRow(context, run, { scope }, changes, versionArgument(context, options))
|
|
652
690
|
}),
|
|
653
691
|
remove: writing(site, 'remove', async (id: unknown, ...options: unknown[]) => {
|
|
654
692
|
await removeRow(context, run, { scope: appRowPredicate(context, capability, id, 'delete') }, versionArgument(context, options))
|
|
@@ -690,21 +728,14 @@ function publicApplicationRows(
|
|
|
690
728
|
get: bound(site, async (id: unknown) => {
|
|
691
729
|
appListPredicate(context, capability)
|
|
692
730
|
const action = heldAction(context, capability)
|
|
693
|
-
return selectOne(context, run, appKeyScope(model, action, id), visibleFor(model, action), action)
|
|
731
|
+
return selectOne(context, run, appKeyScope(model, [action], id), visibleFor(model, action), soleReach(action))
|
|
694
732
|
}),
|
|
695
733
|
getForUpdate: refusal('lock'),
|
|
696
734
|
claim: refusal('claim'),
|
|
697
735
|
update: writing(site, 'update', async (id: unknown, values: unknown, ...options: unknown[]) => {
|
|
698
736
|
tokenRow('update', id)
|
|
699
|
-
const
|
|
700
|
-
const
|
|
701
|
-
const saved = await updateRow<Readonly<Record<string, unknown>>>(
|
|
702
|
-
context,
|
|
703
|
-
run,
|
|
704
|
-
{ scope: appKeyScope(model, grant.action, id) },
|
|
705
|
-
changes,
|
|
706
|
-
versionArgument(context, options),
|
|
707
|
-
)
|
|
737
|
+
const { scope, changes } = appUpdate(context, capability, id, values)
|
|
738
|
+
const saved = await updateRow<Readonly<Record<string, unknown>>>(context, run, { scope }, changes, versionArgument(context, options))
|
|
708
739
|
return freeze({ version: saved['version'] })
|
|
709
740
|
}),
|
|
710
741
|
remove: writing(site, 'remove', async (id: unknown, ...options: unknown[]) => {
|
|
@@ -795,7 +826,7 @@ async function claimRows(
|
|
|
795
826
|
): Promise<readonly Readonly<Record<string, unknown>>[]> {
|
|
796
827
|
const model = context.model
|
|
797
828
|
const table = model.name
|
|
798
|
-
const { grant, scope } = claimScope(context, capability)
|
|
829
|
+
const { grant, action, scope } = claimScope(context, capability)
|
|
799
830
|
const queue = model.queue
|
|
800
831
|
const version = model.version
|
|
801
832
|
if (queue === undefined || version === undefined) {
|
|
@@ -806,7 +837,7 @@ async function claimRows(
|
|
|
806
837
|
throw new QueryRefusedError('list_page', 'claim takes { limit }: attempts count claims, so claim only what the run will reach')
|
|
807
838
|
}
|
|
808
839
|
const limit = limitOf(options['limit'])
|
|
809
|
-
const visible = visibleFor(model,
|
|
840
|
+
const visible = visibleFor(model, action)
|
|
810
841
|
const conditions = conditionsOf(context, actionShape(model, visible), options['where'])
|
|
811
842
|
if (scope === undefined) {
|
|
812
843
|
return []
|
|
@@ -889,7 +920,7 @@ async function claimRows(
|
|
|
889
920
|
site.files,
|
|
890
921
|
session,
|
|
891
922
|
answerRows(dialect, table, rowColumnsOf(model), rows),
|
|
892
|
-
|
|
923
|
+
soleReach(action),
|
|
893
924
|
nameSet(site.holds ?? []),
|
|
894
925
|
),
|
|
895
926
|
claimed,
|
|
@@ -905,7 +936,7 @@ async function claimRows(
|
|
|
905
936
|
for (const name of chain) {
|
|
906
937
|
links.set(name, String(row[name]))
|
|
907
938
|
}
|
|
908
|
-
const ref = runtimeOf(context.connection).capabilities.row({ table, action
|
|
939
|
+
const ref = runtimeOf(context.connection).capabilities.row({ table, action, principal: grant.principal, id, chain: links })
|
|
909
940
|
const answered: Record<string, unknown> = {}
|
|
910
941
|
for (const name of visible) {
|
|
911
942
|
setOwn(answered, name, row[name])
|
|
@@ -1058,7 +1089,7 @@ function unscopedRows(context: TableContext, site: DataSite, run: Runner, mode:
|
|
|
1058
1089
|
return freeze({
|
|
1059
1090
|
list: bound(
|
|
1060
1091
|
site,
|
|
1061
|
-
async (options?: unknown) => listRows(context, run, everything(), visible, options, keyed ? [keyOf(model)] : visible, unscopedRead),
|
|
1092
|
+
async (options?: unknown) => listRows(context, run, everything(), visible, options, keyed ? [keyOf(model)] : visible, soleReach(unscopedRead)),
|
|
1062
1093
|
),
|
|
1063
1094
|
count: bound(site, async (options?: unknown) => countRows(context, run, everything(), visible, options)),
|
|
1064
1095
|
aggregate: bound(site, async <Row>(spec: AggregateSpec<Row>) => aggregateRows(context, run, everything(), context.shape, spec)),
|
|
@@ -1072,7 +1103,7 @@ function unscopedRows(context: TableContext, site: DataSite, run: Runner, mode:
|
|
|
1072
1103
|
if (row === undefined) {
|
|
1073
1104
|
throw new NotFoundError(`${table} not found`)
|
|
1074
1105
|
}
|
|
1075
|
-
await answerFiles(model, run.files, session, [row], unscopedRead)
|
|
1106
|
+
await answerFiles(model, run.files, session, [row], soleReach(unscopedRead))
|
|
1076
1107
|
return row
|
|
1077
1108
|
}),
|
|
1078
1109
|
insert: write('insert', async (values: unknown) => insertUnscoped(context, run, site.principal, values)),
|
|
@@ -1306,7 +1337,7 @@ async function listRows(
|
|
|
1306
1337
|
visible: readonly string[],
|
|
1307
1338
|
input: unknown,
|
|
1308
1339
|
tiebreak: readonly string[],
|
|
1309
|
-
|
|
1340
|
+
reach?: ReadReach,
|
|
1310
1341
|
): Promise<{ readonly items: Readonly<Record<string, unknown>>[]; readonly next: string | null }> {
|
|
1311
1342
|
const table = context.model.name
|
|
1312
1343
|
const options = readOptions(input, ['where', 'orderBy', 'limit', 'after'], 'list')
|
|
@@ -1337,7 +1368,7 @@ async function listRows(
|
|
|
1337
1368
|
const last = page[page.length - 1]
|
|
1338
1369
|
const items = page.map((row) => withoutCursor(row, order))
|
|
1339
1370
|
return {
|
|
1340
|
-
items:
|
|
1371
|
+
items: reach === undefined ? items : await answerFiles(context.model, run.files, session, items, reach),
|
|
1341
1372
|
next: rows.length > limit && last !== undefined ? encodeCursor(last, order) : null,
|
|
1342
1373
|
}
|
|
1343
1374
|
}
|
package/src/policy.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { READ_ANY } from '@fabrika/auth-core'
|
|
1
2
|
import type { Insertable, Selectable, SelectType, Updateable } from 'kysely'
|
|
2
3
|
import {
|
|
3
4
|
freeze,
|
|
@@ -58,6 +59,8 @@ export interface ApplicationScope<Col extends string> {
|
|
|
58
59
|
/** One action, or a list of them: a table that staff and a public procedure both read names both actions. */
|
|
59
60
|
export type TableAction<A extends string> = A | readonly A[]
|
|
60
61
|
|
|
62
|
+
export { READ_ANY }
|
|
63
|
+
|
|
61
64
|
export interface TableActions<A extends string> {
|
|
62
65
|
readonly read: TableAction<A>
|
|
63
66
|
readonly insert?: TableAction<A>
|
|
@@ -65,6 +68,11 @@ export interface TableActions<A extends string> {
|
|
|
65
68
|
readonly delete?: TableAction<A>
|
|
66
69
|
}
|
|
67
70
|
|
|
71
|
+
/** An application table's actions: its read may be `'any'` instead of its actions (`READ_ANY`). */
|
|
72
|
+
export interface ApplicationActions<A extends string> extends Omit<TableActions<A>, 'read'> {
|
|
73
|
+
readonly read: TableAction<A> | typeof READ_ANY
|
|
74
|
+
}
|
|
75
|
+
|
|
68
76
|
/**
|
|
69
77
|
* A queue table's columns (ADR-0060's drain): `due` is when a row is next claimed, `null` once it is done; `attempts`
|
|
70
78
|
* counts its claims, and a row with `maxAttempts` (default 10) of them is no longer claimed.
|
|
@@ -223,7 +231,9 @@ export interface ChildPolicy<Table extends string, Col extends string, A extends
|
|
|
223
231
|
}
|
|
224
232
|
|
|
225
233
|
/** An application table: written like a dimension table, by an `app` endpoint whose action is the operation's. */
|
|
226
|
-
export interface ApplicationPolicy<Col extends string, A extends string, V extends Col = Col>
|
|
234
|
+
export interface ApplicationPolicy<Col extends string, A extends string, V extends Col = Col>
|
|
235
|
+
extends WritablePolicy<Col, A, V, ApplicationActions<A>>
|
|
236
|
+
{
|
|
227
237
|
readonly scope: ApplicationScope<Col>
|
|
228
238
|
}
|
|
229
239
|
|
|
@@ -287,9 +297,17 @@ export type ScopeModel =
|
|
|
287
297
|
| { readonly mode: 'reference'; readonly reason: string }
|
|
288
298
|
| { readonly mode: 'app'; readonly reason: string; readonly key: string }
|
|
289
299
|
|
|
290
|
-
/**
|
|
300
|
+
/**
|
|
301
|
+
* Each operation's actions: any one of them allows it. `read` names at least one, unless `readAny`; an empty list
|
|
302
|
+
* allows no operation.
|
|
303
|
+
*/
|
|
291
304
|
export interface ActionModel {
|
|
292
305
|
readonly read: readonly string[]
|
|
306
|
+
/**
|
|
307
|
+
* An application table read with `'any'`: every `app` endpoint, job and webhook reads it, whatever its action; never a
|
|
308
|
+
* public procedure or a scoped site. `read` is empty then, and a `when` narrows only the writes of its action.
|
|
309
|
+
*/
|
|
310
|
+
readonly readAny: boolean
|
|
293
311
|
readonly insert: readonly string[]
|
|
294
312
|
readonly update: readonly string[]
|
|
295
313
|
readonly delete: readonly string[]
|
|
@@ -591,6 +609,9 @@ function parseVocabulary(input: unknown): VocabularyModel {
|
|
|
591
609
|
}
|
|
592
610
|
}
|
|
593
611
|
const actions = ownKeys(plainRecord(ownEntry(vocabulary, 'actions'), 'the vocabulary actions'))
|
|
612
|
+
if (includes(actions, READ_ANY)) {
|
|
613
|
+
throw new PolicyError(`the vocabulary may not declare the action '${READ_ANY}': a table's actions.read says '${READ_ANY}' for every action`)
|
|
614
|
+
}
|
|
594
615
|
const relationsInput = ownEntry(vocabulary, 'relations')
|
|
595
616
|
const relations: Record<string, string> = {}
|
|
596
617
|
if (relationsInput !== undefined) {
|
|
@@ -1032,6 +1053,24 @@ export function updatableBy(model: TableModel, action: string): NameSet {
|
|
|
1032
1053
|
return nameSet(columns)
|
|
1033
1054
|
}
|
|
1034
1055
|
|
|
1056
|
+
/**
|
|
1057
|
+
* The columns any of `actions` updates in `model`, as one role holding them: the union of `updatableBy` over them.
|
|
1058
|
+
* One action's set is `updatableBy`'s own.
|
|
1059
|
+
*/
|
|
1060
|
+
export function updatableByAny(model: TableModel, actions: readonly string[]): NameSet {
|
|
1061
|
+
if (actions.length === 1) {
|
|
1062
|
+
return updatableBy(model, actions[0] ?? '')
|
|
1063
|
+
}
|
|
1064
|
+
const columns: string[] = []
|
|
1065
|
+
for (let index = 0; index < actions.length; index++) {
|
|
1066
|
+
const names = namesOf(updatableBy(model, actions[index] ?? ''))
|
|
1067
|
+
for (let position = 0; position < names.length; position++) {
|
|
1068
|
+
columns[columns.length] = names[position] ?? ''
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
return nameSet(columns)
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1035
1074
|
/** Whether the table names `action`: one of its table actions, or a column's own write action. */
|
|
1036
1075
|
export function namesAction(model: TableModel, action: string): boolean {
|
|
1037
1076
|
const { read, insert, update, delete: remove } = model.actions
|
|
@@ -1052,12 +1091,47 @@ export function namesAction(model: TableModel, action: string): boolean {
|
|
|
1052
1091
|
* one. A capability reads its own row, so a write `Ref` sees the columns of its action and none of another's.
|
|
1053
1092
|
*/
|
|
1054
1093
|
export function visibleFor(model: TableModel, action: string): readonly string[] {
|
|
1094
|
+
return visibleThrough(model, soleReach(action))
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
/**
|
|
1098
|
+
* The actions whose reach one read answers: an `app` capability's held actions that admit the operation, each row
|
|
1099
|
+
* passing where one of them admits it (`conditionedAny`), and whether their `when` narrows the rows (`narrowed`), which
|
|
1100
|
+
* it does not in a table read with `'any'`.
|
|
1101
|
+
*/
|
|
1102
|
+
export interface ReadReach {
|
|
1103
|
+
readonly actions: readonly string[]
|
|
1104
|
+
readonly narrowed: boolean
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
/** The reach of a capability of one action, whose own condition narrows its rows. */
|
|
1108
|
+
export function soleReach(action: string): ReadReach {
|
|
1109
|
+
return freeze({ actions: freeze([action]), narrowed: true })
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
/**
|
|
1113
|
+
* Whether every row a read of `reach` answers is a row `action` reads alone: it is one of the reaching actions, and either
|
|
1114
|
+
* no `when` of its own narrows it or it alone reaches the rows. A column of its own is shown only then, so several held
|
|
1115
|
+
* actions never put one action's column on another action's rows.
|
|
1116
|
+
*/
|
|
1117
|
+
export function readsAsAction(model: TableModel, action: string, reach: ReadReach): boolean {
|
|
1118
|
+
if (!includes(reach.actions, action)) {
|
|
1119
|
+
return false
|
|
1120
|
+
}
|
|
1121
|
+
return !reach.narrowed || reach.actions.length === 1 || !hasOwn(model.when, action)
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
/**
|
|
1125
|
+
* The columns a read of `reach` answers, the union of what each reaching action reads alone: every one that declares no
|
|
1126
|
+
* read action of its own, and one that does where that action reads every answered row (`readsAsAction`).
|
|
1127
|
+
*/
|
|
1128
|
+
export function visibleThrough(model: TableModel, reach: ReadReach): readonly string[] {
|
|
1055
1129
|
const columns = namesOf(model.columns)
|
|
1056
1130
|
const visible: string[] = []
|
|
1057
1131
|
for (let index = 0; index < columns.length; index++) {
|
|
1058
1132
|
const name = columns[index] ?? ''
|
|
1059
1133
|
const declared = hasOwn(model.columnReads, name) ? model.columnReads[name] : undefined
|
|
1060
|
-
if (declared === undefined || declared
|
|
1134
|
+
if (declared === undefined || readsAsAction(model, declared, reach)) {
|
|
1061
1135
|
visible[visible.length] = name
|
|
1062
1136
|
}
|
|
1063
1137
|
}
|
|
@@ -1201,7 +1275,13 @@ function parseActions(
|
|
|
1201
1275
|
}
|
|
1202
1276
|
return distinctActions(table, value, `actions.${field}`, action)
|
|
1203
1277
|
}
|
|
1204
|
-
const
|
|
1278
|
+
const readAny = ownEntry(actions, 'read') === READ_ANY
|
|
1279
|
+
if (readAny && !hasOwn(scope, 'app')) {
|
|
1280
|
+
throw new PolicyError(
|
|
1281
|
+
`${table}: actions.read 'any' is for an application table; a dimension, child or reference table names its read actions`,
|
|
1282
|
+
)
|
|
1283
|
+
}
|
|
1284
|
+
const read = readAny ? freeze([]) : operation('read')
|
|
1205
1285
|
const records = hasOwn(scope, 'dimension') || hasOwn(scope, 'child')
|
|
1206
1286
|
const kind = hasOwn(scope, 'app') ? 'an application table' : 'a reference table'
|
|
1207
1287
|
const reach = (field: 'appRead' | 'appCount'): readonly string[] => {
|
|
@@ -1232,7 +1312,7 @@ function parseActions(
|
|
|
1232
1312
|
throw new PolicyError(`${table}: actions.appRead and actions.appCount both name ${name}`)
|
|
1233
1313
|
}
|
|
1234
1314
|
}
|
|
1235
|
-
return freeze({ read, insert: operation('insert'), update: operation('update'), delete: operation('delete'), appRead, appCount })
|
|
1315
|
+
return freeze({ read, readAny, insert: operation('insert'), update: operation('update'), delete: operation('delete'), appRead, appCount })
|
|
1236
1316
|
}
|
|
1237
1317
|
|
|
1238
1318
|
/** A plain array of distinct actions of the vocabulary, at least one. */
|
|
@@ -1614,6 +1694,23 @@ function tableActionNames(table: LinkedTable): NameSet {
|
|
|
1614
1694
|
return nameSet(names)
|
|
1615
1695
|
}
|
|
1616
1696
|
|
|
1697
|
+
/** The actions that write a table: its insert, update and delete actions and its columns' own write actions. */
|
|
1698
|
+
function writeActionNames(table: LinkedTable): NameSet {
|
|
1699
|
+
const names: string[] = []
|
|
1700
|
+
const lists = [table.actions.insert, table.actions.update, table.actions.delete]
|
|
1701
|
+
for (let list = 0; list < lists.length; list++) {
|
|
1702
|
+
const actions = lists[list] ?? []
|
|
1703
|
+
for (let index = 0; index < actions.length; index++) {
|
|
1704
|
+
names[names.length] = actions[index] ?? ''
|
|
1705
|
+
}
|
|
1706
|
+
}
|
|
1707
|
+
const writes = ownKeys(table.columnWrites)
|
|
1708
|
+
for (let index = 0; index < writes.length; index++) {
|
|
1709
|
+
names[names.length] = ownEntry(table.columnWrites, writes[index] ?? '') ?? ''
|
|
1710
|
+
}
|
|
1711
|
+
return nameSet(names)
|
|
1712
|
+
}
|
|
1713
|
+
|
|
1617
1714
|
/**
|
|
1618
1715
|
* `when`: action → the condition a row must meet for it. An insert action's condition is judged on the inserted row and
|
|
1619
1716
|
* the existing rows its paths reach (`insertProblem`); a reference table is read by every site and takes none.
|
|
@@ -1640,6 +1737,11 @@ function parseWhen(
|
|
|
1640
1737
|
if (!inSet(named, action)) {
|
|
1641
1738
|
throw new PolicyError(`${table.name}: when.${action} names an action the table does not name (${namesOf(named).join(', ')})`)
|
|
1642
1739
|
}
|
|
1740
|
+
if (table.actions.readAny && !inSet(writeActionNames(table), action)) {
|
|
1741
|
+
throw new PolicyError(
|
|
1742
|
+
`${table.name}: when.${action} narrows nothing: the table is read with 'any', and a when narrows only the writes of an action that inserts, updates or deletes`,
|
|
1743
|
+
)
|
|
1744
|
+
}
|
|
1643
1745
|
const what = `${table.name}: when.${action}`
|
|
1644
1746
|
const condition = parseCondition(ownEntry(declared, action), root, tables, what, hops)
|
|
1645
1747
|
// An insert reads the rows its foreign keys reach in its transaction, locked until the commit (`judgeInserted`).
|
|
@@ -2898,12 +3000,21 @@ type ScopeOf<P, T> = PolicyOf<P, T> extends { readonly scope: infer S } ? S : ne
|
|
|
2898
3000
|
|
|
2899
3001
|
export type Operation = 'read' | 'insert' | 'update' | 'delete'
|
|
2900
3002
|
|
|
2901
|
-
/**
|
|
3003
|
+
/**
|
|
3004
|
+
* The action, or the union of the actions, of operation `Op` on table `T`; `never` when the policy names none, and for
|
|
3005
|
+
* the read of a table read with `'any'` (`ReadsAny`).
|
|
3006
|
+
*/
|
|
2902
3007
|
export type ActionOf<P, T, Op extends Operation> = PolicyOf<P, T> extends { readonly actions: infer Acts }
|
|
2903
|
-
? Acts extends { readonly [K in Op]: infer A } ? A extends
|
|
3008
|
+
? Acts extends { readonly [K in Op]: infer A } ? A extends typeof READ_ANY ? never
|
|
3009
|
+
: A extends string ? A
|
|
3010
|
+
: A extends readonly (infer L extends string)[] ? L
|
|
3011
|
+
: never
|
|
2904
3012
|
: never
|
|
2905
3013
|
: never
|
|
2906
3014
|
|
|
3015
|
+
/** Whether table `T` is read with `'any'`: by every `app` endpoint, job and webhook, whatever its action. */
|
|
3016
|
+
export type ReadsAny<P, T> = PolicyOf<P, T> extends { readonly actions: { readonly read: typeof READ_ANY } } ? true : false
|
|
3017
|
+
|
|
2907
3018
|
export type DimensionOf<P, T> = ScopeOf<P, T> extends { readonly dimension: infer D extends string } ? D : never
|
|
2908
3019
|
export type ContainerOf<P, T> = ScopeOf<P, T> extends { readonly in: { readonly dimension: infer D extends string } } ? D : never
|
|
2909
3020
|
/** The dimension that lists and inserts of T are scoped by: its container, or for a top-level table its own. */
|
package/src/registry.ts
CHANGED
|
@@ -79,7 +79,7 @@ export interface FrameworkSide {
|
|
|
79
79
|
/** Connects the layer once and answers its binding; a call while one is connected throws. */
|
|
80
80
|
bind(runtime: DataLayerRuntime): ProvisionalBinding
|
|
81
81
|
readonly framework: FrameworkTables
|
|
82
|
-
/** The untyped handle over one table that `@fabrika/db/testing` hands the isolation
|
|
82
|
+
/** The untyped handle over one table that `@fabrika/db/testing` hands the policy isolation kit. */
|
|
83
83
|
probe(table: string): TableProbe
|
|
84
84
|
/** One table's context, for the framework's own store builders (`defineStore`, `@fabrika/db/testing`). */
|
|
85
85
|
tableContext(table: string, mode: 'dimension' | 'child'): TableContext
|