@fabrika/db 0.9.0-dev.3 → 0.9.0-dev.30
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 +39 -4
- package/src/admissions.ts +78 -12
- package/src/application-rows.ts +108 -0
- package/src/authority.ts +560 -0
- package/src/builtins.ts +21 -0
- package/src/capability.ts +56 -2
- package/src/codegen-sqlite.ts +194 -0
- package/src/codegen.ts +230 -41
- package/src/condition-nodes.ts +119 -0
- package/src/conditions.ts +683 -0
- package/src/database.ts +227 -61
- package/src/description.ts +37 -8
- package/src/errors.ts +8 -5
- package/src/file-columns.ts +56 -11
- package/src/framework-tables.ts +28 -8
- package/src/framework.ts +15 -5
- package/src/guard.ts +373 -15
- package/src/index.ts +17 -1
- package/src/job-leases.ts +212 -55
- package/src/job-wakes.ts +450 -0
- package/src/json.ts +124 -0
- package/src/links.ts +8 -1
- package/src/migrations-sqlite.ts +181 -0
- package/src/migrations.ts +43 -0
- package/src/nodes.ts +84 -14
- package/src/orm-types.ts +198 -67
- package/src/orm.ts +383 -133
- package/src/policy.ts +1933 -122
- package/src/postgres.ts +26 -0
- package/src/registry.ts +55 -3
- package/src/request-receipts.ts +107 -0
- package/src/sql.ts +247 -476
- package/src/sqlite-bun.ts +149 -0
- package/src/sqlite-migrate.ts +242 -0
- package/src/sqlite.ts +269 -0
- package/src/store.ts +2292 -239
- package/src/stored-files.ts +188 -52
- package/src/tables.ts +171 -5
- package/src/temporal.ts +122 -9
- package/src/testing-shared.ts +1348 -0
- package/src/testing-sqlite.ts +685 -0
- package/src/testing.ts +310 -494
- package/src/vocabulary-tables.ts +159 -27
- package/types/src/application-rows.d.ts +58 -0
- package/types/src/authority.d.ts +147 -0
- package/types/src/builtins.d.ts +2 -0
- package/types/src/capability.d.ts +55 -2
- package/types/src/codegen-sqlite.d.ts +13 -0
- package/types/src/codegen.d.ts +79 -5
- package/types/src/condition-nodes.d.ts +9 -0
- package/types/src/conditions.d.ts +151 -0
- package/types/src/database.d.ts +81 -20
- package/types/src/description.d.ts +26 -8
- package/types/src/errors.d.ts +7 -5
- package/types/src/file-columns.d.ts +6 -4
- package/types/src/framework.d.ts +12 -5
- package/types/src/guard.d.ts +24 -0
- package/types/src/index.d.ts +5 -4
- package/types/src/job-leases.d.ts +34 -5
- package/types/src/job-wakes.d.ts +101 -0
- package/types/src/json.d.ts +18 -0
- package/types/src/migrations-sqlite.d.ts +4 -0
- package/types/src/migrations.d.ts +2 -0
- package/types/src/nodes.d.ts +17 -2
- package/types/src/orm-types.d.ts +155 -63
- package/types/src/orm.d.ts +21 -0
- package/types/src/policy.d.ts +344 -16
- package/types/src/postgres.d.ts +12 -0
- package/types/src/registry.d.ts +40 -3
- package/types/src/request-receipts.d.ts +22 -0
- package/types/src/sql.d.ts +29 -21
- 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 +143 -34
- package/types/src/stored-files.d.ts +6 -2
- package/types/src/tables.d.ts +18 -2
- package/types/src/temporal.d.ts +24 -0
- package/types/src/testing-shared.d.ts +290 -0
- package/types/src/testing-sqlite.d.ts +37 -0
- package/types/src/testing.d.ts +2 -93
- package/types/src/vocabulary-tables.d.ts +169 -22
package/src/postgres.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { SQL } from 'bun'
|
|
2
|
+
import { PostgresJSDialect } from 'kysely-postgres-js'
|
|
3
|
+
import { Database } from './database'
|
|
4
|
+
import { QueryRefusedError } from './errors'
|
|
5
|
+
import { isPlainIdentifier } from './identifier'
|
|
6
|
+
|
|
7
|
+
export interface PostgresOptions {
|
|
8
|
+
readonly url: string
|
|
9
|
+
/** The application's schema (`app_<id>`), set as the connection's `search_path`. */
|
|
10
|
+
readonly schema: string
|
|
11
|
+
readonly max?: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Over `Bun.SQL` with `prepare: false`: Bun otherwise keeps one prepared statement per distinct SQL text per
|
|
16
|
+
* connection, and `in` lists of different lengths make that grow without bound.
|
|
17
|
+
*/
|
|
18
|
+
export function openPostgres(options: PostgresOptions): Database {
|
|
19
|
+
if (!isPlainIdentifier(options.schema)) {
|
|
20
|
+
throw new QueryRefusedError('invalid_name', 'the schema must match ^[a-z_][a-z0-9_]*$')
|
|
21
|
+
}
|
|
22
|
+
const pool = new SQL({ url: options.url, adapter: 'postgres', prepare: false, max: options.max ?? 10, connection: { search_path: options.schema } })
|
|
23
|
+
return Database.fromDialect(new PostgresJSDialect({ postgres: pool }), { schema: options.schema })
|
|
24
|
+
}
|
|
25
|
+
// Application modules load after this one: a replaced opener would receive the database URL.
|
|
26
|
+
Object.freeze(openPostgres)
|
package/src/registry.ts
CHANGED
|
@@ -3,10 +3,13 @@
|
|
|
3
3
|
// files may import.
|
|
4
4
|
|
|
5
5
|
import type { Admissions } from './admissions'
|
|
6
|
+
import type { ContainerGrant, RelationHold } from './capability'
|
|
6
7
|
import type { Session } from './database'
|
|
7
8
|
import type { WebhookTables } from './framework-tables'
|
|
8
9
|
import type { JobLeases } from './job-leases'
|
|
10
|
+
import type { JobWakes } from './job-wakes'
|
|
9
11
|
import type { DataAccess } from './orm'
|
|
12
|
+
import type { RequestReceipts } from './request-receipts'
|
|
10
13
|
import type { DataLayerRuntime, TableContext, TableProbe } from './store'
|
|
11
14
|
import type { StoredFiles } from './stored-files'
|
|
12
15
|
|
|
@@ -16,21 +19,70 @@ import type { StoredFiles } from './stored-files'
|
|
|
16
19
|
*/
|
|
17
20
|
export type Locator = (dimension: string, id: string, using?: Session) => Promise<ReadonlyMap<string, string> | null>
|
|
18
21
|
|
|
22
|
+
/** Whether an active row of `relation`'s source names `principal` at `node`: what a Ref held through it is minted on. */
|
|
23
|
+
export type RelationCheck = (relation: string, node: string, principal: string) => Promise<boolean>
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Whether a row `relation` reaches for `principal` reveals `node` of `dimension` now: what a Ref held through a reveal
|
|
27
|
+
* is minted on.
|
|
28
|
+
*/
|
|
29
|
+
export type RevealCheck = (relation: string, dimension: string, node: string, principal: string, action: string) => Promise<boolean>
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Of `relations` and `paths`, the caller's relations and grants of `action` that reach the object `id` of `dimension`
|
|
33
|
+
* through a secondary path that holds the action for a Ref (`writeThrough`), or through a relation row or reveal: what a
|
|
34
|
+
* Ref held through a path is minted on, and carries.
|
|
35
|
+
*/
|
|
36
|
+
export type PathCheck = (
|
|
37
|
+
dimension: string,
|
|
38
|
+
id: string,
|
|
39
|
+
action: string,
|
|
40
|
+
principal: string,
|
|
41
|
+
relations: readonly RelationHold[],
|
|
42
|
+
paths: readonly ContainerGrant[],
|
|
43
|
+
) => Promise<{ readonly relations: readonly RelationHold[]; readonly paths: readonly ContainerGrant[] }>
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Of `values`, the caller's grants of `action` at nodes of `revealing`, those whose row reveals `node` of `dimension` to
|
|
47
|
+
* its members now: what a Ref held through member reveals is minted on, and carries.
|
|
48
|
+
*/
|
|
49
|
+
export type MemberRevealCheck = (
|
|
50
|
+
revealing: string,
|
|
51
|
+
values: readonly string[],
|
|
52
|
+
dimension: string,
|
|
53
|
+
node: string,
|
|
54
|
+
action: string,
|
|
55
|
+
) => Promise<readonly string[]>
|
|
56
|
+
|
|
19
57
|
/** What the framework writes itself: its own `fabrika_*` tables. Usable once bound. */
|
|
20
58
|
export interface FrameworkTables {
|
|
21
59
|
readonly webhooks: WebhookTables
|
|
22
60
|
readonly jobs: JobLeases
|
|
61
|
+
/** A job's durable pending work (`fabrika_wakes`), which follows its lease, and the scheduler's next wake over both. */
|
|
62
|
+
readonly wakes: JobWakes
|
|
23
63
|
/** A procedure's admission buckets, the challenge tokens it accepted and its byte budget. */
|
|
24
64
|
readonly admissions: Admissions
|
|
25
65
|
/** Every stored file (`fabrika_files`): staged before its object, claimed at its first reference, swept unreferenced. */
|
|
26
66
|
readonly files: StoredFiles
|
|
67
|
+
/** The answers of keyed mutations, read and written in the invocation's transaction (ADR-0078). */
|
|
68
|
+
readonly receipts: RequestReceipts
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** A connection of the layer and its locator. Only its holder can `release` it, which lets the layer be bound again. */
|
|
72
|
+
export interface ProvisionalBinding {
|
|
73
|
+
readonly locate: Locator
|
|
74
|
+
readonly relates: RelationCheck
|
|
75
|
+
readonly reveals: RevealCheck
|
|
76
|
+
readonly holdsThrough: PathCheck
|
|
77
|
+
readonly revealsToMembers: MemberRevealCheck
|
|
78
|
+
release(): void
|
|
27
79
|
}
|
|
28
80
|
|
|
29
81
|
export interface FrameworkSide {
|
|
30
|
-
/** Connects the layer once and answers its
|
|
31
|
-
bind(runtime: DataLayerRuntime):
|
|
82
|
+
/** Connects the layer once and answers its binding; a call while one is connected throws. */
|
|
83
|
+
bind(runtime: DataLayerRuntime): ProvisionalBinding
|
|
32
84
|
readonly framework: FrameworkTables
|
|
33
|
-
/** The untyped handle over one table that `@fabrika/db/testing` hands the isolation
|
|
85
|
+
/** The untyped handle over one table that `@fabrika/db/testing` hands the policy isolation kit. */
|
|
34
86
|
probe(table: string): TableProbe
|
|
35
87
|
/** One table's context, for the framework's own store builders (`defineStore`, `@fabrika/db/testing`). */
|
|
36
88
|
tableContext(table: string, mode: 'dimension' | 'child'): TableContext
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// The framework's side of `fabrika_request_receipts` (`@fabrika/db/migrations`, ADR-0078): the answer of a mutation
|
|
2
|
+
// whose request carried a key, kept so a repeat of that request is answered without running it again. Keyed by the
|
|
3
|
+
// principal and the key; only `@fabrika/app`'s dispatcher reaches it, through `frameworkTablesOf`, and always in the
|
|
4
|
+
// invocation's own transaction, so a receipt commits with the write it answers. Each write prunes a bounded batch of
|
|
5
|
+
// receipts past the retention, so the table holds about one retention of keyed writes.
|
|
6
|
+
|
|
7
|
+
import { runFixedStatement, type Session } from './database'
|
|
8
|
+
import { QueryRefusedError } from './errors'
|
|
9
|
+
import type { DialectTexts } from './job-leases'
|
|
10
|
+
import { REQUEST_RECEIPTS_TABLE } from './migrations'
|
|
11
|
+
|
|
12
|
+
/** What a receipt records of the request it answers: its operation, the digest of its input, and the output's JSON. */
|
|
13
|
+
export interface RequestReceipt {
|
|
14
|
+
readonly operation: string
|
|
15
|
+
readonly identity: string
|
|
16
|
+
readonly output: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface RequestReceipts {
|
|
20
|
+
/**
|
|
21
|
+
* The receipt of `principal`'s `key`, read in `session`; `null` when there is none. On Postgres it first takes the
|
|
22
|
+
* key's transaction lock, so a concurrent copy of the request waits for this transaction and then reads its receipt
|
|
23
|
+
* instead of running too; SQLite's one writer already serializes them.
|
|
24
|
+
*/
|
|
25
|
+
find(session: Session, principal: string, key: string): Promise<RequestReceipt | null>
|
|
26
|
+
/**
|
|
27
|
+
* Records `receipt` under `principal`'s `key` in `session`, created at `now` (epoch ms), after pruning a batch of
|
|
28
|
+
* receipts created before `retainedSince`. `false` when the key already has a receipt: another copy of the request
|
|
29
|
+
* recorded it meanwhile, and this one's transaction must not commit.
|
|
30
|
+
*/
|
|
31
|
+
record(session: Session, principal: string, key: string, receipt: RequestReceipt, now: number, retainedSince: number): Promise<boolean>
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const R = REQUEST_RECEIPTS_TABLE
|
|
35
|
+
|
|
36
|
+
/** Receipts one write prunes at most; every write prunes, so expired receipts never outgrow the live ones. */
|
|
37
|
+
const PRUNE_BATCH = 32
|
|
38
|
+
|
|
39
|
+
// Held until the transaction ends; two keys whose hashes collide only wait for each other.
|
|
40
|
+
const SQL_LOCK = `select 1 as locked from pg_advisory_xact_lock(hashtextextended($1 || chr(10) || $2, 0))`
|
|
41
|
+
|
|
42
|
+
const SQL_FIND: DialectTexts = {
|
|
43
|
+
postgres: `select operation, identity, output from ${R} where principal = $1 and request_key = $2`,
|
|
44
|
+
sqlite: `select operation, identity, output from ${R} where principal = ?1 and request_key = ?2`,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// `skip locked`: a prune never waits on a receipt another transaction holds. It never touches the key it writes.
|
|
48
|
+
const SQL_RECORD_POSTGRES = `with pruned as (
|
|
49
|
+
delete from ${R} where (principal, request_key) in (
|
|
50
|
+
select principal, request_key from ${R}
|
|
51
|
+
where created_at < $7::bigint and not (principal = $1 and request_key = $2)
|
|
52
|
+
limit ${PRUNE_BATCH} for update skip locked
|
|
53
|
+
)
|
|
54
|
+
)
|
|
55
|
+
insert into ${R} (principal, request_key, operation, identity, output, created_at) values ($1, $2, $3, $4, $5, $6::bigint)
|
|
56
|
+
on conflict (principal, request_key) do nothing
|
|
57
|
+
returning request_key`
|
|
58
|
+
|
|
59
|
+
// SQLite has no data-modifying CTE, so the prune is a statement of its own before the insert, in the same transaction.
|
|
60
|
+
const SQLITE_PRUNE = `delete from ${R} where (principal, request_key) in (
|
|
61
|
+
select principal, request_key from ${R}
|
|
62
|
+
where created_at < ?3 and not (principal = ?1 and request_key = ?2)
|
|
63
|
+
limit ${PRUNE_BATCH}
|
|
64
|
+
)`
|
|
65
|
+
|
|
66
|
+
const SQLITE_RECORD = `insert into ${R} (principal, request_key, operation, identity, output, created_at) values (?1, ?2, ?3, ?4, ?5, ?6)
|
|
67
|
+
on conflict (principal, request_key) do nothing
|
|
68
|
+
returning request_key`
|
|
69
|
+
|
|
70
|
+
function text(row: Readonly<Record<string, unknown>>, column: string): string {
|
|
71
|
+
const value = row[column]
|
|
72
|
+
if (typeof value !== 'string') {
|
|
73
|
+
throw new QueryRefusedError('unexpected_row', `${R}.${column} came back as ${typeof value}`)
|
|
74
|
+
}
|
|
75
|
+
return value
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function isRow(value: unknown): value is Readonly<Record<string, unknown>> {
|
|
79
|
+
return typeof value === 'object' && value !== null
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function requestReceipts(): RequestReceipts {
|
|
83
|
+
return Object.freeze({
|
|
84
|
+
async find(session: Session, principal: string, key: string): Promise<RequestReceipt | null> {
|
|
85
|
+
if (session.dialect === 'postgres') {
|
|
86
|
+
await runFixedStatement(session, SQL_LOCK, [principal, key])
|
|
87
|
+
}
|
|
88
|
+
const rows = await runFixedStatement(session, SQL_FIND[session.dialect], [principal, key])
|
|
89
|
+
const row = rows[0]
|
|
90
|
+
if (row === undefined) {
|
|
91
|
+
return null
|
|
92
|
+
}
|
|
93
|
+
if (!isRow(row)) {
|
|
94
|
+
throw new QueryRefusedError('unexpected_row', `${R} answered a row that is not an object`)
|
|
95
|
+
}
|
|
96
|
+
return { operation: text(row, 'operation'), identity: text(row, 'identity'), output: text(row, 'output') }
|
|
97
|
+
},
|
|
98
|
+
async record(session: Session, principal: string, key: string, receipt: RequestReceipt, now: number, retainedSince: number): Promise<boolean> {
|
|
99
|
+
const values = [principal, key, receipt.operation, receipt.identity, receipt.output, now]
|
|
100
|
+
if (session.dialect === 'sqlite') {
|
|
101
|
+
await runFixedStatement(session, SQLITE_PRUNE, [principal, key, retainedSince])
|
|
102
|
+
return (await runFixedStatement(session, SQLITE_RECORD, values)).length === 1
|
|
103
|
+
}
|
|
104
|
+
return (await runFixedStatement(session, SQL_RECORD_POSTGRES, [...values, retainedSince])).length === 1
|
|
105
|
+
},
|
|
106
|
+
})
|
|
107
|
+
}
|