@fabrika/db 0.9.0-dev.22 → 0.9.0-dev.26
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/codegen.ts +1 -1
- package/src/errors.ts +2 -1
- package/src/file-columns.ts +5 -5
- package/src/framework.ts +1 -0
- package/src/index.ts +1 -0
- package/src/migrations-sqlite.ts +18 -0
- package/src/migrations.ts +18 -0
- package/src/orm-types.ts +29 -6
- package/src/orm.ts +100 -0
- package/src/policy.ts +89 -28
- package/src/registry.ts +3 -0
- package/src/request-receipts.ts +107 -0
- package/src/store.ts +2 -0
- package/src/tables.ts +4 -2
- package/src/vocabulary-tables.ts +42 -16
- package/types/src/errors.d.ts +2 -1
- package/types/src/framework.d.ts +1 -0
- package/types/src/index.d.ts +1 -1
- package/types/src/migrations.d.ts +1 -0
- package/types/src/orm-types.d.ts +24 -8
- package/types/src/orm.d.ts +11 -0
- package/types/src/policy.d.ts +12 -10
- package/types/src/registry.d.ts +3 -0
- package/types/src/request-receipts.d.ts +22 -0
- package/types/src/tables.d.ts +1 -1
- package/types/src/vocabulary-tables.d.ts +35 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabrika/db",
|
|
3
|
-
"version": "0.9.0-dev.
|
|
3
|
+
"version": "0.9.0-dev.26",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -92,9 +92,9 @@
|
|
|
92
92
|
"test": "bun test"
|
|
93
93
|
},
|
|
94
94
|
"dependencies": {
|
|
95
|
-
"@fabrika/auth-core": "0.9.0-dev.
|
|
96
|
-
"@fabrika/platform": "0.9.0-dev.
|
|
97
|
-
"@fabrika/platform-node": "0.9.0-dev.
|
|
95
|
+
"@fabrika/auth-core": "0.9.0-dev.26",
|
|
96
|
+
"@fabrika/platform": "0.9.0-dev.26",
|
|
97
|
+
"@fabrika/platform-node": "0.9.0-dev.26",
|
|
98
98
|
"kysely": "0.29.6",
|
|
99
99
|
"kysely-postgres-js": "5.0.1",
|
|
100
100
|
"zod": "^3.25.76"
|
package/src/codegen.ts
CHANGED
|
@@ -167,7 +167,7 @@ export async function introspectCatalog(url: string, schema: string, objects: re
|
|
|
167
167
|
if (!Array.isArray(namespaces) || namespaces.length === 0) {
|
|
168
168
|
throw new CodegenError(`schema ${schema} does not exist; run fabrika db migrate first`)
|
|
169
169
|
}
|
|
170
|
-
// The migration runner's own pre-commit check, so an object created outside a migration fails
|
|
170
|
+
// The migration runner's own pre-commit check, so an object created outside a migration fails the check.
|
|
171
171
|
const code = z.array(z.object({ violation: z.string() })).parse(await tx.unsafe(databaseCodeQuery(schema, objects)))
|
|
172
172
|
if (code.length > 0) {
|
|
173
173
|
throw new CodegenError(
|
package/src/errors.ts
CHANGED
|
@@ -49,6 +49,7 @@ export const QUERY_REFUSALS = {
|
|
|
49
49
|
statement_shape: 'the compiled statement has a form the guard does not admit',
|
|
50
50
|
scope_predicate: 'the scope predicate is missing or is not a top-level conjunct',
|
|
51
51
|
list_page: 'a list limit, cursor or order outside what a page admits',
|
|
52
|
+
too_many_rows: 'all() matched more rows than its cap (MAX_ALL_ROWS); it refuses rather than truncate',
|
|
52
53
|
undeclared_reach: 'the site reaches data it did not declare (sql or unscoped, or their mode)',
|
|
53
54
|
sql_template: 'ctx.sql was not called as one tagged template whose values are interpolated whole',
|
|
54
55
|
sql_statement: 'ctx.sql runs one statement of an admitted kind, and none inside another',
|
|
@@ -60,7 +61,7 @@ export const QUERY_REFUSALS = {
|
|
|
60
61
|
file_scan: "a scan verdict outside the scan job's lease",
|
|
61
62
|
claim_site: 'claim leases rows in a transaction of its own: a job calls it on ctx.db, never inside ctx.transaction or a request',
|
|
62
63
|
on_conflict: "an insert's onConflict names no column of the table, a column the insert leaves empty, or another action",
|
|
63
|
-
public_reach: "a public procedure updates, removes, locks or claims no row, and an insert's conflict writes nothing",
|
|
64
|
+
public_reach: "a public procedure updates, removes, locks or claims no row, reads none with all(), and an insert's conflict writes nothing",
|
|
64
65
|
file_value:
|
|
65
66
|
"a file column takes a file reference this request holds and may read (an incoming file, a put, or a read under the table's read action) of the column's kind, or null",
|
|
66
67
|
file_scope: 'a file is referenced from another top-level scope than the one its first reference set',
|
package/src/file-columns.ts
CHANGED
|
@@ -9,7 +9,7 @@ import type { Primitive } from './description'
|
|
|
9
9
|
import { NotFoundError, QueryRefusedError } from './errors'
|
|
10
10
|
import { fileHoldingOf, fileReference, holdsFile } from './file-references'
|
|
11
11
|
import { isPlainIdentifier } from './identifier'
|
|
12
|
-
import { includes, type ReadReach, readsAsAction, type TableModel } from './policy'
|
|
12
|
+
import { includes, type ReadReach, readsAsAction, readsAsOneOf, type TableModel } from './policy'
|
|
13
13
|
import { claimStoredFile, lockStoredFile, stampStoredFile, storedFilesMetadata } from './stored-files'
|
|
14
14
|
|
|
15
15
|
/** Where a site's file references belong, and what claiming a pending one needs. */
|
|
@@ -26,17 +26,17 @@ export interface FileBinding {
|
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* Whether a reference read under `reach` (one action, or an `anyOf` endpoint's held actions that read the row) lets its
|
|
29
|
-
* holder serve, open and copy the file: a table read action, or the column's own read
|
|
29
|
+
* holder serve, open and copy the file: a table read action, or one of the column's own read actions, that reads every answered
|
|
30
30
|
* row alone (`readsAsAction`), or a table read action among `held`, the actions a job holds over the whole application.
|
|
31
31
|
* When every action of the reach is a table read action, each answered row was admitted by one that alone reads it,
|
|
32
|
-
* so a column without its own read action is readable. A column with its own read
|
|
33
|
-
*
|
|
32
|
+
* so a column without its own read action is readable. A column with its own read actions is answered only under one of
|
|
33
|
+
* them (`visibleThrough`), so `held` never widens it. A table read with `'any'` admits whoever read the row: the
|
|
34
34
|
* store refused every other reader.
|
|
35
35
|
*/
|
|
36
36
|
function readsFile(model: TableModel, column: string, reach: ReadReach, held: NameSet): boolean {
|
|
37
37
|
const own = hasOwn(model.columnReads, column) ? model.columnReads[column] : undefined
|
|
38
38
|
if (own !== undefined) {
|
|
39
|
-
return
|
|
39
|
+
return readsAsOneOf(model, own, reach)
|
|
40
40
|
}
|
|
41
41
|
if (model.actions.readAny || readsThroughEvery(model, reach)) {
|
|
42
42
|
return true
|
package/src/framework.ts
CHANGED
|
@@ -25,6 +25,7 @@ export type { JobLeases, JobLeaseState, JobOutcome, LeaseChange, LeaseProjection
|
|
|
25
25
|
export type { FinishWakes, JobWakes, NewWake, Wake, WakeReason } from './job-wakes'
|
|
26
26
|
export type { DataAccess, DataHandles, DataSite } from './orm'
|
|
27
27
|
export type { FrameworkTables, Locator, MemberRevealCheck, PathCheck, ProvisionalBinding, RelationCheck, RevealCheck } from './registry'
|
|
28
|
+
export type { RequestReceipt, RequestReceipts } from './request-receipts'
|
|
28
29
|
export type { SqlMode, SqlSite, SqlTag, SqlValue } from './sql'
|
|
29
30
|
export type { FileColumnRef, QuarantinedStoredFile, ServedFile, StagedFile, StoredFileMeta, StoredFiles, StoredFileStatus } from './stored-files'
|
|
30
31
|
|
package/src/index.ts
CHANGED
package/src/migrations-sqlite.ts
CHANGED
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
JOB_KICKS_TABLE,
|
|
19
19
|
JOB_LEASES_TABLE,
|
|
20
20
|
JOB_WAKES_TABLE,
|
|
21
|
+
REQUEST_RECEIPTS_TABLE,
|
|
21
22
|
WEBHOOK_BINDINGS_TABLE,
|
|
22
23
|
WEBHOOK_DELIVERIES_TABLE,
|
|
23
24
|
} from './migrations'
|
|
@@ -158,6 +159,23 @@ CREATE INDEX ${FILE_DELETIONS_TABLE}_recorded_at ON ${FILE_DELETIONS_TABLE} (rec
|
|
|
158
159
|
sql: `-- The wakes the run holding the job's lease took when it took the lease: its finish consumes or retries exactly these.
|
|
159
160
|
-- A wake written or merged into afterwards is not taken, though due before the run started: the run may not have read its work.
|
|
160
161
|
ALTER TABLE ${JOB_WAKES_TABLE} ADD COLUMN owned INTEGER NOT NULL DEFAULT 0 CHECK (owned IN (0, 1));
|
|
162
|
+
`,
|
|
163
|
+
}),
|
|
164
|
+
Object.freeze({
|
|
165
|
+
name: '0005_request_receipts.sql',
|
|
166
|
+
sql: `-- The answer of a mutation whose request carried a key (ADR-0078), for a repeat of that request: the principal's key,
|
|
167
|
+
-- the operation and the SHA-256 of its input, and the output's JSON. created_at is epoch milliseconds; each write prunes
|
|
168
|
+
-- a batch of receipts past the retention.
|
|
169
|
+
CREATE TABLE ${REQUEST_RECEIPTS_TABLE} (
|
|
170
|
+
principal TEXT NOT NULL,
|
|
171
|
+
request_key TEXT NOT NULL,
|
|
172
|
+
operation TEXT NOT NULL,
|
|
173
|
+
identity TEXT NOT NULL,
|
|
174
|
+
output TEXT NOT NULL,
|
|
175
|
+
created_at INTEGER NOT NULL,
|
|
176
|
+
PRIMARY KEY (principal, request_key)
|
|
177
|
+
) STRICT;
|
|
178
|
+
CREATE INDEX ${REQUEST_RECEIPTS_TABLE}_created_at ON ${REQUEST_RECEIPTS_TABLE} (created_at);
|
|
161
179
|
`,
|
|
162
180
|
}),
|
|
163
181
|
])
|
package/src/migrations.ts
CHANGED
|
@@ -16,6 +16,7 @@ export const FORM_FILES_TABLE = 'fabrika_form_files'
|
|
|
16
16
|
export const FORM_BYTES_TABLE = 'fabrika_form_bytes'
|
|
17
17
|
export const FILES_TABLE = 'fabrika_files'
|
|
18
18
|
export const FILE_DELETIONS_TABLE = 'fabrika_file_deletions'
|
|
19
|
+
export const REQUEST_RECEIPTS_TABLE = 'fabrika_request_receipts'
|
|
19
20
|
/** The function an application's last adoption migration calls with the name of a file collection it adopted. */
|
|
20
21
|
export const FILES_RELEASE_FUNCTION = 'fabrika_files_release'
|
|
21
22
|
/**
|
|
@@ -279,6 +280,23 @@ CREATE UNIQUE INDEX ${JOB_WAKES_TABLE}_dedupe ON ${JOB_WAKES_TABLE} (dedupe) WHE
|
|
|
279
280
|
sql: `-- The wakes the run holding the job's lease took when it took the lease: its finish consumes or retries exactly these.
|
|
280
281
|
-- A wake written or merged into afterwards is not taken, though due before the run started: the run may not have read its work.
|
|
281
282
|
ALTER TABLE ${JOB_WAKES_TABLE} ADD COLUMN owned BOOLEAN NOT NULL DEFAULT false;
|
|
283
|
+
`,
|
|
284
|
+
}),
|
|
285
|
+
Object.freeze({
|
|
286
|
+
name: '0011_request_receipts.sql',
|
|
287
|
+
sql: `-- The answer of a mutation whose request carried a key (ADR-0078), for a repeat of that request: the principal's key,
|
|
288
|
+
-- the operation and the SHA-256 of its input, and the output's JSON. created_at is epoch milliseconds; each write prunes
|
|
289
|
+
-- a batch of receipts past the retention.
|
|
290
|
+
CREATE TABLE ${REQUEST_RECEIPTS_TABLE} (
|
|
291
|
+
principal TEXT NOT NULL,
|
|
292
|
+
request_key TEXT NOT NULL,
|
|
293
|
+
operation TEXT NOT NULL,
|
|
294
|
+
identity TEXT NOT NULL,
|
|
295
|
+
output TEXT NOT NULL,
|
|
296
|
+
created_at BIGINT NOT NULL,
|
|
297
|
+
PRIMARY KEY (principal, request_key)
|
|
298
|
+
);
|
|
299
|
+
CREATE INDEX ${REQUEST_RECEIPTS_TABLE}_created_at ON ${REQUEST_RECEIPTS_TABLE} (created_at);
|
|
282
300
|
`,
|
|
283
301
|
}),
|
|
284
302
|
])
|
package/src/orm-types.ts
CHANGED
|
@@ -48,9 +48,11 @@ type AppActionsOf<P, T, F extends 'appRead' | 'appCount'> = PolicyOf<P, T> exten
|
|
|
48
48
|
{ readonly actions: { readonly [K in F]: readonly (infer A extends string)[] } } ? A
|
|
49
49
|
: never
|
|
50
50
|
|
|
51
|
-
/** Each column that declares its own read
|
|
51
|
+
/** Each column that declares its own read actions, by column: the union of the actions it lists. */
|
|
52
52
|
type ColumnReadsOf<P, T> = PolicyOf<P, T> extends { readonly columns: infer C } ? {
|
|
53
|
-
readonly [K in keyof C as C[K] extends { readonly read: string } ? K : never]: C[K] extends
|
|
53
|
+
readonly [K in keyof C as C[K] extends { readonly read: string | readonly string[] } ? K : never]: C[K] extends
|
|
54
|
+
{ readonly read: infer A extends string | readonly string[] } ? A extends readonly (infer L extends string)[] ? L : A
|
|
55
|
+
: never
|
|
54
56
|
}
|
|
55
57
|
: Record<never, never>
|
|
56
58
|
|
|
@@ -235,10 +237,13 @@ type ListedIn<Ix extends AnyDbIndex, D> = Lookup<Ix['listed'], D>
|
|
|
235
237
|
type WithinIn<Ix extends AnyDbIndex, D> = Lookup<Ix['within'], D>
|
|
236
238
|
|
|
237
239
|
/**
|
|
238
|
-
* The columns a capability of action `A` cannot read: each
|
|
239
|
-
*
|
|
240
|
+
* The columns a capability of action `A` cannot read: each whose own read actions do not include `A`. A union `A` (a
|
|
241
|
+
* widened capability type) holds no single action provably, so it reads none of them.
|
|
240
242
|
*/
|
|
241
|
-
type HiddenFor<Reads, A> = { [C in keyof Reads]:
|
|
243
|
+
type HiddenFor<Reads, A> = { [C in keyof Reads]: IsSingle<A> extends true ? ([A] extends [Reads[C]] ? never : C) : C }[keyof Reads]
|
|
244
|
+
|
|
245
|
+
/** Whether `A` is one literal, not a union of several. */
|
|
246
|
+
type IsSingle<A, All = A> = [A] extends [never] ? false : A extends unknown ? ([All] extends [A] ? true : false) : never
|
|
242
247
|
|
|
243
248
|
/**
|
|
244
249
|
* A row as a capability of action `A` reads, filters and sorts it: a column that declares another read action is
|
|
@@ -392,6 +397,12 @@ export interface ListOptions<Row> {
|
|
|
392
397
|
readonly after?: string
|
|
393
398
|
}
|
|
394
399
|
|
|
400
|
+
/** What `all()` takes: a list's filter and order, and no page; it answers every row or refuses past `MAX_ALL_ROWS`. */
|
|
401
|
+
export interface AllOptions<Row> {
|
|
402
|
+
readonly where?: readonly ConditionTuple<Row>[]
|
|
403
|
+
readonly orderBy?: readonly OrderTerm<Row>[]
|
|
404
|
+
}
|
|
405
|
+
|
|
395
406
|
export interface CountOptions<Row> {
|
|
396
407
|
readonly where?: readonly ConditionTuple<Row>[]
|
|
397
408
|
}
|
|
@@ -436,6 +447,7 @@ export interface UnownedTarget<D extends string> {
|
|
|
436
447
|
/** A child table of the target: its rows are addressed by their own key under the target; a view's are only read. */
|
|
437
448
|
export interface ChildRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
|
|
438
449
|
readonly list: Read<T, E, A, (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>>
|
|
450
|
+
readonly all: Read<T, E, A, (options?: AllOptions<RowFor<E, A>>) => Promise<RowFor<E, A>[]>>
|
|
439
451
|
readonly get: Read<T, E, A, (key: string) => Promise<RowFor<E, A>>>
|
|
440
452
|
readonly insert: E['view'] extends true ? Unavailable<`${T} is a view, which no site writes: write the tables it reads`>
|
|
441
453
|
: Write<T, E, 'insert', A, K, InsertMethod<E>>
|
|
@@ -453,6 +465,7 @@ export interface ChildRows<T extends string, E extends IndexedTable, A extends s
|
|
|
453
465
|
/** A dimension table the target contains: listed in it, and inserted into it by a mutation of its insert action. */
|
|
454
466
|
export interface ContainedRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
|
|
455
467
|
readonly list: Read<T, E, A, (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>>
|
|
468
|
+
readonly all: Read<T, E, A, (options?: AllOptions<RowFor<E, A>>) => Promise<RowFor<E, A>[]>>
|
|
456
469
|
readonly insert: Write<T, E, 'insert', A, K, InsertMethod<E>>
|
|
457
470
|
}
|
|
458
471
|
|
|
@@ -474,6 +487,7 @@ export interface WithinRows<T extends string, E extends IndexedTable, A extends
|
|
|
474
487
|
readonly claim: Claim<T, E, A, K>
|
|
475
488
|
readonly list: A extends E['actions']['read'] ? (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>
|
|
476
489
|
: Unavailable<ListRefusal<T, E, A>>
|
|
490
|
+
readonly all: A extends E['actions']['read'] ? (options?: AllOptions<RowFor<E, A>>) => Promise<RowFor<E, A>[]> : Unavailable<ListRefusal<T, E, A>>
|
|
477
491
|
readonly count: A extends E['actions']['read'] ? (options?: CountOptions<RowFor<E, A>>) => Promise<number> : Unavailable<ListRefusal<T, E, A>>
|
|
478
492
|
readonly aggregate: A extends E['actions']['read']
|
|
479
493
|
? <const S extends AggregateSpec<RowFor<E, A>>>(spec: S) => Promise<AggregateRow<RowFor<E, A>, S>[]>
|
|
@@ -519,6 +533,7 @@ type PublicRead<T extends string, E extends IndexedTable, A extends string, Memb
|
|
|
519
533
|
*/
|
|
520
534
|
export interface ApplicationRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
|
|
521
535
|
readonly list: AppList<T, E, A, (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>>
|
|
536
|
+
readonly all: AppList<T, E, A, (options?: AllOptions<RowFor<E, A>>) => Promise<RowFor<E, A>[]>>
|
|
522
537
|
readonly count: AppList<T, E, A, (options?: CountOptions<RowFor<E, A>>) => Promise<number>>
|
|
523
538
|
readonly aggregate: AppList<T, E, A, <const S extends AggregateSpec<RowFor<E, A>>>(spec: S) => Promise<AggregateRow<RowFor<E, A>, S>[]>>
|
|
524
539
|
/** One row by its key; a column that declares another read action than `A` is hidden. */
|
|
@@ -554,7 +569,7 @@ type PublicRefusal<Op extends string> = Unavailable<`a public procedure does not
|
|
|
554
569
|
|
|
555
570
|
/** A public procedure updates and removes only a versioned row, by an id a verified signed token of its request carries. */
|
|
556
571
|
type PublicVersioned<T extends string, E extends IndexedTable, Member> = E['versioned'] extends true ? Member
|
|
557
|
-
: Unavailable<`a public procedure updates and removes only a versioned row:
|
|
572
|
+
: Unavailable<`a public procedure updates and removes only a versioned row: give ${T} a version column`>
|
|
558
573
|
|
|
559
574
|
/**
|
|
560
575
|
* An application table of a public procedure of action `A`, in a site of kind `K`: read under its read action only,
|
|
@@ -563,6 +578,7 @@ type PublicVersioned<T extends string, E extends IndexedTable, Member> = E['vers
|
|
|
563
578
|
*/
|
|
564
579
|
export interface PublicApplicationRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
|
|
565
580
|
readonly list: PublicRead<T, E, A, (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>>
|
|
581
|
+
readonly all: Unavailable<'a public procedure pages its reads: read the table with list'>
|
|
566
582
|
readonly count: PublicRead<T, E, A, (options?: CountOptions<RowFor<E, A>>) => Promise<number>>
|
|
567
583
|
readonly aggregate: PublicRead<T, E, A, <const S extends AggregateSpec<RowFor<E, A>>>(spec: S) => Promise<AggregateRow<RowFor<E, A>, S>[]>>
|
|
568
584
|
readonly get: PublicRead<T, E, A, (key: string) => Promise<RowFor<E, A>>>
|
|
@@ -604,6 +620,10 @@ export interface AppRecordRows<T extends string, E extends IndexedTable, A exten
|
|
|
604
620
|
: Unavailable<
|
|
605
621
|
`a list of ${T} needs its read action '${E['actions']['read'] & string}' or an action of its appRead, and this endpoint holds '${A}'`
|
|
606
622
|
>
|
|
623
|
+
readonly all: AnyAllowed<A, E['actions']['read'] | E['appRead']> extends true ? (options?: AllOptions<RowFor<E, A>>) => Promise<RowFor<E, A>[]>
|
|
624
|
+
: Unavailable<
|
|
625
|
+
`a list of ${T} needs its read action '${E['actions']['read'] & string}' or an action of its appRead, and this endpoint holds '${A}'`
|
|
626
|
+
>
|
|
607
627
|
readonly count: AnyAllowed<A, E['actions']['read'] | E['appRead'] | E['appCount']> extends true
|
|
608
628
|
? (options?: CountOptions<RowFor<E, A>>) => Promise<number>
|
|
609
629
|
: Unavailable<
|
|
@@ -636,6 +656,7 @@ type TableRow<E extends IndexedTable> = RowFor<E, E['actions']['read']>
|
|
|
636
656
|
*/
|
|
637
657
|
export interface ScopedApplicationRows<E extends IndexedTable> {
|
|
638
658
|
list(options?: ListOptions<TableRow<E>>): Promise<ListPage<TableRow<E>>>
|
|
659
|
+
all(options?: AllOptions<TableRow<E>>): Promise<TableRow<E>[]>
|
|
639
660
|
count(options?: CountOptions<TableRow<E>>): Promise<number>
|
|
640
661
|
aggregate<const S extends AggregateSpec<TableRow<E>>>(spec: S): Promise<AggregateRow<TableRow<E>, S>[]>
|
|
641
662
|
/** One row by its key. */
|
|
@@ -665,6 +686,7 @@ export type ScopedApplicationTables<Ix extends AnyDbIndex, A extends string> = {
|
|
|
665
686
|
/** A reference table: every endpoint reads it, through its read action, and none writes it. */
|
|
666
687
|
export interface ReferenceRows<E extends IndexedTable> {
|
|
667
688
|
list(options?: ListOptions<TableRow<E>>): Promise<ListPage<TableRow<E>>>
|
|
689
|
+
all(options?: AllOptions<TableRow<E>>): Promise<TableRow<E>[]>
|
|
668
690
|
count(options?: CountOptions<TableRow<E>>): Promise<number>
|
|
669
691
|
}
|
|
670
692
|
|
|
@@ -700,6 +722,7 @@ type KeylessMessage<T extends string> = `${T} is a reference table, which has no
|
|
|
700
722
|
/** A table a site declared `unscoped`: its scope predicate is `true`, and `'read'` writes nothing. */
|
|
701
723
|
export interface UnscopedRows<T extends string, E extends IndexedTable, M extends UnscopedMode, K extends DataKind> {
|
|
702
724
|
list(options?: ListOptions<TableRow<E>>): Promise<ListPage<TableRow<E>>>
|
|
725
|
+
all(options?: AllOptions<TableRow<E>>): Promise<TableRow<E>[]>
|
|
703
726
|
count(options?: CountOptions<TableRow<E>>): Promise<number>
|
|
704
727
|
aggregate<const S extends AggregateSpec<TableRow<E>>>(spec: S): Promise<AggregateRow<TableRow<E>, S>[]>
|
|
705
728
|
readonly get: E['mode'] extends 'reference' ? Unavailable<KeylessMessage<T>> : (key: string) => Promise<TableRow<E>>
|
package/src/orm.ts
CHANGED
|
@@ -108,6 +108,12 @@ import { answerRows, timestampInput } from './temporal'
|
|
|
108
108
|
/** A list page holds this many rows unless the caller asks for fewer, and never more than `MAX_LIST_LIMIT`. */
|
|
109
109
|
export const DEFAULT_LIST_LIMIT = 50
|
|
110
110
|
export const MAX_LIST_LIMIT = 500
|
|
111
|
+
/**
|
|
112
|
+
* The most rows `all()` answers; past it, it refuses instead of truncating. It bounds rows, not bytes: 10 000 rows of
|
|
113
|
+
* ~1 KB, held two or three times over (driver, answer, response), is tens of MB of the one 128 MB Durable Object
|
|
114
|
+
* isolate every concurrent invocation of the environment shares. Wide rows or a growing set page with `list`.
|
|
115
|
+
*/
|
|
116
|
+
export const MAX_ALL_ROWS = 10_000
|
|
111
117
|
|
|
112
118
|
/** The framework's own tables; no declaration reaches one. */
|
|
113
119
|
const FRAMEWORK_PREFIX = 'fabrika_'
|
|
@@ -162,6 +168,11 @@ export interface DataHandles {
|
|
|
162
168
|
readonly reference: object
|
|
163
169
|
/** `ctx.db.unscoped`, or `undefined` for a site that declares none. */
|
|
164
170
|
readonly unscoped: object | undefined
|
|
171
|
+
/**
|
|
172
|
+
* At most two rows of `table` whose `column` holds `value`, as an unscoped read answers them: the framework's read of
|
|
173
|
+
* the caller's own row (`ctx.caller` of `@fabrika/app`). No handle of an application reaches it.
|
|
174
|
+
*/
|
|
175
|
+
rowsNaming(table: string, column: string, value: string): Promise<readonly Readonly<Record<string, unknown>>[]>
|
|
165
176
|
}
|
|
166
177
|
|
|
167
178
|
/** The data side of one bound layer, which only the framework reaches (`@fabrika/db/framework`). */
|
|
@@ -293,6 +304,24 @@ function handlesOf(parts: DataLayerParts, site: DataSite): DataHandles {
|
|
|
293
304
|
},
|
|
294
305
|
reference: referenceTables(parts, site, run),
|
|
295
306
|
unscoped,
|
|
307
|
+
rowsNaming: async (table: string, column: string, value: string) => {
|
|
308
|
+
site.check()
|
|
309
|
+
const model = table.startsWith(FRAMEWORK_PREFIX) ? undefined : modelOf(parts.models, table)
|
|
310
|
+
if (model === undefined) {
|
|
311
|
+
throw new QueryRefusedError('unknown_table', `the caller's table ${table} is not a table of src/db/tables.ts`)
|
|
312
|
+
}
|
|
313
|
+
const keyed = model.scope.mode === 'dimension' || model.scope.mode === 'child' || model.scope.mode === 'app'
|
|
314
|
+
const page = await listRows(
|
|
315
|
+
parts.contextOf(table),
|
|
316
|
+
run,
|
|
317
|
+
everything(),
|
|
318
|
+
model.visible,
|
|
319
|
+
{ where: [[column, '=', value]], limit: 2 },
|
|
320
|
+
keyed ? [keyOf(model)] : model.visible,
|
|
321
|
+
soleReach(model.actions.read[0] ?? ''),
|
|
322
|
+
)
|
|
323
|
+
return page.items
|
|
324
|
+
},
|
|
296
325
|
})
|
|
297
326
|
}
|
|
298
327
|
|
|
@@ -411,6 +440,10 @@ function childRows(context: TableContext, site: DataSite, run: Runner, capabilit
|
|
|
411
440
|
const grant = namedRef(context, capability, parent)
|
|
412
441
|
return listRows(context, run, parentPredicate(context, grant), visibleFor(context.model, grant.action), options, [key], soleReach(grant.action))
|
|
413
442
|
}),
|
|
443
|
+
all: bound(site, async (options?: unknown) => {
|
|
444
|
+
const grant = namedRef(context, capability, parent)
|
|
445
|
+
return allRows(context, run, parentPredicate(context, grant), visibleFor(context.model, grant.action), options, [key], soleReach(grant.action))
|
|
446
|
+
}),
|
|
414
447
|
get: bound(site, async (id: unknown) => {
|
|
415
448
|
const grant = namedRef(context, capability, parent)
|
|
416
449
|
if (typeof id !== 'string' || id.length === 0) {
|
|
@@ -445,6 +478,11 @@ function containedRows(context: TableContext, site: DataSite, run: Runner, capab
|
|
|
445
478
|
const scope = refScope(context, context.model, context.model.chain, grant, new Date())
|
|
446
479
|
return listRows(context, run, scope, visibleFor(context.model, grant.action), options, [keyOf(context.model)], soleReach(grant.action))
|
|
447
480
|
}),
|
|
481
|
+
all: bound(site, async (options?: unknown) => {
|
|
482
|
+
const grant = namedRef(context, capability, dimension)
|
|
483
|
+
const scope = refScope(context, context.model, context.model.chain, grant, new Date())
|
|
484
|
+
return allRows(context, run, scope, visibleFor(context.model, grant.action), options, [keyOf(context.model)], soleReach(grant.action))
|
|
485
|
+
}),
|
|
448
486
|
insert: writing(
|
|
449
487
|
site,
|
|
450
488
|
'insert',
|
|
@@ -502,6 +540,10 @@ function listedRows(
|
|
|
502
540
|
const predicate = scope()
|
|
503
541
|
return listRows(context, run, predicate, visible(), options, key, soleReach(heldAction(context, capability)))
|
|
504
542
|
}),
|
|
543
|
+
all: bound(site, async (options?: unknown) => {
|
|
544
|
+
const predicate = scope()
|
|
545
|
+
return allRows(context, run, predicate, visible(), options, key, soleReach(heldAction(context, capability)))
|
|
546
|
+
}),
|
|
505
547
|
count: bound(site, async (options?: unknown) => {
|
|
506
548
|
const predicate = scope()
|
|
507
549
|
return countRows(context, run, predicate, visible(), options)
|
|
@@ -549,6 +591,10 @@ function appRecordRows(context: TableContext, site: DataSite, run: Runner, capab
|
|
|
549
591
|
const { predicate, reach } = appRecordRead(context, capability, 'rows')
|
|
550
592
|
return listRows(context, run, predicate, visibleThrough(model, reach), options, key, reach)
|
|
551
593
|
}),
|
|
594
|
+
all: bound(site, async (options?: unknown) => {
|
|
595
|
+
const { predicate, reach } = appRecordRead(context, capability, 'rows')
|
|
596
|
+
return allRows(context, run, predicate, visibleThrough(model, reach), options, key, reach)
|
|
597
|
+
}),
|
|
552
598
|
count: bound(site, async (options?: unknown) => {
|
|
553
599
|
const { predicate, reach } = appRecordRead(context, capability, 'counts')
|
|
554
600
|
return countRows(context, run, predicate, visibleThrough(model, reach), options)
|
|
@@ -581,6 +627,7 @@ function scopedApplicationRows(context: TableContext, site: DataSite, run: Runne
|
|
|
581
627
|
const scope = () => scopedApplicationRead(context, capability)
|
|
582
628
|
return freeze({
|
|
583
629
|
list: bound(site, async (options?: unknown) => listRows(context, run, scope(), visible, options, key, soleReach(heldAction(context, capability)))),
|
|
630
|
+
all: bound(site, async (options?: unknown) => allRows(context, run, scope(), visible, options, key, soleReach(heldAction(context, capability)))),
|
|
584
631
|
count: bound(site, async (options?: unknown) => countRows(context, run, scope(), visible, options)),
|
|
585
632
|
aggregate: bound(site, async <Row>(spec: AggregateSpec<Row>) => aggregateRows(context, run, scope(), context.shape, spec)),
|
|
586
633
|
get: bound(
|
|
@@ -665,6 +712,10 @@ function applicationRows(
|
|
|
665
712
|
const { predicate, reach } = appListRead(context, capability)
|
|
666
713
|
return listRows(context, run, predicate, visibleThrough(model, reach), options, key, reach)
|
|
667
714
|
}),
|
|
715
|
+
all: bound(site, async (options?: unknown) => {
|
|
716
|
+
const { predicate, reach } = appListRead(context, capability)
|
|
717
|
+
return allRows(context, run, predicate, visibleThrough(model, reach), options, key, reach)
|
|
718
|
+
}),
|
|
668
719
|
count: bound(site, async (options?: unknown) => {
|
|
669
720
|
const { predicate, reach } = appListRead(context, capability)
|
|
670
721
|
return countRows(context, run, predicate, visibleThrough(model, reach), options)
|
|
@@ -723,6 +774,11 @@ function publicApplicationRows(
|
|
|
723
774
|
}
|
|
724
775
|
return freeze({
|
|
725
776
|
list: rows['list'],
|
|
777
|
+
all: bound(site, async () => {
|
|
778
|
+
// The table's own read refusal first, as list answers it, so the message never points to a list it refuses too.
|
|
779
|
+
appListPredicate(context, capability)
|
|
780
|
+
throw new QueryRefusedError('public_reach', 'a public procedure pages its reads: read the table with list')
|
|
781
|
+
}),
|
|
726
782
|
count: rows['count'],
|
|
727
783
|
aggregate: rows['aggregate'],
|
|
728
784
|
get: bound(site, async (id: unknown) => {
|
|
@@ -1020,6 +1076,7 @@ function referenceTables(parts: DataLayerParts, site: DataSite, run: Runner): ob
|
|
|
1020
1076
|
model.name,
|
|
1021
1077
|
freeze({
|
|
1022
1078
|
list: bound(site, async (options?: unknown) => listRows(context, run, everything(), model.visible, options, model.visible)),
|
|
1079
|
+
all: bound(site, async (options?: unknown) => allRows(context, run, everything(), model.visible, options, model.visible)),
|
|
1023
1080
|
count: bound(site, async (options?: unknown) => countRows(context, run, everything(), model.visible, options)),
|
|
1024
1081
|
}),
|
|
1025
1082
|
)
|
|
@@ -1091,6 +1148,10 @@ function unscopedRows(context: TableContext, site: DataSite, run: Runner, mode:
|
|
|
1091
1148
|
site,
|
|
1092
1149
|
async (options?: unknown) => listRows(context, run, everything(), visible, options, keyed ? [keyOf(model)] : visible, soleReach(unscopedRead)),
|
|
1093
1150
|
),
|
|
1151
|
+
all: bound(
|
|
1152
|
+
site,
|
|
1153
|
+
async (options?: unknown) => allRows(context, run, everything(), visible, options, keyed ? [keyOf(model)] : visible, soleReach(unscopedRead)),
|
|
1154
|
+
),
|
|
1094
1155
|
count: bound(site, async (options?: unknown) => countRows(context, run, everything(), visible, options)),
|
|
1095
1156
|
aggregate: bound(site, async <Row>(spec: AggregateSpec<Row>) => aggregateRows(context, run, everything(), context.shape, spec)),
|
|
1096
1157
|
get: bound(site, async (id: unknown) => {
|
|
@@ -1373,6 +1434,45 @@ async function listRows(
|
|
|
1373
1434
|
}
|
|
1374
1435
|
}
|
|
1375
1436
|
|
|
1437
|
+
/**
|
|
1438
|
+
* Every row of a read in one statement, in the order asked with the tiebreak last, or a refusal when more than
|
|
1439
|
+
* `MAX_ALL_ROWS` match: it never answers a truncated set.
|
|
1440
|
+
*/
|
|
1441
|
+
async function allRows(
|
|
1442
|
+
context: TableContext,
|
|
1443
|
+
run: Runner,
|
|
1444
|
+
scope: OperationNode | undefined,
|
|
1445
|
+
visible: readonly string[],
|
|
1446
|
+
input: unknown,
|
|
1447
|
+
tiebreak: readonly string[],
|
|
1448
|
+
reach?: ReadReach,
|
|
1449
|
+
): Promise<Readonly<Record<string, unknown>>[]> {
|
|
1450
|
+
const table = context.model.name
|
|
1451
|
+
const options = readOptions(input, ['where', 'orderBy'], 'all')
|
|
1452
|
+
const shape = actionShape(context.model, visible)
|
|
1453
|
+
const conditions = conditionsOf(context, shape, options['where'])
|
|
1454
|
+
const order = orderOf(shape, options['orderBy'], tiebreak)
|
|
1455
|
+
if (scope === undefined) {
|
|
1456
|
+
return []
|
|
1457
|
+
}
|
|
1458
|
+
const dialect = dialectOf(context)
|
|
1459
|
+
const node = selectNode(rowState(table, conditions, order, MAX_ALL_ROWS + 1), visible, scope, undefined, context.model.dates, dialect)
|
|
1460
|
+
const session = await run.read()
|
|
1461
|
+
const rows = await session.run<Readonly<Record<string, unknown>>>(node, {
|
|
1462
|
+
root: 'SelectQueryNode',
|
|
1463
|
+
table,
|
|
1464
|
+
joins: {},
|
|
1465
|
+
scope,
|
|
1466
|
+
subqueries: context.subqueries,
|
|
1467
|
+
through: context.through,
|
|
1468
|
+
})
|
|
1469
|
+
if (rows.length > MAX_ALL_ROWS) {
|
|
1470
|
+
throw new QueryRefusedError('too_many_rows', `all() of ${table} matches more than ${MAX_ALL_ROWS} rows: narrow it with where, or page it with list`)
|
|
1471
|
+
}
|
|
1472
|
+
const items = answerRows(dialect, table, rowColumnsOf(context.model), rows)
|
|
1473
|
+
return reach === undefined ? items : await answerFiles(context.model, run.files, session, items, reach)
|
|
1474
|
+
}
|
|
1475
|
+
|
|
1376
1476
|
async function countRows(
|
|
1377
1477
|
context: TableContext,
|
|
1378
1478
|
run: Runner,
|