@fabrika/db 0.9.0-dev.1 → 0.9.0-dev.10
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 +44 -12
- package/src/admissions.ts +78 -12
- package/src/application-rows.ts +108 -0
- package/src/capability.ts +5 -0
- package/src/codegen-sqlite.ts +191 -0
- package/src/codegen.ts +170 -28
- package/src/database.ts +227 -61
- package/src/description.ts +34 -8
- package/src/errors.ts +6 -4
- package/src/file-columns.ts +36 -9
- package/src/framework-tables.ts +28 -8
- package/src/framework.ts +14 -5
- package/src/guard.ts +8 -0
- package/src/index.ts +10 -1
- package/src/job-leases.ts +210 -54
- package/src/job-wakes.ts +450 -0
- package/src/json.ts +124 -0
- package/src/links.ts +8 -1
- package/src/migrations-sqlite.ts +163 -0
- package/src/migrations.ts +25 -0
- package/src/nodes.ts +64 -9
- package/src/orm-types.ts +5 -5
- package/src/orm.ts +94 -37
- package/src/policy.ts +17 -2
- package/src/postgres.ts +26 -0
- package/src/registry.ts +11 -2
- package/src/sql.ts +230 -477
- package/src/sqlite-bun.ts +149 -0
- package/src/sqlite-migrate.ts +242 -0
- package/src/sqlite.ts +224 -0
- package/src/store.ts +385 -103
- package/src/stored-files.ts +188 -52
- package/src/tables.ts +16 -0
- package/src/temporal.ts +122 -9
- package/src/testing-shared.ts +878 -0
- package/src/testing-sqlite.ts +570 -0
- package/src/testing.ts +255 -482
- package/types/src/admissions.d.ts +21 -0
- package/types/src/application-rows.d.ts +58 -0
- package/types/src/builtins.d.ts +42 -0
- package/types/src/capability.d.ts +119 -0
- package/types/src/codegen-sqlite.d.ts +13 -0
- package/types/src/codegen.d.ts +113 -0
- package/types/src/database.d.ts +185 -0
- package/types/src/description.d.ts +124 -0
- package/types/src/errors.d.ts +98 -0
- package/types/src/file-columns.d.ts +39 -0
- package/types/src/file-references.d.ts +27 -0
- package/types/src/framework-tables.d.ts +59 -0
- package/types/src/framework.d.ts +38 -0
- package/types/src/guard.d.ts +23 -0
- package/types/src/identifier.d.ts +4 -0
- package/types/src/index.d.ts +11 -0
- package/types/src/job-leases.d.ts +112 -0
- package/types/src/job-wakes.d.ts +101 -0
- package/types/src/json.d.ts +18 -0
- package/types/src/links.d.ts +13 -0
- package/types/src/migrations-sqlite.d.ts +4 -0
- package/types/src/migrations.d.ts +26 -0
- package/types/src/nodes.d.ts +73 -0
- package/types/src/orm-types.d.ts +638 -0
- package/types/src/orm.d.ts +82 -0
- package/types/src/policy.d.ts +358 -0
- package/types/src/postgres.d.ts +12 -0
- package/types/src/production.d.ts +5 -0
- package/types/src/registry.d.ts +43 -0
- package/types/src/sql.d.ts +67 -0
- package/types/src/sqlite-bun.d.ts +21 -0
- package/types/src/sqlite-migrate.d.ts +28 -0
- package/types/src/sqlite.d.ts +24 -0
- package/types/src/store.d.ts +351 -0
- package/types/src/stored-files.d.ts +102 -0
- package/types/src/tables.d.ts +21 -0
- package/types/src/temporal.d.ts +36 -0
- package/types/src/testing-shared.d.ts +250 -0
- package/types/src/testing-sqlite.d.ts +22 -0
- package/types/src/testing.d.ts +35 -0
- package/types/src/vocabulary-tables.d.ts +225 -0
package/src/store.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { uuidv7 } from '@fabrika/platform'
|
|
1
2
|
import { BinaryOperationNode, type OperationNode, OperatorNode, type ReturningNode, ValueNode } from 'kysely'
|
|
2
3
|
import { admissions } from './admissions'
|
|
3
4
|
import {
|
|
@@ -31,8 +32,13 @@ import {
|
|
|
31
32
|
type Within,
|
|
32
33
|
type WithinGrant,
|
|
33
34
|
} from './capability'
|
|
34
|
-
import { type Database, InvocationTransaction, type Session, sessionOf } from './database'
|
|
35
|
+
import { type Database, InvocationTransaction, type Session, sessionOf, type SqlDialect } from './database'
|
|
35
36
|
import {
|
|
37
|
+
type ComparableColumn,
|
|
38
|
+
comparableColumnOf,
|
|
39
|
+
type Compare,
|
|
40
|
+
type Condition,
|
|
41
|
+
type Contains,
|
|
36
42
|
finishSelect,
|
|
37
43
|
isPrimitive,
|
|
38
44
|
type JoinShape,
|
|
@@ -49,7 +55,9 @@ import { fileReference } from './file-references'
|
|
|
49
55
|
import { webhookTables } from './framework-tables'
|
|
50
56
|
import type { ForeignKeyPair, GuardContext, RootKind } from './guard'
|
|
51
57
|
import { fencedTransaction, jobLeases } from './job-leases'
|
|
52
|
-
import {
|
|
58
|
+
import { jobWakes, leaseProjection } from './job-wakes'
|
|
59
|
+
import { jsonInput } from './json'
|
|
60
|
+
import { linksPredicate, type LinkValue, linkValues } from './links'
|
|
53
61
|
import {
|
|
54
62
|
acknowledgement,
|
|
55
63
|
acknowledgementNode,
|
|
@@ -93,23 +101,27 @@ import {
|
|
|
93
101
|
type TableModels,
|
|
94
102
|
tableModels,
|
|
95
103
|
type Tables,
|
|
96
|
-
updatableBy,
|
|
97
104
|
updateActions,
|
|
98
105
|
type UpdateValues,
|
|
99
106
|
type VersionArguments,
|
|
100
107
|
visibleFor,
|
|
101
108
|
} from './policy'
|
|
102
|
-
import { frameworkSideOf, type
|
|
109
|
+
import { frameworkSideOf, type ProvisionalBinding, register } from './registry'
|
|
103
110
|
import type { ScopeTerms, ScopeTest, ScopeUse } from './sql'
|
|
104
111
|
import { storedFiles, storedFilesMetadata } from './stored-files'
|
|
105
112
|
import { type TablePolicies, tablePolicies } from './tables'
|
|
106
|
-
import {
|
|
113
|
+
import { answerRows, dayInput, type RowColumns, storedTimestamp, timestampInput } from './temporal'
|
|
107
114
|
|
|
108
115
|
export interface DataLayerRuntime {
|
|
109
116
|
readonly database: Database
|
|
110
117
|
readonly capabilities: CapabilityAuthority
|
|
111
118
|
/** Whether the request running now may only read (an export's); its every statement then runs `READ ONLY`. */
|
|
112
119
|
readonly readOnly?: () => boolean
|
|
120
|
+
/**
|
|
121
|
+
* Runs inside the transaction of every change of a job's wakes or lease, after the change: the Cloudflare driver arms
|
|
122
|
+
* the alarm from `wakes.next(transaction)` there, so the alarm commits with the change. A throw rolls the change back.
|
|
123
|
+
*/
|
|
124
|
+
readonly projectWakes?: (transaction: Session) => Promise<void>
|
|
113
125
|
}
|
|
114
126
|
|
|
115
127
|
/** Empty until the framework connects the layer at boot; every table handle of one `scoped` call shares it. */
|
|
@@ -124,9 +136,86 @@ export interface TableContext {
|
|
|
124
136
|
readonly destination: TableModel | undefined
|
|
125
137
|
readonly shape: TableShape
|
|
126
138
|
readonly joins: Readonly<Record<string, readonly ForeignKeyPair[]>>
|
|
139
|
+
/** The model of each child a select may join, by its table name. */
|
|
140
|
+
readonly children: Readonly<Record<string, TableModel>>
|
|
127
141
|
readonly connection: Connection
|
|
128
142
|
}
|
|
129
143
|
|
|
144
|
+
/** The dialect of the database the layer is bound to: what a read answers and a write binds depends on it. */
|
|
145
|
+
export function dialectOf(context: TableContext): SqlDialect {
|
|
146
|
+
return runtimeOf(context.connection).database.dialect
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** The columns of `model`'s own rows that a read answers in a form of its own (`answerRows`). */
|
|
150
|
+
export function rowColumnsOf(model: TableModel): RowColumns {
|
|
151
|
+
return { days: model.dates, timestamps: model.timestamps, booleans: model.booleans, json: model.json }
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** A timestamp's bound value on SQLite: its stored UTC text. Postgres takes a `Date` or ISO text as it is. */
|
|
155
|
+
function boundTimestamp(model: TableModel, column: string, value: Primitive): Primitive {
|
|
156
|
+
if (typeof value !== 'string' && !(value instanceof Date)) {
|
|
157
|
+
throw new QueryRefusedError('invalid_value', `${model.name}.${column} takes a Date or ISO 8601 text with Z or an offset`)
|
|
158
|
+
}
|
|
159
|
+
return storedTimestamp(value, `${model.name}.${column}`)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** The values a write binds: on SQLite each timestamp as its stored UTC text, so text order is time order. */
|
|
163
|
+
export function boundValues(context: TableContext, values: ReadonlyMap<string, Primitive | null>): ReadonlyMap<string, Primitive | null> {
|
|
164
|
+
const model = context.model
|
|
165
|
+
if (dialectOf(context) === 'postgres' || ownKeys(model.timestamps).length === 0) {
|
|
166
|
+
return values
|
|
167
|
+
}
|
|
168
|
+
const bound = new Map<string, Primitive | null>()
|
|
169
|
+
for (const [name, value] of values) {
|
|
170
|
+
bound.set(name, value === null || !inSet(model.timestamps, name) ? value : boundTimestamp(model, name, value))
|
|
171
|
+
}
|
|
172
|
+
return bound
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** A term's values as they are bound: on SQLite, one over a timestamp of `model` compares stored UTC text. */
|
|
176
|
+
function boundTerm(model: TableModel, term: Compare | Contains): Compare | Contains {
|
|
177
|
+
const value = term.kind === 'compare' ? term.value : null
|
|
178
|
+
if (term.kind !== 'compare' || value === null || !inSet(model.timestamps, term.column)) {
|
|
179
|
+
return term
|
|
180
|
+
}
|
|
181
|
+
const bind = (item: Primitive): Primitive => boundTimestamp(model, term.column, item)
|
|
182
|
+
return { ...term, value: isList(value) ? value.map(bind) : bind(value) }
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function isList(value: Primitive | readonly Primitive[]): value is readonly Primitive[] {
|
|
186
|
+
return isArray(value)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** A list's terms over `model`'s columns, as `boundTerm` binds each. */
|
|
190
|
+
export function boundTerms(context: TableContext, model: TableModel, terms: readonly (Compare | Contains)[]): readonly (Compare | Contains)[] {
|
|
191
|
+
if (dialectOf(context) === 'postgres' || ownKeys(model.timestamps).length === 0) {
|
|
192
|
+
return terms
|
|
193
|
+
}
|
|
194
|
+
return terms.map((term) => boundTerm(model, term))
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** A description's conditions over `model`'s columns, an OR group's terms included, as `boundTerm` binds each. */
|
|
198
|
+
function boundConditions(context: TableContext, model: TableModel, conditions: readonly Condition[]): readonly Condition[] {
|
|
199
|
+
if (dialectOf(context) === 'postgres' || ownKeys(model.timestamps).length === 0) {
|
|
200
|
+
return conditions
|
|
201
|
+
}
|
|
202
|
+
return conditions.map((condition) =>
|
|
203
|
+
condition.kind === 'any' ? { ...condition, conditions: condition.conditions.map((term) => boundTerm(model, term)) } : boundTerm(model, condition)
|
|
204
|
+
)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** A select's conditions, its joined children's included, as they are bound. */
|
|
208
|
+
function boundState(context: TableContext, state: SelectState): SelectState {
|
|
209
|
+
if (dialectOf(context) === 'postgres') {
|
|
210
|
+
return state
|
|
211
|
+
}
|
|
212
|
+
const joins = state.joins.map((join) => {
|
|
213
|
+
const child = ownEntry(context.children, join.join.shape.table)
|
|
214
|
+
return child === undefined ? join : { ...join, select: { ...join.select, conditions: boundConditions(context, child, join.select.conditions) } }
|
|
215
|
+
})
|
|
216
|
+
return { ...state, conditions: boundConditions(context, context.model, state.conditions), joins }
|
|
217
|
+
}
|
|
218
|
+
|
|
130
219
|
export function runtimeOf(connection: Connection): DataLayerRuntime {
|
|
131
220
|
const runtime = connection.runtime
|
|
132
221
|
if (runtime === undefined) {
|
|
@@ -227,6 +316,76 @@ function linksAbove(model: TableModel, dimension: string): readonly Container[]
|
|
|
227
316
|
return []
|
|
228
317
|
}
|
|
229
318
|
|
|
319
|
+
/**
|
|
320
|
+
* The links of a row that lie inside the `Within`'s own dimension: the object itself for a dimension table, and the
|
|
321
|
+
* chain links nearer than `dimension`. A grant on one of them admits the row as a single-object read admits it.
|
|
322
|
+
*/
|
|
323
|
+
function linksBelow(model: TableModel, dimension: string): readonly Container[] {
|
|
324
|
+
const links = model.scope.mode === 'dimension' ? selfLinks(model) : model.chain
|
|
325
|
+
const below: Container[] = []
|
|
326
|
+
for (let index = 0; index < links.length; index++) {
|
|
327
|
+
const link = links[index]
|
|
328
|
+
if (link === undefined || link.dimension === dimension) {
|
|
329
|
+
return below
|
|
330
|
+
}
|
|
331
|
+
below[below.length] = link
|
|
332
|
+
}
|
|
333
|
+
return []
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* One `<column> in (values)` per contained grant on a link of the row inside the `Within`'s dimension. A grant on a
|
|
338
|
+
* dimension the table carries no link of admits none of its rows; one on the `Within`'s dimension or above it is refused.
|
|
339
|
+
*/
|
|
340
|
+
function containedTests(model: TableModel, grant: WithinGrant): { readonly column: string; readonly values: readonly string[] }[] {
|
|
341
|
+
const below = linksBelow(model, grant.dimension)
|
|
342
|
+
const above = linksAbove(model, grant.dimension)
|
|
343
|
+
const tests: { readonly column: string; readonly values: readonly string[] }[] = []
|
|
344
|
+
for (let index = 0; index < grant.contained.length; index++) {
|
|
345
|
+
const contained = grant.contained[index]
|
|
346
|
+
if (contained === undefined) {
|
|
347
|
+
continue
|
|
348
|
+
}
|
|
349
|
+
if (contained.dimension === grant.dimension || linkOf(above, contained.dimension) !== undefined) {
|
|
350
|
+
throw new CapabilityError(
|
|
351
|
+
'unenforceable',
|
|
352
|
+
`the Within of ${grant.dimension} carries a grant on ${contained.dimension}, which does not lie inside ${grant.dimension} in ${model.name}`,
|
|
353
|
+
)
|
|
354
|
+
}
|
|
355
|
+
const link = linkOf(below, contained.dimension)
|
|
356
|
+
if (link !== undefined && contained.values.length > 0) {
|
|
357
|
+
tests[tests.length] = { column: link.column, values: contained.values }
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return tests
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** The link of `dimension` among `links`, by an index loop: no array method an application could replace. */
|
|
364
|
+
function linkOf(links: readonly Container[], dimension: string): Container | undefined {
|
|
365
|
+
for (let index = 0; index < links.length; index++) {
|
|
366
|
+
const link = links[index]
|
|
367
|
+
if (link !== undefined && link.dimension === dimension) {
|
|
368
|
+
return link
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
return undefined
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** Every scope value a `Within` carries: its own, its containers' and its contained dimensions'. */
|
|
375
|
+
function withinCount(grant: WithinGrant): number {
|
|
376
|
+
if (grant.values === 'all') {
|
|
377
|
+
return 0
|
|
378
|
+
}
|
|
379
|
+
let count = grant.values.length
|
|
380
|
+
for (let index = 0; index < grant.containers.length; index++) {
|
|
381
|
+
count += grant.containers[index]?.values.length ?? 0
|
|
382
|
+
}
|
|
383
|
+
for (let index = 0; index < grant.contained.length; index++) {
|
|
384
|
+
count += grant.contained[index]?.values.length ?? 0
|
|
385
|
+
}
|
|
386
|
+
return count
|
|
387
|
+
}
|
|
388
|
+
|
|
230
389
|
/**
|
|
231
390
|
* `column in (values) or <container column> in (container values) …`, one disjunct per container dimension, so
|
|
232
391
|
* the statement itself judges each row by the chain it carries now. Undefined for an empty `Within`: the caller
|
|
@@ -238,7 +397,7 @@ function withinPredicate(model: TableModel, column: string, grant: WithinGrant):
|
|
|
238
397
|
}
|
|
239
398
|
const above = linksAbove(model, grant.dimension)
|
|
240
399
|
const predicates: OperationNode[] = []
|
|
241
|
-
|
|
400
|
+
const count = withinCount(grant)
|
|
242
401
|
if (grant.values.length > 0) {
|
|
243
402
|
predicates[predicates.length] = inList(model.name, column, grant.values)
|
|
244
403
|
}
|
|
@@ -247,23 +406,24 @@ function withinPredicate(model: TableModel, column: string, grant: WithinGrant):
|
|
|
247
406
|
if (container === undefined) {
|
|
248
407
|
continue
|
|
249
408
|
}
|
|
250
|
-
|
|
251
|
-
for (let position = 0; position < above.length; position++) {
|
|
252
|
-
if (above[position]?.dimension === container.dimension) {
|
|
253
|
-
link = above[position]
|
|
254
|
-
}
|
|
255
|
-
}
|
|
409
|
+
const link = linkOf(above, container.dimension)
|
|
256
410
|
if (link === undefined) {
|
|
257
411
|
throw new CapabilityError(
|
|
258
412
|
'unenforceable',
|
|
259
413
|
`the Within of ${grant.dimension} reaches through ${container.dimension}, which ${model.name} carries no column for above ${grant.dimension}; the layer cannot enforce it`,
|
|
260
414
|
)
|
|
261
415
|
}
|
|
262
|
-
count += container.values.length
|
|
263
416
|
if (container.values.length > 0) {
|
|
264
417
|
predicates[predicates.length] = inList(model.name, link.column, container.values)
|
|
265
418
|
}
|
|
266
419
|
}
|
|
420
|
+
const contained = containedTests(model, grant)
|
|
421
|
+
for (let index = 0; index < contained.length; index++) {
|
|
422
|
+
const test = contained[index]
|
|
423
|
+
if (test !== undefined) {
|
|
424
|
+
predicates[predicates.length] = inList(model.name, test.column, test.values)
|
|
425
|
+
}
|
|
426
|
+
}
|
|
267
427
|
if (count > MAX_WITHIN_VALUES) {
|
|
268
428
|
throw new CapabilityError('within_size', `a Within carries at most ${MAX_WITHIN_VALUES} scope values; this one carries ${count}`)
|
|
269
429
|
}
|
|
@@ -273,7 +433,14 @@ function withinPredicate(model: TableModel, column: string, grant: WithinGrant):
|
|
|
273
433
|
/** The object a Ref of the model's own dimension names, with every link of its chain. */
|
|
274
434
|
export function selfLinks(model: TableModel): readonly Container[] {
|
|
275
435
|
const scope = dimensionScope(model)
|
|
276
|
-
|
|
436
|
+
const links: Container[] = [{ dimension: scope.dimension, column: scope.key }]
|
|
437
|
+
for (let index = 0; index < model.chain.length; index++) {
|
|
438
|
+
const link = model.chain[index]
|
|
439
|
+
if (link !== undefined) {
|
|
440
|
+
links[links.length] = link
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
return links
|
|
277
444
|
}
|
|
278
445
|
|
|
279
446
|
/** This object, over the whole chain the Ref carries. */
|
|
@@ -348,12 +515,12 @@ export function reachPredicate(context: TableContext, capability: unknown): Oper
|
|
|
348
515
|
}
|
|
349
516
|
|
|
350
517
|
/**
|
|
351
|
-
* A fragment
|
|
352
|
-
* written by an `app` capability only, a dimension or child table never by one. An UPDATE may
|
|
353
|
-
* own write action
|
|
354
|
-
* `
|
|
518
|
+
* A fragment its author marks as an UPDATE or DELETE target: the capability's action is that operation's, and an
|
|
519
|
+
* application table is written by an `app` capability only, a dimension or child table never by one. An UPDATE may
|
|
520
|
+
* instead hold a column's own write action; which columns the statement sets is the author's and the approver's to
|
|
521
|
+
* keep to that action's (`updatableBy`), as the statement is not read for it.
|
|
355
522
|
*/
|
|
356
|
-
function checkWriteTarget(context: TableContext, grant: CapabilityGrant, use: 'update' | 'delete'
|
|
523
|
+
function checkWriteTarget(context: TableContext, grant: CapabilityGrant, use: 'update' | 'delete'): void {
|
|
357
524
|
const model = context.model
|
|
358
525
|
const statement = `${use === 'update' ? 'UPDATE' : 'DELETE'} of ${model.name}`
|
|
359
526
|
if (grant.kind === 'app' && model.scope.mode !== 'app') {
|
|
@@ -380,42 +547,26 @@ function checkWriteTarget(context: TableContext, grant: CapabilityGrant, use: 'u
|
|
|
380
547
|
if (columnWrites.length > 0) needs[needs.length] = `a column's own write action (${columnWrites.join(', ')})`
|
|
381
548
|
throw new CapabilityError('wrong_action', `${statement} needs ${needs.join(' or ')}; the capability holds ${grant.action}`)
|
|
382
549
|
}
|
|
383
|
-
if (assigned === undefined) {
|
|
384
|
-
return
|
|
385
|
-
}
|
|
386
|
-
const written = updatableBy(model, grant.action)
|
|
387
|
-
const outside: string[] = assigned.length === 0 ? ['no column it can name'] : []
|
|
388
|
-
for (let index = 0; index < assigned.length; index++) {
|
|
389
|
-
const column = assigned[index]
|
|
390
|
-
if (column !== undefined && !inSet(written, column)) outside[outside.length] = column
|
|
391
|
-
}
|
|
392
|
-
if (outside.length > 0) {
|
|
393
|
-
throw new CapabilityError(
|
|
394
|
-
'wrong_action',
|
|
395
|
-
`${statement} assigns ${
|
|
396
|
-
outside.join(', ')
|
|
397
|
-
}, which ${grant.action} does not write: a column's own write action scopes an UPDATE of its columns only`,
|
|
398
|
-
)
|
|
399
|
-
}
|
|
400
550
|
}
|
|
401
551
|
|
|
402
552
|
/**
|
|
403
553
|
* `ctx.sql.scope`'s predicate: what the handles of `capability` match in the table for `use`, as column tests. A
|
|
404
554
|
* fragment that reads (`'read'`) needs an action the handles read the table with: any action it names for a `Ref`,
|
|
405
555
|
* its read action for a `Within`, an action `ctx.db.app` lists it for with an `app` capability, and its read action
|
|
406
|
-
* for a scoped site's read of an application table. A fragment
|
|
407
|
-
* operation's own action, and an `app` capability writes application tables only. A `Ref` matches its object's
|
|
408
|
-
* and every link of the chain it carries; a `Within` matches like `ctx.db.within` (its values, each container
|
|
409
|
-
* its own column, or every row for `all`); an `app` capability, and a scoped read of an application table,
|
|
556
|
+
* for a scoped site's read of an application table. A fragment its author marks `use: 'update'` or `'delete'` needs
|
|
557
|
+
* that operation's own action, and an `app` capability writes application tables only. A `Ref` matches its object's
|
|
558
|
+
* column and every link of the chain it carries; a `Within` matches like `ctx.db.within` (its values, each container
|
|
559
|
+
* grant on its own column, or every row for `all`); an `app` capability, and a scoped read of an application table,
|
|
560
|
+
* every row.
|
|
410
561
|
*/
|
|
411
|
-
export function scopeTerms(context: TableContext, capability: unknown, use: ScopeUse
|
|
562
|
+
export function scopeTerms(context: TableContext, capability: unknown, use: ScopeUse): ScopeTerms {
|
|
412
563
|
const grant = runtimeOf(context.connection).capabilities.resolve(capability)
|
|
413
564
|
const model = context.model
|
|
414
565
|
if (grant.kind === 'row') {
|
|
415
566
|
throw new CapabilityError('wrong_capability', `ctx.sql.scope takes a Ref, a Within or an app capability, got ${describeGrant(grant)}`)
|
|
416
567
|
}
|
|
417
568
|
if (use !== 'read') {
|
|
418
|
-
checkWriteTarget(context, grant, use
|
|
569
|
+
checkWriteTarget(context, grant, use)
|
|
419
570
|
}
|
|
420
571
|
if (grant.kind === 'app') {
|
|
421
572
|
if (use === 'read') {
|
|
@@ -443,7 +594,22 @@ export function scopeTerms(context: TableContext, capability: unknown, use: Scop
|
|
|
443
594
|
if (use === 'read' && !namesAction(model, grant.action)) {
|
|
444
595
|
throw new CapabilityError('wrong_action', `${model.name} does not name the action ${grant.action}: a Ref reads it with an action it names`)
|
|
445
596
|
}
|
|
446
|
-
|
|
597
|
+
const reached: Container[] = []
|
|
598
|
+
for (let index = start; index < links.length; index++) {
|
|
599
|
+
const link = links[index]
|
|
600
|
+
if (link !== undefined) {
|
|
601
|
+
reached[reached.length] = link
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
const values = linkValues(model.name, reached, grant)
|
|
605
|
+
const tests: ScopeTest[] = []
|
|
606
|
+
for (let index = 0; index < values.length; index++) {
|
|
607
|
+
const entry = values[index]
|
|
608
|
+
if (entry !== undefined) {
|
|
609
|
+
tests[tests.length] = { column: entry.link.column, value: entry.value }
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
return [tests]
|
|
447
613
|
}
|
|
448
614
|
const column = reachColumn(model, grant.dimension)
|
|
449
615
|
if (column === undefined) {
|
|
@@ -463,21 +629,31 @@ export function scopeTerms(context: TableContext, capability: unknown, use: Scop
|
|
|
463
629
|
}
|
|
464
630
|
const above = linksAbove(model, grant.dimension)
|
|
465
631
|
const groups: ScopeTest[][] = []
|
|
466
|
-
|
|
632
|
+
const count = withinCount(grant)
|
|
467
633
|
if (grant.values.length > 0) {
|
|
468
|
-
groups.
|
|
634
|
+
groups[groups.length] = [{ column, value: grant.values }]
|
|
469
635
|
}
|
|
470
|
-
for (
|
|
471
|
-
const
|
|
636
|
+
for (let index = 0; index < grant.containers.length; index++) {
|
|
637
|
+
const container = grant.containers[index]
|
|
638
|
+
if (container === undefined) {
|
|
639
|
+
continue
|
|
640
|
+
}
|
|
641
|
+
const link = linkOf(above, container.dimension)
|
|
472
642
|
if (link === undefined) {
|
|
473
643
|
throw new CapabilityError(
|
|
474
644
|
'unenforceable',
|
|
475
645
|
`the Within of ${grant.dimension} reaches through ${container.dimension}, which ${model.name} carries no column for above ${grant.dimension}; the layer cannot enforce it`,
|
|
476
646
|
)
|
|
477
647
|
}
|
|
478
|
-
count += container.values.length
|
|
479
648
|
if (container.values.length > 0) {
|
|
480
|
-
groups.
|
|
649
|
+
groups[groups.length] = [{ column: link.column, value: container.values }]
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
const contained = containedTests(model, grant)
|
|
653
|
+
for (let index = 0; index < contained.length; index++) {
|
|
654
|
+
const test = contained[index]
|
|
655
|
+
if (test !== undefined) {
|
|
656
|
+
groups[groups.length] = [{ column: test.column, value: test.values }]
|
|
481
657
|
}
|
|
482
658
|
}
|
|
483
659
|
if (count > MAX_WITHIN_VALUES) {
|
|
@@ -830,6 +1006,10 @@ export function readValues(values: unknown, allowed: NameSet, what: 'insert' | '
|
|
|
830
1006
|
copied.set(key, fileValue(model, key, value))
|
|
831
1007
|
continue
|
|
832
1008
|
}
|
|
1009
|
+
if (inSet(model.json, key)) {
|
|
1010
|
+
copied.set(key, value === null ? null : jsonInput(value, `${table}.${key}`))
|
|
1011
|
+
continue
|
|
1012
|
+
}
|
|
833
1013
|
if (value !== null && !isPrimitive(value)) {
|
|
834
1014
|
throw new QueryRefusedError('invalid_argument', `${table}.${key} takes a plain value (string, number, bigint, boolean, Date or null)`)
|
|
835
1015
|
}
|
|
@@ -895,29 +1075,40 @@ async function selectRows<S>(
|
|
|
895
1075
|
if (scope === undefined) {
|
|
896
1076
|
return []
|
|
897
1077
|
}
|
|
898
|
-
const
|
|
1078
|
+
const dialect = dialectOf(context)
|
|
1079
|
+
const node = selectNode(boundState(context, state), visible, scope, undefined, context.model.dates, dialect)
|
|
899
1080
|
const rows = await (await run.read()).run<S>(node, guard(context, 'SelectQueryNode', scope))
|
|
900
|
-
return
|
|
1081
|
+
return answerRows(dialect, context.model.name, joinedColumns(context, state), rows)
|
|
901
1082
|
}
|
|
902
1083
|
|
|
903
|
-
/** The
|
|
904
|
-
function
|
|
1084
|
+
/** The column sets of a select's rows: the table's own, and each joined child's under its `<child>.<column>` alias. */
|
|
1085
|
+
function joinedColumns(context: TableContext, state: SelectState): RowColumns {
|
|
1086
|
+
const own = rowColumnsOf(context.model)
|
|
905
1087
|
if (state.joins.length === 0) {
|
|
906
1088
|
return own
|
|
907
1089
|
}
|
|
908
|
-
const
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
1090
|
+
const joined = (pick: (columns: RowColumns) => NameSet): NameSet => {
|
|
1091
|
+
const names: string[] = []
|
|
1092
|
+
const owned = namesOf(pick(own))
|
|
1093
|
+
for (let index = 0; index < owned.length; index++) {
|
|
1094
|
+
names[names.length] = owned[index] ?? ''
|
|
1095
|
+
}
|
|
1096
|
+
for (let index = 0; index < state.joins.length; index++) {
|
|
1097
|
+
const table = state.joins[index]?.join.shape.table ?? ''
|
|
1098
|
+
const child = ownEntry(context.children, table)
|
|
1099
|
+
const columns = child === undefined ? [] : namesOf(pick(rowColumnsOf(child)))
|
|
1100
|
+
for (let position = 0; position < columns.length; position++) {
|
|
1101
|
+
names[names.length] = `${table}.${columns[position]}`
|
|
1102
|
+
}
|
|
918
1103
|
}
|
|
1104
|
+
return nameSet(names)
|
|
1105
|
+
}
|
|
1106
|
+
return {
|
|
1107
|
+
days: joined((columns) => columns.days),
|
|
1108
|
+
timestamps: joined((columns) => columns.timestamps),
|
|
1109
|
+
booleans: joined((columns) => columns.booleans),
|
|
1110
|
+
json: joined((columns) => columns.json),
|
|
919
1111
|
}
|
|
920
|
-
return nameSet(names)
|
|
921
1112
|
}
|
|
922
1113
|
|
|
923
1114
|
/**
|
|
@@ -933,10 +1124,11 @@ export async function selectOne<O>(
|
|
|
933
1124
|
): Promise<O> {
|
|
934
1125
|
const start = startSelect(context.shape)
|
|
935
1126
|
const state = finishSelect(start, start)
|
|
936
|
-
const
|
|
1127
|
+
const dialect = dialectOf(context)
|
|
1128
|
+
const node = selectNode(state, visible, scope, undefined, context.model.dates, dialect)
|
|
937
1129
|
const session = await run.read()
|
|
938
1130
|
const rows = await session.run<O>(node, guard(context, 'SelectQueryNode', scope))
|
|
939
|
-
const [row] =
|
|
1131
|
+
const [row] = answerRows(dialect, context.model.name, rowColumnsOf(context.model), rows)
|
|
940
1132
|
if (row === undefined) {
|
|
941
1133
|
throw new NotFoundError(`${context.model.name} not found`)
|
|
942
1134
|
}
|
|
@@ -1027,9 +1219,10 @@ async function lockDestination(transaction: Session, context: TableContext, gran
|
|
|
1027
1219
|
export async function insertRow<O>(
|
|
1028
1220
|
context: TableContext,
|
|
1029
1221
|
run: Runner,
|
|
1030
|
-
|
|
1222
|
+
given: ReadonlyMap<string, Primitive | null>,
|
|
1031
1223
|
destination: RefGrant | undefined,
|
|
1032
1224
|
): Promise<O> {
|
|
1225
|
+
const values = boundValues(context, given)
|
|
1033
1226
|
return run.write(async (transaction) => {
|
|
1034
1227
|
if (destination !== undefined) {
|
|
1035
1228
|
await lockDestination(transaction, context, destination)
|
|
@@ -1081,6 +1274,10 @@ export function onConflictOf(
|
|
|
1081
1274
|
if (!inSet(model.columns, name)) {
|
|
1082
1275
|
throw new QueryRefusedError('on_conflict', `onConflict names ${String(name)}, which is not a column of ${model.name}`)
|
|
1083
1276
|
}
|
|
1277
|
+
if (inSet(model.json, name)) {
|
|
1278
|
+
// jsonb compares by value and SQLite's text by spelling: the two dialects would disagree about a conflict.
|
|
1279
|
+
throw new QueryRefusedError('on_conflict', `onConflict names ${name}, a JSON column of ${model.name}, which the data layer never compares`)
|
|
1280
|
+
}
|
|
1084
1281
|
return name
|
|
1085
1282
|
})
|
|
1086
1283
|
if (columns === undefined || columns.length === 0) {
|
|
@@ -1172,10 +1369,12 @@ async function insertOnConflict(
|
|
|
1172
1369
|
context: TableContext,
|
|
1173
1370
|
run: Runner,
|
|
1174
1371
|
transaction: Session,
|
|
1175
|
-
|
|
1372
|
+
given: ReadonlyMap<string, Primitive | null>,
|
|
1176
1373
|
conflict: OnConflict,
|
|
1177
1374
|
): Promise<Readonly<Record<string, unknown>>> {
|
|
1178
1375
|
const model = context.model
|
|
1376
|
+
const values = boundValues(context, given)
|
|
1377
|
+
const updates = boundValues(context, conflict.updates)
|
|
1179
1378
|
const matches: OperationNode[] = []
|
|
1180
1379
|
for (let index = 0; index < conflict.columns.length; index++) {
|
|
1181
1380
|
const name = conflict.columns[index] ?? ''
|
|
@@ -1201,17 +1400,17 @@ async function insertOnConflict(
|
|
|
1201
1400
|
return { inserted: false }
|
|
1202
1401
|
}
|
|
1203
1402
|
const where = scopedWhere(allOf(matches), scope)
|
|
1204
|
-
const unchanged =
|
|
1403
|
+
const unchanged = updates.size === 0
|
|
1205
1404
|
const update = unchanged
|
|
1206
1405
|
? acknowledgementNode(model.name, keyOf(model), model.version, where)
|
|
1207
|
-
: updateNode(model.name,
|
|
1406
|
+
: updateNode(model.name, updates, model.version, where, answer(context))
|
|
1208
1407
|
const [updated] = await transaction.run<Readonly<Record<string, unknown>>>(
|
|
1209
1408
|
update,
|
|
1210
1409
|
guard(context, unchanged ? 'SelectQueryNode' : 'UpdateQueryNode', scope),
|
|
1211
1410
|
)
|
|
1212
1411
|
if (updated !== undefined) {
|
|
1213
1412
|
checkVersion(context, updated)
|
|
1214
|
-
await referenceUpdated(context, run, transaction,
|
|
1413
|
+
await referenceUpdated(context, run, transaction, updates, updated)
|
|
1215
1414
|
return { ...updated, inserted: false }
|
|
1216
1415
|
}
|
|
1217
1416
|
}
|
|
@@ -1264,11 +1463,12 @@ export async function updateRow<O>(
|
|
|
1264
1463
|
context: TableContext,
|
|
1265
1464
|
run: Runner,
|
|
1266
1465
|
target: Addressed,
|
|
1267
|
-
|
|
1466
|
+
given: ReadonlyMap<string, Primitive | null>,
|
|
1268
1467
|
expectedVersion: OperationNode | undefined,
|
|
1269
1468
|
destination?: RefGrant,
|
|
1270
1469
|
): Promise<O> {
|
|
1271
1470
|
const { name: table, version } = context.model
|
|
1471
|
+
const changes = boundValues(context, given)
|
|
1272
1472
|
const where = scopedWhere(conditionsWith(target, expectedVersion), target.scope)
|
|
1273
1473
|
const unchanged = changes.size === 0
|
|
1274
1474
|
const node = unchanged
|
|
@@ -1303,11 +1503,11 @@ type Columns<Row> = keyof Row & string
|
|
|
1303
1503
|
type NumericColumn<Row> = { [K in Columns<Row>]: Exclude<Row[K], null> extends number | bigint | string ? K : never }[Columns<Row>]
|
|
1304
1504
|
|
|
1305
1505
|
export interface AggregateSpec<Row> {
|
|
1306
|
-
readonly groupBy?: readonly
|
|
1506
|
+
readonly groupBy?: readonly ComparableColumn<Row>[]
|
|
1307
1507
|
readonly count?: boolean
|
|
1308
1508
|
readonly sum?: readonly NumericColumn<Row>[]
|
|
1309
|
-
readonly min?: readonly
|
|
1310
|
-
readonly max?: readonly
|
|
1509
|
+
readonly min?: readonly ComparableColumn<Row>[]
|
|
1510
|
+
readonly max?: readonly ComparableColumn<Row>[]
|
|
1311
1511
|
readonly where?: (q: ScopedSelect<Row, Row, NoColumns>) => ScopedSelect<Row, unknown, NoColumns>
|
|
1312
1512
|
}
|
|
1313
1513
|
|
|
@@ -1344,7 +1544,7 @@ function aggregateColumns(table: string, shape: TableShape, spec: AggregateInput
|
|
|
1344
1544
|
if (!inSet(shape.visible, name)) {
|
|
1345
1545
|
throw new QueryRefusedError('unknown_column', `${String(name)} is not a column of ${table} (columns: ${namesOf(shape.visible).join(', ')})`)
|
|
1346
1546
|
}
|
|
1347
|
-
return name
|
|
1547
|
+
return comparableColumnOf(shape, name, 'aggregates')
|
|
1348
1548
|
})
|
|
1349
1549
|
if (names === undefined) {
|
|
1350
1550
|
throw new QueryRefusedError('invalid_argument', `aggregate ${field} takes a plain array of column names`)
|
|
@@ -1389,18 +1589,27 @@ export async function aggregateRows<Row, O>(
|
|
|
1389
1589
|
if (scope === undefined) {
|
|
1390
1590
|
return []
|
|
1391
1591
|
}
|
|
1392
|
-
const
|
|
1592
|
+
const dialect = dialectOf(context)
|
|
1593
|
+
const conditions = boundConditions(context, context.model, filter?.conditions ?? [])
|
|
1594
|
+
const node = aggregateNode(context.model.name, columns, conditions, scope, context.model.dates)
|
|
1393
1595
|
const rows = await (await run.read()).run<O>(node, guard(context, 'SelectQueryNode', scope))
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1596
|
+
const own = rowColumnsOf(context.model)
|
|
1597
|
+
const answered = answerRows(dialect, context.model.name, {
|
|
1598
|
+
days: aggregated(own.days, columns),
|
|
1599
|
+
timestamps: aggregated(own.timestamps, columns),
|
|
1600
|
+
booleans: aggregated(own.booleans, columns),
|
|
1601
|
+
json: nameSet([]),
|
|
1602
|
+
}, rows)
|
|
1603
|
+
return dialect === 'postgres' ? answered : sqliteAggregates(context.model.name, columns, answered)
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
/** The columns of `set` among an aggregate's: a grouped one by its name, and its `min_` and `max_`. */
|
|
1607
|
+
function aggregated(set: NameSet, columns: AggregateColumns): NameSet {
|
|
1399
1608
|
const names: string[] = []
|
|
1400
1609
|
const add = (prefix: string, list: readonly string[]) => {
|
|
1401
1610
|
for (let index = 0; index < list.length; index++) {
|
|
1402
1611
|
const name = list[index] ?? ''
|
|
1403
|
-
if (inSet(
|
|
1612
|
+
if (inSet(set, name)) {
|
|
1404
1613
|
names[names.length] = `${prefix}${name}`
|
|
1405
1614
|
}
|
|
1406
1615
|
}
|
|
@@ -1411,6 +1620,37 @@ function aggregateDays(dates: NameSet, columns: AggregateColumns): NameSet {
|
|
|
1411
1620
|
return nameSet(names)
|
|
1412
1621
|
}
|
|
1413
1622
|
|
|
1623
|
+
/**
|
|
1624
|
+
* An aggregate's numbers as Postgres's driver answers them: `count` as bigint text, and an integral `sum` as text too
|
|
1625
|
+
* (Postgres sums an integer as bigint or numeric). SQLite answers both as numbers, and a whole sum of a REAL column is
|
|
1626
|
+
* text here where Postgres's float8 sum is a number; `AggregateRow` types a sum as either.
|
|
1627
|
+
*/
|
|
1628
|
+
function sqliteAggregates<O>(table: string, columns: AggregateColumns, rows: O[]): O[] {
|
|
1629
|
+
const integral = (where: string, value: unknown): unknown => {
|
|
1630
|
+
if (typeof value !== 'number' || !Number.isInteger(value)) {
|
|
1631
|
+
return value
|
|
1632
|
+
}
|
|
1633
|
+
if (!isSafeInteger(value)) {
|
|
1634
|
+
throw new QueryRefusedError('unexpected_row', `${where} came back as an integer past the safe range, which a number cannot hold exactly`)
|
|
1635
|
+
}
|
|
1636
|
+
return String(value)
|
|
1637
|
+
}
|
|
1638
|
+
for (let index = 0; index < rows.length; index++) {
|
|
1639
|
+
const row = rows[index]
|
|
1640
|
+
if (typeof row !== 'object' || row === null) {
|
|
1641
|
+
continue
|
|
1642
|
+
}
|
|
1643
|
+
if (columns.count) {
|
|
1644
|
+
defineValue(row, 'count', integral(`${table}.count`, ownDescriptor(row, 'count')?.value))
|
|
1645
|
+
}
|
|
1646
|
+
for (let position = 0; position < columns.sum.length; position++) {
|
|
1647
|
+
const name = `sum_${columns.sum[position] ?? ''}`
|
|
1648
|
+
defineValue(row, name, integral(`${table}.${name}`, ownDescriptor(row, name)?.value))
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
return rows
|
|
1652
|
+
}
|
|
1653
|
+
|
|
1414
1654
|
/** An insert's row, stamped from its capability, the destination it locks, and what it does on a conflict. */
|
|
1415
1655
|
export interface PreparedInsert {
|
|
1416
1656
|
readonly row: Map<string, Primitive | null>
|
|
@@ -1433,10 +1673,8 @@ export function prepareDimensionInsert(context: TableContext, target: unknown, v
|
|
|
1433
1673
|
stampInsert(context, grant, row, scope.key, grant.id)
|
|
1434
1674
|
return { row, destination: undefined, conflict }
|
|
1435
1675
|
}
|
|
1436
|
-
stampInsert(context, grant, row, scope.key,
|
|
1437
|
-
|
|
1438
|
-
row.set(link.column, value)
|
|
1439
|
-
}
|
|
1676
|
+
stampInsert(context, grant, row, scope.key, uuidv7())
|
|
1677
|
+
stampChain(row, linkValues(context.model.name, context.model.chain, grant))
|
|
1440
1678
|
return { row, destination: grant, conflict }
|
|
1441
1679
|
}
|
|
1442
1680
|
|
|
@@ -1445,7 +1683,7 @@ export function prepareApplicationInsert(context: TableContext, capability: unkn
|
|
|
1445
1683
|
const grant = appGrant(context, capability, 'insert')
|
|
1446
1684
|
const row = readValues(values, context.model.insert, 'insert', context.model)
|
|
1447
1685
|
const conflict = onConflictOf(context, options, grant.action, row)
|
|
1448
|
-
stampInsert(context, grant, row, keyOf(context.model),
|
|
1686
|
+
stampInsert(context, grant, row, keyOf(context.model), uuidv7())
|
|
1449
1687
|
return { row, destination: undefined, conflict }
|
|
1450
1688
|
}
|
|
1451
1689
|
|
|
@@ -1455,13 +1693,21 @@ export function prepareChildInsert(context: TableContext, parent: unknown, value
|
|
|
1455
1693
|
const grant = refGrant(context, parent, scope.parent.dimension, 'insert')
|
|
1456
1694
|
const row = readValues(values, context.model.insert, 'insert', context.model)
|
|
1457
1695
|
const conflict = onConflictOf(context, options, grant.action, row)
|
|
1458
|
-
stampInsert(context, grant, row, scope.key,
|
|
1459
|
-
|
|
1460
|
-
row.set(link.column, value)
|
|
1461
|
-
}
|
|
1696
|
+
stampInsert(context, grant, row, scope.key, uuidv7())
|
|
1697
|
+
stampChain(row, linkValues(context.model.name, context.model.chain, grant))
|
|
1462
1698
|
return { row, destination: grant, conflict }
|
|
1463
1699
|
}
|
|
1464
1700
|
|
|
1701
|
+
/** Writes each chain column the destination `Ref` settles into an insert's row. */
|
|
1702
|
+
function stampChain(row: Map<string, Primitive | null>, values: readonly LinkValue[]): void {
|
|
1703
|
+
for (let index = 0; index < values.length; index++) {
|
|
1704
|
+
const entry = values[index]
|
|
1705
|
+
if (entry !== undefined) {
|
|
1706
|
+
row.set(entry.link.column, entry.value)
|
|
1707
|
+
}
|
|
1708
|
+
}
|
|
1709
|
+
}
|
|
1710
|
+
|
|
1465
1711
|
/** A prepared insert, with its conflict handling when the caller named one. */
|
|
1466
1712
|
export function insertPrepared(context: TableContext, run: Runner, prepared: PreparedInsert): Promise<Readonly<Record<string, unknown>>> {
|
|
1467
1713
|
const conflict = prepared.conflict
|
|
@@ -1565,9 +1811,17 @@ export class DimensionTable<DB, P, T extends string> {
|
|
|
1565
1811
|
const scope = objectPredicate(context, ref, 'delete')
|
|
1566
1812
|
const target = refGrant(context, to, container.dimension, 'insert')
|
|
1567
1813
|
const values = linkValues(context.model.name, context.model.chain, target)
|
|
1568
|
-
const changes = new Map<string, Primitive | null>(
|
|
1814
|
+
const changes = new Map<string, Primitive | null>()
|
|
1815
|
+
stampChain(changes, values)
|
|
1816
|
+
const chain: Record<string, string> = {}
|
|
1817
|
+
for (let index = 0; index < values.length; index++) {
|
|
1818
|
+
const entry = values[index]
|
|
1819
|
+
if (entry !== undefined) {
|
|
1820
|
+
setOwn(chain, entry.link.dimension, entry.value)
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1569
1823
|
const acknowledged = await updateRow<Acknowledgement<P, T>>(context, storeRunner(context), addressed(scope), changes, undefined, target)
|
|
1570
|
-
return { ...acknowledged, chain
|
|
1824
|
+
return { ...acknowledged, chain }
|
|
1571
1825
|
}
|
|
1572
1826
|
|
|
1573
1827
|
async aggregate<const S extends AggregateSpec<Row<DB, P, T>>>(
|
|
@@ -1721,7 +1975,7 @@ function probeOf(context: TableContext): TableProbe {
|
|
|
1721
1975
|
)
|
|
1722
1976
|
},
|
|
1723
1977
|
group: async (capability: unknown, columns: readonly string[]) =>
|
|
1724
|
-
aggregateRows<Readonly<Record<string,
|
|
1978
|
+
aggregateRows<Readonly<Record<string, Primitive | null>>, Readonly<Record<string, unknown>>>(
|
|
1725
1979
|
context,
|
|
1726
1980
|
run,
|
|
1727
1981
|
list(capability, 'rows'),
|
|
@@ -1793,7 +2047,7 @@ function appProbeOf(context: TableContext): TableProbe {
|
|
|
1793
2047
|
},
|
|
1794
2048
|
count,
|
|
1795
2049
|
group: async (capability: unknown, columns: readonly string[]) =>
|
|
1796
|
-
aggregateRows<Readonly<Record<string,
|
|
2050
|
+
aggregateRows<Readonly<Record<string, Primitive | null>>, Readonly<Record<string, unknown>>>(context, run, every(capability), context.shape, {
|
|
1797
2051
|
groupBy: columns,
|
|
1798
2052
|
count: true,
|
|
1799
2053
|
}),
|
|
@@ -1827,11 +2081,11 @@ function destinationOf(models: TableModels, model: TableModel, parent: TableMode
|
|
|
1827
2081
|
|
|
1828
2082
|
/** What a select or an aggregate of a capability that reads `visible` may name: those columns, and no join. */
|
|
1829
2083
|
export function actionShape(model: TableModel, visible: readonly string[]): TableShape {
|
|
1830
|
-
return freeze({ table: model.name, visible: nameSet(visible), joinable: freeze({}), dates: model.dates })
|
|
2084
|
+
return freeze({ table: model.name, visible: nameSet(visible), joinable: freeze({}), dates: model.dates, json: model.json })
|
|
1831
2085
|
}
|
|
1832
2086
|
|
|
1833
2087
|
function shapeOf(model: TableModel, joinable: Readonly<Record<string, JoinShape>>): TableShape {
|
|
1834
|
-
return freeze({ table: model.name, visible: nameSet(model.visible), joinable, dates: model.dates })
|
|
2088
|
+
return freeze({ table: model.name, visible: nameSet(model.visible), joinable, dates: model.dates, json: model.json })
|
|
1835
2089
|
}
|
|
1836
2090
|
|
|
1837
2091
|
function joinShapes(models: TableModels, parent: TableModel): Readonly<Record<string, JoinShape>> {
|
|
@@ -1900,8 +2154,23 @@ export function scoped<DB, P>(tables: Tables<DB, P>): ScopedDatabase<DB, P> {
|
|
|
1900
2154
|
setOwn(joins, child, join.on)
|
|
1901
2155
|
}
|
|
1902
2156
|
}
|
|
2157
|
+
const childModels: Record<string, TableModel> = {}
|
|
2158
|
+
for (let index = 0; index < children.length; index++) {
|
|
2159
|
+
const child = modelOf(models, children[index] ?? '')
|
|
2160
|
+
if (child !== undefined) {
|
|
2161
|
+
setOwn(childModels, child.name, child)
|
|
2162
|
+
}
|
|
2163
|
+
}
|
|
1903
2164
|
const shape = shapeOf(model, joinable)
|
|
1904
|
-
return freeze({
|
|
2165
|
+
return freeze({
|
|
2166
|
+
model,
|
|
2167
|
+
parent,
|
|
2168
|
+
destination: destinationOf(models, model, parent),
|
|
2169
|
+
shape,
|
|
2170
|
+
joins: freeze(joins),
|
|
2171
|
+
children: freeze(childModels),
|
|
2172
|
+
connection,
|
|
2173
|
+
})
|
|
1905
2174
|
}
|
|
1906
2175
|
|
|
1907
2176
|
const database: ScopedDatabase<DB, P> = Object.freeze({ tables: tablePolicies(tables) })
|
|
@@ -1917,17 +2186,30 @@ export function scoped<DB, P>(tables: Tables<DB, P>): ScopedDatabase<DB, P> {
|
|
|
1917
2186
|
}
|
|
1918
2187
|
return probeOf(contextFor(table, mode))
|
|
1919
2188
|
},
|
|
1920
|
-
bind(runtime: DataLayerRuntime):
|
|
2189
|
+
bind(runtime: DataLayerRuntime): ProvisionalBinding {
|
|
1921
2190
|
if (connection.runtime !== undefined) {
|
|
1922
2191
|
throw new CapabilityError('layer_unbound', 'the data layer is already connected; only the framework connects it, once, at boot')
|
|
1923
2192
|
}
|
|
1924
|
-
const bound: DataLayerRuntime = freeze({
|
|
2193
|
+
const bound: DataLayerRuntime = freeze({
|
|
2194
|
+
database: runtime.database,
|
|
2195
|
+
capabilities: runtime.capabilities,
|
|
2196
|
+
readOnly: runtime.readOnly,
|
|
2197
|
+
projectWakes: runtime.projectWakes,
|
|
2198
|
+
})
|
|
1925
2199
|
connection.runtime = bound
|
|
1926
|
-
return
|
|
2200
|
+
return freeze({
|
|
2201
|
+
locate: async (dimension: string, id: string, using?: Session) => locateObject(models, bound, dimension, id, using),
|
|
2202
|
+
release() {
|
|
2203
|
+
if (connection.runtime === bound) {
|
|
2204
|
+
connection.runtime = undefined
|
|
2205
|
+
}
|
|
2206
|
+
},
|
|
2207
|
+
})
|
|
1927
2208
|
},
|
|
1928
2209
|
framework: freeze({
|
|
1929
2210
|
webhooks: webhookTables(() => runtimeOf(connection)),
|
|
1930
|
-
jobs: jobLeases(() => runtimeOf(connection)),
|
|
2211
|
+
jobs: jobLeases(() => runtimeOf(connection), leaseProjection(() => runtimeOf(connection))),
|
|
2212
|
+
wakes: jobWakes(() => runtimeOf(connection)),
|
|
1931
2213
|
admissions: admissions(() => runtimeOf(connection)),
|
|
1932
2214
|
files: storedFiles(() => runtimeOf(connection)),
|
|
1933
2215
|
}),
|