@fabrika/db 0.9.0-dev.12 → 0.9.0-dev.13
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 +254 -0
- package/src/builtins.ts +21 -0
- package/src/capability.ts +15 -0
- package/src/codegen.ts +11 -1
- package/src/condition-nodes.ts +119 -0
- package/src/conditions.ts +683 -0
- package/src/description.ts +3 -0
- package/src/framework.ts +1 -1
- package/src/guard.ts +302 -15
- package/src/nodes.ts +9 -6
- package/src/orm-types.ts +13 -2
- package/src/orm.ts +66 -28
- package/src/policy.ts +1197 -39
- package/src/registry.ts +11 -0
- package/src/sql.ts +22 -3
- package/src/store.ts +740 -55
- package/src/tables.ts +101 -2
- package/src/vocabulary-tables.ts +23 -1
- package/types/src/authority.d.ts +66 -0
- package/types/src/builtins.d.ts +2 -0
- package/types/src/capability.d.ts +15 -0
- package/types/src/condition-nodes.d.ts +9 -0
- package/types/src/conditions.d.ts +151 -0
- package/types/src/description.d.ts +3 -0
- package/types/src/framework.d.ts +1 -1
- package/types/src/guard.d.ts +16 -0
- package/types/src/nodes.d.ts +2 -0
- package/types/src/orm-types.d.ts +17 -2
- package/types/src/policy.d.ts +191 -0
- package/types/src/registry.d.ts +9 -0
- package/types/src/sql.d.ts +2 -2
- package/types/src/store.d.ts +45 -7
- package/types/src/tables.d.ts +6 -0
- package/types/src/vocabulary-tables.d.ts +37 -2
package/src/orm.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import { uuidv7 } from '@fabrika/platform'
|
|
7
7
|
import type { OperationNode } from 'kysely'
|
|
8
8
|
import { BinaryOperationNode, OperatorNode, ValueNode } from 'kysely'
|
|
9
|
+
import { refScope } from './authority'
|
|
9
10
|
import { freeze, hasOwn, inSet, isSafeInteger, nameSet, namesOf, ownDescriptor, ownKeys, ownNames, parseList, setOwn } from './builtins'
|
|
10
11
|
import { type CapabilityGrant, describeGrant, type RefGrant } from './capability'
|
|
11
12
|
import type { InvocationTransaction, Session } from './database'
|
|
@@ -23,7 +24,6 @@ import { CapabilityError, NotFoundError, type QueryRefusal, QueryRefusedError }
|
|
|
23
24
|
import { answerFiles, type FileBinding } from './file-columns'
|
|
24
25
|
import { databaseNow } from './job-leases'
|
|
25
26
|
import { rearmClaim } from './job-wakes'
|
|
26
|
-
import { linksPredicate, linkValues } from './links'
|
|
27
27
|
import {
|
|
28
28
|
aggregateNode,
|
|
29
29
|
allOf,
|
|
@@ -42,7 +42,7 @@ import {
|
|
|
42
42
|
withCursorColumns,
|
|
43
43
|
} from './nodes'
|
|
44
44
|
import type { DataKind, TargetForm, UnscopedMode } from './orm-types'
|
|
45
|
-
import { modelOf, namesAction, type TableModels, updatableBy, visibleFor } from './policy'
|
|
45
|
+
import { accessTablesOf, delegates, modelOf, namesAction, type TableModels, updatableBy, visibleFor } from './policy'
|
|
46
46
|
import { type SqlSite, type SqlTag, sqlTag } from './sql'
|
|
47
47
|
import {
|
|
48
48
|
actionShape,
|
|
@@ -50,6 +50,7 @@ import {
|
|
|
50
50
|
type AggregateSpec,
|
|
51
51
|
answer,
|
|
52
52
|
appKeyPredicate,
|
|
53
|
+
appKeyScope,
|
|
53
54
|
appListPredicate,
|
|
54
55
|
appRecordPredicate,
|
|
55
56
|
appRowPredicate,
|
|
@@ -71,7 +72,7 @@ import {
|
|
|
71
72
|
prepareApplicationInsert,
|
|
72
73
|
prepareChildInsert,
|
|
73
74
|
prepareDimensionInsert,
|
|
74
|
-
|
|
75
|
+
reachedBy,
|
|
75
76
|
reachPredicate,
|
|
76
77
|
readValues,
|
|
77
78
|
removeRow,
|
|
@@ -190,6 +191,7 @@ export function dataAccess(parts: DataLayerParts): DataAccess {
|
|
|
190
191
|
}
|
|
191
192
|
return scopeTerms(parts.contextOf(table), capability, use)
|
|
192
193
|
},
|
|
194
|
+
accessTablesOf(parts.models),
|
|
193
195
|
)
|
|
194
196
|
},
|
|
195
197
|
})
|
|
@@ -327,7 +329,7 @@ function unownedTarget(site: DataSite, dimension: string): Record<string, unknow
|
|
|
327
329
|
|
|
328
330
|
/** The predicate of the target object, over the whole chain its `Ref` carries. */
|
|
329
331
|
function targetPredicate(context: TableContext, grant: RefGrant): OperationNode {
|
|
330
|
-
return
|
|
332
|
+
return refScope(context.relations, context.model, selfLinks(context.model), grant, new Date())
|
|
331
333
|
}
|
|
332
334
|
|
|
333
335
|
function targetRow(
|
|
@@ -348,7 +350,14 @@ function targetRow(
|
|
|
348
350
|
const scope = targetPredicate(context, grant)
|
|
349
351
|
const node = lockedSelectNode(context.model.name, visibleFor(context.model, grant.action), scope, context.model.dates)
|
|
350
352
|
const session = await run.read()
|
|
351
|
-
const rows = await session.run(node, {
|
|
353
|
+
const rows = await session.run(node, {
|
|
354
|
+
root: 'SelectQueryNode',
|
|
355
|
+
table: context.model.name,
|
|
356
|
+
joins: {},
|
|
357
|
+
scope,
|
|
358
|
+
lock: 'update',
|
|
359
|
+
subqueries: context.subqueries,
|
|
360
|
+
})
|
|
352
361
|
const [row] = answerRows(dialectOf(context), context.model.name, rowColumnsOf(context.model), rows)
|
|
353
362
|
if (row === undefined) {
|
|
354
363
|
throw new NotFoundError(`${context.model.name} not found`)
|
|
@@ -393,8 +402,7 @@ function childRows(context: TableContext, site: DataSite, run: Runner, capabilit
|
|
|
393
402
|
if (typeof id !== 'string' || id.length === 0) {
|
|
394
403
|
throw new QueryRefusedError('row_address', `a ${context.model.name} row is addressed by its ${key}, a non-empty string`)
|
|
395
404
|
}
|
|
396
|
-
|
|
397
|
-
return selectOne(context, run, scope, visibleFor(context.model, grant.action), grant.action)
|
|
405
|
+
return selectOne(context, run, parentPredicate(context, grant, id), visibleFor(context.model, grant.action), grant.action)
|
|
398
406
|
}),
|
|
399
407
|
insert: writing(
|
|
400
408
|
site,
|
|
@@ -407,8 +415,7 @@ function childRows(context: TableContext, site: DataSite, run: Runner, capabilit
|
|
|
407
415
|
}
|
|
408
416
|
const grant = updatingRef(context, capability, parent)
|
|
409
417
|
const changes = readValues(values, updatableBy(context.model, grant.action), 'update', context.model)
|
|
410
|
-
|
|
411
|
-
return updateRow(context, run, { scope }, changes, versionArgument(context, options))
|
|
418
|
+
return updateRow(context, run, { scope: parentPredicate(context, grant, id) }, changes, versionArgument(context, options))
|
|
412
419
|
}),
|
|
413
420
|
remove: writing(site, 'remove', async (id: unknown, ...options: unknown[]) => {
|
|
414
421
|
await removeRow(context, run, { scope: childObjectPredicate(context, capability, id, 'delete') }, versionArgument(context, options))
|
|
@@ -421,7 +428,7 @@ function containedRows(context: TableContext, site: DataSite, run: Runner, capab
|
|
|
421
428
|
return freeze({
|
|
422
429
|
list: bound(site, async (options?: unknown) => {
|
|
423
430
|
const grant = namedRef(context, capability, dimension)
|
|
424
|
-
const scope =
|
|
431
|
+
const scope = refScope(context.relations, context.model, context.model.chain, grant, new Date())
|
|
425
432
|
return listRows(context, run, scope, visibleFor(context.model, grant.action), options, [keyOf(context.model)], grant.action)
|
|
426
433
|
}),
|
|
427
434
|
insert: writing(
|
|
@@ -453,9 +460,9 @@ function withinTables(parts: DataLayerParts, site: DataSite, run: Runner, capabi
|
|
|
453
460
|
for (let index = 0; index < tables.length; index++) {
|
|
454
461
|
const model = modelOf(parts.models, tables[index] ?? '')
|
|
455
462
|
const listable = model?.scope.mode === 'dimension' || model?.scope.mode === 'child'
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
setOwn(handle, model.name, listedRows(parts, context, site, run, capability, () => reachPredicate(context, capability)))
|
|
463
|
+
const context = model !== undefined && listable ? parts.contextOf(model.name) : undefined
|
|
464
|
+
if (context !== undefined && reachedBy(context, grant.dimension)) {
|
|
465
|
+
setOwn(handle, context.model.name, listedRows(parts, context, site, run, capability, () => reachPredicate(context, capability)))
|
|
459
466
|
}
|
|
460
467
|
}
|
|
461
468
|
return freeze(handle)
|
|
@@ -560,10 +567,11 @@ function scopedApplicationRows(context: TableContext, site: DataSite, run: Runne
|
|
|
560
567
|
list: bound(site, async (options?: unknown) => listRows(context, run, scope(), visible, options, key, heldAction(context, capability))),
|
|
561
568
|
count: bound(site, async (options?: unknown) => countRows(context, run, scope(), visible, options)),
|
|
562
569
|
aggregate: bound(site, async <Row>(spec: AggregateSpec<Row>) => aggregateRows(context, run, scope(), context.shape, spec)),
|
|
563
|
-
get: bound(
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
570
|
+
get: bound(
|
|
571
|
+
site,
|
|
572
|
+
async (id: unknown) =>
|
|
573
|
+
selectOne(context, run, scopedApplicationRead(context, capability, appKeyPredicate(model, id)), visible, heldAction(context, capability)),
|
|
574
|
+
),
|
|
567
575
|
})
|
|
568
576
|
}
|
|
569
577
|
|
|
@@ -603,11 +611,11 @@ function applicationRows(
|
|
|
603
611
|
: site.kind === 'query'
|
|
604
612
|
? refused(site, 'read_only', 'getForUpdate locks the row, so it runs only in a mutation: declare the endpoint with r.mutation')
|
|
605
613
|
: bound(site, async (id: unknown) => {
|
|
606
|
-
const scope = appKeyPredicate(model, id)
|
|
607
614
|
const action = namedApp(context, capability)
|
|
615
|
+
const scope = appKeyScope(model, action, id)
|
|
608
616
|
const node = lockedSelectNode(table, visibleFor(model, action), scope, model.dates)
|
|
609
617
|
const session = await run.read()
|
|
610
|
-
const rows = await session.run(node, { root: 'SelectQueryNode', table, joins: {}, scope, lock: 'update' })
|
|
618
|
+
const rows = await session.run(node, { root: 'SelectQueryNode', table, joins: {}, scope, lock: 'update', subqueries: context.subqueries })
|
|
611
619
|
const [row] = answerRows(dialectOf(context), table, rowColumnsOf(model), rows)
|
|
612
620
|
if (row === undefined) {
|
|
613
621
|
throw new NotFoundError(`${table} not found`)
|
|
@@ -628,7 +636,7 @@ function applicationRows(
|
|
|
628
636
|
),
|
|
629
637
|
get: bound(site, async (id: unknown) => {
|
|
630
638
|
const action = namedApp(context, capability)
|
|
631
|
-
return selectOne(context, run,
|
|
639
|
+
return selectOne(context, run, appKeyScope(model, action, id), visibleFor(model, action), action)
|
|
632
640
|
}),
|
|
633
641
|
getForUpdate,
|
|
634
642
|
claim: claiming(parts, context, site, capability),
|
|
@@ -640,7 +648,7 @@ function applicationRows(
|
|
|
640
648
|
update: writing(site, 'update', async (id: unknown, values: unknown, ...options: unknown[]) => {
|
|
641
649
|
const grant = updatingApp(context, capability)
|
|
642
650
|
const changes = readValues(values, updatableBy(model, grant.action), 'update', model)
|
|
643
|
-
return updateRow(context, run, { scope:
|
|
651
|
+
return updateRow(context, run, { scope: appKeyScope(model, grant.action, id) }, changes, versionArgument(context, options))
|
|
644
652
|
}),
|
|
645
653
|
remove: writing(site, 'remove', async (id: unknown, ...options: unknown[]) => {
|
|
646
654
|
await removeRow(context, run, { scope: appRowPredicate(context, capability, id, 'delete') }, versionArgument(context, options))
|
|
@@ -682,7 +690,7 @@ function publicApplicationRows(
|
|
|
682
690
|
get: bound(site, async (id: unknown) => {
|
|
683
691
|
appListPredicate(context, capability)
|
|
684
692
|
const action = heldAction(context, capability)
|
|
685
|
-
return selectOne(context, run,
|
|
693
|
+
return selectOne(context, run, appKeyScope(model, action, id), visibleFor(model, action), action)
|
|
686
694
|
}),
|
|
687
695
|
getForUpdate: refusal('lock'),
|
|
688
696
|
claim: refusal('claim'),
|
|
@@ -693,7 +701,7 @@ function publicApplicationRows(
|
|
|
693
701
|
const saved = await updateRow<Readonly<Record<string, unknown>>>(
|
|
694
702
|
context,
|
|
695
703
|
run,
|
|
696
|
-
{ scope:
|
|
704
|
+
{ scope: appKeyScope(model, grant.action, id) },
|
|
697
705
|
changes,
|
|
698
706
|
versionArgument(context, options),
|
|
699
707
|
)
|
|
@@ -808,7 +816,14 @@ async function claimRows(
|
|
|
808
816
|
const now = await databaseNow(session)
|
|
809
817
|
const waiting = { due: queue.due, attempts: queue.attempts, now, maxAttempts: queue.maxAttempts }
|
|
810
818
|
const node = claimNode(rowState(table, conditions, order, limit), selected, waiting, scope, model.dates, dialect)
|
|
811
|
-
const rows = await session.run<Readonly<Record<string, unknown>>>(node, {
|
|
819
|
+
const rows = await session.run<Readonly<Record<string, unknown>>>(node, {
|
|
820
|
+
root: 'SelectQueryNode',
|
|
821
|
+
table,
|
|
822
|
+
joins: {},
|
|
823
|
+
scope,
|
|
824
|
+
lock: 'claim',
|
|
825
|
+
subqueries: context.subqueries,
|
|
826
|
+
})
|
|
812
827
|
const byAttempts = new Map<number, string[]>()
|
|
813
828
|
for (const row of rows) {
|
|
814
829
|
const attempts = row[queue.attempts]
|
|
@@ -824,7 +839,7 @@ async function claimRows(
|
|
|
824
839
|
const changes = new Map<string, Primitive>([[queue.attempts, attempts + 1], [queue.due, due]])
|
|
825
840
|
const updated = await session.run<Readonly<Record<string, unknown>>>(
|
|
826
841
|
updateNode(table, boundValues(context, changes), version, scopedWhere(inList(table, key, ids), scope), answer(context)),
|
|
827
|
-
{ root: 'UpdateQueryNode', table, joins: {}, scope },
|
|
842
|
+
{ root: 'UpdateQueryNode', table, joins: {}, scope, subqueries: context.subqueries },
|
|
828
843
|
)
|
|
829
844
|
for (const row of updated) {
|
|
830
845
|
const id = row['id']
|
|
@@ -846,7 +861,13 @@ async function claimRows(
|
|
|
846
861
|
dialect,
|
|
847
862
|
table,
|
|
848
863
|
rowColumnsOf(model),
|
|
849
|
-
await session.run<Readonly<Record<string, unknown>>>(later, {
|
|
864
|
+
await session.run<Readonly<Record<string, unknown>>>(later, {
|
|
865
|
+
root: 'SelectQueryNode',
|
|
866
|
+
table,
|
|
867
|
+
joins: {},
|
|
868
|
+
scope,
|
|
869
|
+
subqueries: context.subqueries,
|
|
870
|
+
}),
|
|
850
871
|
)
|
|
851
872
|
next = row === undefined ? null : dueOf(row, table, queue.due)
|
|
852
873
|
}
|
|
@@ -1010,6 +1031,14 @@ function unscopedRows(context: TableContext, site: DataSite, run: Runner, mode:
|
|
|
1010
1031
|
if (model.view) {
|
|
1011
1032
|
return refused(site, 'read_only', `${table} is a view, which no handler writes`)
|
|
1012
1033
|
}
|
|
1034
|
+
// A row that hands out access is placed by its writer; an unscoped write would delegate without the writer's bound.
|
|
1035
|
+
if (delegates(model)) {
|
|
1036
|
+
return refused(
|
|
1037
|
+
site,
|
|
1038
|
+
'undeclared_reach',
|
|
1039
|
+
`${table} hands out access, so it is written through a capability only, never unscoped`,
|
|
1040
|
+
)
|
|
1041
|
+
}
|
|
1013
1042
|
if (mode === 'read') {
|
|
1014
1043
|
const verb = operation === 'remove' ? 'remove' : operation
|
|
1015
1044
|
return refused(site, 'undeclared_reach', `${table} is declared unscoped: 'read', so ${verb} needs unscoped: { ${table}: 'write' }`)
|
|
@@ -1028,7 +1057,7 @@ function unscopedRows(context: TableContext, site: DataSite, run: Runner, mode:
|
|
|
1028
1057
|
const dialect = dialectOf(context)
|
|
1029
1058
|
const node = selectNode(rowState(table, [], [], 1), visible, scope, byKey(id), model.dates, dialect)
|
|
1030
1059
|
const session = await run.read()
|
|
1031
|
-
const rows = await session.run(node, { root: 'SelectQueryNode', table, joins: {}, scope })
|
|
1060
|
+
const rows = await session.run(node, { root: 'SelectQueryNode', table, joins: {}, scope, subqueries: context.subqueries })
|
|
1032
1061
|
const [row] = answerRows(dialect, table, rowColumnsOf(model), rows)
|
|
1033
1062
|
if (row === undefined) {
|
|
1034
1063
|
throw new NotFoundError(`${table} not found`)
|
|
@@ -1291,6 +1320,8 @@ async function listRows(
|
|
|
1291
1320
|
table,
|
|
1292
1321
|
joins: {},
|
|
1293
1322
|
scope,
|
|
1323
|
+
subqueries: context.subqueries,
|
|
1324
|
+
through: context.through,
|
|
1294
1325
|
})
|
|
1295
1326
|
const page = answerRows(dialect, table, rowColumnsOf(context.model), rows.slice(0, limit))
|
|
1296
1327
|
const last = page[page.length - 1]
|
|
@@ -1315,7 +1346,14 @@ async function countRows(
|
|
|
1315
1346
|
return 0
|
|
1316
1347
|
}
|
|
1317
1348
|
const node = aggregateNode(table, { groupBy: [], count: true, sum: [], min: [], max: [] }, conditions, scope)
|
|
1318
|
-
const [row] = await (await run.read()).run<Readonly<Record<string, unknown>>>(node, {
|
|
1349
|
+
const [row] = await (await run.read()).run<Readonly<Record<string, unknown>>>(node, {
|
|
1350
|
+
root: 'SelectQueryNode',
|
|
1351
|
+
table,
|
|
1352
|
+
joins: {},
|
|
1353
|
+
scope,
|
|
1354
|
+
subqueries: context.subqueries,
|
|
1355
|
+
through: context.through,
|
|
1356
|
+
})
|
|
1319
1357
|
const count = row === undefined ? '0' : row['count']
|
|
1320
1358
|
return typeof count === 'string' || typeof count === 'number' ? Number(count) : 0
|
|
1321
1359
|
}
|