@fabrika/db 0.9.0-dev.7 → 0.9.0-dev.71
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 +9 -5
- package/src/authority.ts +560 -0
- package/src/builtins.ts +21 -0
- package/src/capability.ts +51 -2
- package/src/codegen-sqlite.ts +69 -13
- package/src/codegen.ts +181 -42
- package/src/condition-nodes.ts +119 -0
- package/src/conditions.ts +683 -0
- package/src/description.ts +67 -9
- package/src/errors.ts +9 -5
- package/src/file-columns.ts +52 -10
- package/src/framework.ts +5 -2
- package/src/guard.ts +365 -15
- package/src/index.ts +8 -0
- package/src/job-leases.ts +19 -2
- package/src/job-wakes.ts +183 -33
- package/src/json.ts +187 -0
- package/src/list-cursor.ts +82 -0
- package/src/migrations-sqlite.ts +93 -0
- package/src/migrations.ts +99 -0
- package/src/nodes.ts +33 -14
- package/src/notifications.ts +152 -0
- package/src/orm-types.ts +216 -77
- package/src/orm.ts +417 -158
- package/src/policy.ts +2043 -131
- package/src/push-subscriptions.ts +287 -0
- package/src/registry.ts +50 -1
- package/src/request-receipts.ts +107 -0
- package/src/sql.ts +39 -11
- package/src/sqlite-migrate.ts +102 -1
- package/src/sqlite.ts +46 -1
- package/src/store.ts +1958 -167
- package/src/tables.ts +156 -6
- package/src/temporal.ts +39 -4
- package/src/testing-shared.ts +872 -148
- package/src/testing-sqlite.ts +215 -40
- package/src/testing.ts +151 -22
- package/src/vocabulary-tables.ts +172 -26
- package/types/src/authority.d.ts +147 -0
- package/types/src/builtins.d.ts +2 -0
- package/types/src/capability.d.ts +50 -2
- package/types/src/codegen-sqlite.d.ts +8 -1
- package/types/src/codegen.d.ts +54 -10
- package/types/src/condition-nodes.d.ts +9 -0
- package/types/src/conditions.d.ts +151 -0
- package/types/src/description.d.ts +40 -9
- package/types/src/errors.d.ts +8 -5
- package/types/src/file-columns.d.ts +6 -4
- package/types/src/framework.d.ts +5 -2
- package/types/src/guard.d.ts +24 -0
- package/types/src/index.d.ts +4 -3
- package/types/src/job-leases.d.ts +8 -1
- package/types/src/job-wakes.d.ts +28 -5
- package/types/src/json.d.ts +35 -0
- package/types/src/list-cursor.d.ts +13 -0
- package/types/src/migrations.d.ts +5 -0
- package/types/src/nodes.d.ts +5 -1
- package/types/src/notifications.d.ts +37 -0
- package/types/src/orm-types.d.ts +167 -75
- package/types/src/orm.d.ts +21 -0
- package/types/src/policy.d.ts +378 -27
- package/types/src/push-subscriptions.d.ts +49 -0
- package/types/src/registry.d.ts +36 -1
- package/types/src/request-receipts.d.ts +22 -0
- package/types/src/sql.d.ts +2 -2
- package/types/src/sqlite-migrate.d.ts +32 -0
- package/types/src/store.d.ts +118 -31
- package/types/src/tables.d.ts +8 -2
- package/types/src/temporal.d.ts +7 -1
- package/types/src/testing-shared.d.ts +125 -26
- package/types/src/testing-sqlite.d.ts +17 -2
- package/types/src/testing.d.ts +1 -1
- package/types/src/vocabulary-tables.d.ts +176 -22
package/src/tables.ts
CHANGED
|
@@ -1,17 +1,42 @@
|
|
|
1
1
|
// @fabrika/db/tables — the table policies of `src/db/tables.ts` as data. Browser- and Worker-safe: no Bun API and no
|
|
2
2
|
// driver, so `@fabrika/app/contract` and the static contract reader in `@fabrika/check` import it.
|
|
3
3
|
|
|
4
|
-
import type {
|
|
4
|
+
import type {
|
|
5
|
+
AccessArtifact,
|
|
6
|
+
ColumnPolicyArtifact,
|
|
7
|
+
ConditionArtifact,
|
|
8
|
+
ConfersArtifact,
|
|
9
|
+
MemberRevealsArtifact,
|
|
10
|
+
RevealsArtifact,
|
|
11
|
+
TableActionArtifact,
|
|
12
|
+
TablePolicyArtifact,
|
|
13
|
+
TableScope,
|
|
14
|
+
} from '@fabrika/auth-core'
|
|
5
15
|
import { type ApplicationRowsPlan, planApplicationRows } from './application-rows'
|
|
6
16
|
import { freeze, namesOf, ownEntry, ownKeys, setOwn } from './builtins'
|
|
17
|
+
import { conditionArtifact, type ConditionModel } from './conditions'
|
|
7
18
|
import { PolicyError } from './errors'
|
|
8
|
-
import {
|
|
19
|
+
import {
|
|
20
|
+
type AccessModel,
|
|
21
|
+
accessTablesOf,
|
|
22
|
+
type MemberRevealModel,
|
|
23
|
+
modelOf,
|
|
24
|
+
READ_ANY,
|
|
25
|
+
type RelationSource,
|
|
26
|
+
type RevealModel,
|
|
27
|
+
type ScopeModel,
|
|
28
|
+
type TableModel,
|
|
29
|
+
tableModels,
|
|
30
|
+
type Tables,
|
|
31
|
+
} from './policy'
|
|
9
32
|
|
|
10
33
|
export { applicationRowRefusalText } from './application-rows'
|
|
11
34
|
export type { ApplicationRowRefusal, ApplicationRowsPlan } from './application-rows'
|
|
12
35
|
export { PolicyError } from './errors'
|
|
13
36
|
export type { FileReference } from './file-references'
|
|
14
37
|
export type {
|
|
38
|
+
AllOptions,
|
|
39
|
+
AllQuery,
|
|
15
40
|
AnyDbIndex,
|
|
16
41
|
ApplicationRows,
|
|
17
42
|
ApplicationTables,
|
|
@@ -22,6 +47,7 @@ export type {
|
|
|
22
47
|
ClaimOptions,
|
|
23
48
|
ContainedRows,
|
|
24
49
|
CountOptions,
|
|
50
|
+
CountQuery,
|
|
25
51
|
DataKind,
|
|
26
52
|
DbIndex,
|
|
27
53
|
FragmentTable,
|
|
@@ -30,6 +56,7 @@ export type {
|
|
|
30
56
|
InsertMethod,
|
|
31
57
|
ListOptions,
|
|
32
58
|
ListPage,
|
|
59
|
+
ListQuery,
|
|
33
60
|
NoUnscoped,
|
|
34
61
|
OrderTerm,
|
|
35
62
|
OwnerRow,
|
|
@@ -85,6 +112,7 @@ export {
|
|
|
85
112
|
} from './policy'
|
|
86
113
|
export type {
|
|
87
114
|
ColumnTypes,
|
|
115
|
+
ColumnViews,
|
|
88
116
|
DefineVocabularyPolicies,
|
|
89
117
|
NormalizedPolicies,
|
|
90
118
|
TableVocabulary,
|
|
@@ -97,6 +125,7 @@ export type {
|
|
|
97
125
|
VocabularyDimensionScope,
|
|
98
126
|
VocabularyPolicyMap,
|
|
99
127
|
VocabularyTablePolicy,
|
|
128
|
+
VocabularyViewPolicy,
|
|
100
129
|
} from './vocabulary-tables'
|
|
101
130
|
|
|
102
131
|
/**
|
|
@@ -112,6 +141,15 @@ export function applicationRowsPlan<DB, P>(
|
|
|
112
141
|
return planApplicationRows(tableModels(tables), rows)
|
|
113
142
|
}
|
|
114
143
|
|
|
144
|
+
/**
|
|
145
|
+
* The tables whose rows hand out access, lowercased: a relation source or a path reveal, a member-revealing table, and
|
|
146
|
+
* the tables a principal path or a relation's condition reads. Only the data layer writes them; the migration classifier
|
|
147
|
+
* refuses database code that does.
|
|
148
|
+
*/
|
|
149
|
+
export function accessTables<DB, P>(tables: Tables<DB, P>): readonly string[] {
|
|
150
|
+
return accessTablesOf(tableModels(tables))
|
|
151
|
+
}
|
|
152
|
+
|
|
115
153
|
/** Every table's policy in the form the contract artifact carries, by table name. */
|
|
116
154
|
export type TablePolicies = Readonly<Record<string, TablePolicyArtifact>>
|
|
117
155
|
|
|
@@ -134,14 +172,15 @@ function artifactOf(model: TableModel): TablePolicyArtifact {
|
|
|
134
172
|
const declared = [...ownKeys(model.columnReads), ...ownKeys(model.columnWrites)]
|
|
135
173
|
for (let index = 0; index < declared.length; index++) {
|
|
136
174
|
const name = declared[index] ?? ''
|
|
137
|
-
|
|
138
|
-
const
|
|
139
|
-
|
|
175
|
+
// A single action stays its text, so a policy that names one keeps its digest.
|
|
176
|
+
const read = actionArtifact(ownEntry(model.columnReads, name) ?? [])
|
|
177
|
+
const write = actionArtifact(ownEntry(model.columnWrites, name) ?? [])
|
|
178
|
+
setOwn(columns, name, { ...(read === null ? {} : { read }), ...(write === null ? {} : { write }) })
|
|
140
179
|
}
|
|
141
180
|
return {
|
|
142
181
|
scope: scopeOf(model.scope),
|
|
143
182
|
actions: {
|
|
144
|
-
read: actionArtifact(model.actions.read) ?? noReadAction(model.name),
|
|
183
|
+
read: model.actions.readAny ? READ_ANY : actionArtifact(model.actions.read) ?? noReadAction(model.name),
|
|
145
184
|
insert: actionArtifact(model.actions.insert),
|
|
146
185
|
update: actionArtifact(model.actions.update),
|
|
147
186
|
delete: actionArtifact(model.actions.delete),
|
|
@@ -154,7 +193,118 @@ function artifactOf(model: TableModel): TablePolicyArtifact {
|
|
|
154
193
|
createdBy: model.createdBy ?? null,
|
|
155
194
|
columns,
|
|
156
195
|
...(ownKeys(model.files).length === 0 ? {} : { files: { ...model.files } }),
|
|
196
|
+
...(model.view ? { view: true } : {}),
|
|
197
|
+
...(model.confers.length === 0 ? {} : { confers: confersArtifact(model.confers) }),
|
|
198
|
+
...(ownKeys(model.when).length === 0 ? {} : { when: whenArtifact(model.when) }),
|
|
199
|
+
...(ownKeys(model.relationWhen).length === 0 ? {} : { relationWhen: relationWhenArtifact(model.relationWhen) }),
|
|
200
|
+
...(model.reveals.length === 0 ? {} : { reveals: revealsArtifact(model.reveals) }),
|
|
201
|
+
...(model.readThrough.length === 0 ? {} : { readThrough: readThroughArtifact(model) }),
|
|
202
|
+
...writeThroughArtifact(model),
|
|
203
|
+
...(model.memberReveals.length === 0 ? {} : { memberReveals: memberRevealsArtifact(model.memberReveals) }),
|
|
204
|
+
...(model.access.length === 0 ? {} : { access: accessArtifact(model.access) }),
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function accessArtifact(access: readonly AccessModel[]): Record<string, AccessArtifact> {
|
|
209
|
+
const artifact: Record<string, AccessArtifact> = {}
|
|
210
|
+
for (let index = 0; index < access.length; index++) {
|
|
211
|
+
const entry = access[index]
|
|
212
|
+
if (entry !== undefined) {
|
|
213
|
+
setOwn(artifact, entry.relation, { view: entry.view, row: entry.row, principal: entry.principal ?? null, member: entry.member ?? null })
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return artifact
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function memberRevealsArtifact(reveals: readonly MemberRevealModel[]): Record<string, MemberRevealsArtifact> {
|
|
220
|
+
const artifact: Record<string, MemberRevealsArtifact> = {}
|
|
221
|
+
for (let index = 0; index < reveals.length; index++) {
|
|
222
|
+
const reveal = reveals[index]
|
|
223
|
+
if (reveal === undefined) continue
|
|
224
|
+
setOwn(artifact, reveal.source, {
|
|
225
|
+
to: reveal.dimension,
|
|
226
|
+
actions: [...reveal.actions],
|
|
227
|
+
...(reveal.when === undefined ? {} : { when: conditionArtifact(reveal.when) }),
|
|
228
|
+
...(reveal.assign === undefined ? {} : { assign: { self: reveal.assign.self } }),
|
|
229
|
+
})
|
|
230
|
+
}
|
|
231
|
+
return artifact
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function readThroughArtifact(model: TableModel): Record<string, string> {
|
|
235
|
+
const artifact: Record<string, string> = {}
|
|
236
|
+
for (let index = 0; index < model.readThrough.length; index++) {
|
|
237
|
+
const path = model.readThrough[index]
|
|
238
|
+
if (path !== undefined) setOwn(artifact, path.dimension, path.via === undefined ? path.column : `${path.via.column}.${path.column}`)
|
|
239
|
+
}
|
|
240
|
+
return artifact
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function writeThroughArtifact(model: TableModel): { readonly writeThrough: Record<string, string[]> } | undefined {
|
|
244
|
+
const artifact: Record<string, string[]> = {}
|
|
245
|
+
let declared = false
|
|
246
|
+
for (let index = 0; index < model.readThrough.length; index++) {
|
|
247
|
+
const path = model.readThrough[index]
|
|
248
|
+
if (path === undefined || path.writes.length === 0) continue
|
|
249
|
+
setOwn(artifact, path.dimension, [...path.writes])
|
|
250
|
+
declared = true
|
|
251
|
+
}
|
|
252
|
+
return declared ? { writeThrough: artifact } : undefined
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function revealsArtifact(reveals: readonly RevealModel[]): Record<string, RevealsArtifact> {
|
|
256
|
+
const artifact: Record<string, RevealsArtifact> = {}
|
|
257
|
+
for (let index = 0; index < reveals.length; index++) {
|
|
258
|
+
const reveal = reveals[index]
|
|
259
|
+
if (reveal === undefined) continue
|
|
260
|
+
setOwn(artifact, reveal.relation, {
|
|
261
|
+
to: reveal.dimension,
|
|
262
|
+
actions: [...reveal.actions],
|
|
263
|
+
...(reveal.when === undefined ? {} : { when: conditionArtifact(reveal.when) }),
|
|
264
|
+
...(reveal.assign === undefined ? {} : { assign: { self: reveal.assign.self } }),
|
|
265
|
+
})
|
|
266
|
+
}
|
|
267
|
+
return artifact
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function confersArtifact(sources: readonly RelationSource[]): Record<string, ConfersArtifact> {
|
|
271
|
+
const confers: Record<string, ConfersArtifact> = {}
|
|
272
|
+
for (let index = 0; index < sources.length; index++) {
|
|
273
|
+
const source = sources[index]
|
|
274
|
+
if (source === undefined) continue
|
|
275
|
+
setOwn(confers, source.relation, {
|
|
276
|
+
principal: source.principalPath === undefined ? source.principal : `${source.principal}.${source.principalPath.column}`,
|
|
277
|
+
node: source.node,
|
|
278
|
+
expires: source.expires ?? null,
|
|
279
|
+
...(source.when === undefined ? {} : { when: conditionArtifact(source.when) }),
|
|
280
|
+
assign: source.assign === undefined ? null : { maxDays: source.assign.maxDays ?? null, self: source.assign.self },
|
|
281
|
+
})
|
|
282
|
+
}
|
|
283
|
+
return confers
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function whenArtifact(when: Readonly<Record<string, ConditionModel>>): Record<string, ConditionArtifact> {
|
|
287
|
+
const artifact: Record<string, ConditionArtifact> = {}
|
|
288
|
+
const actions = ownKeys(when)
|
|
289
|
+
for (let index = 0; index < actions.length; index++) {
|
|
290
|
+
const action = actions[index] ?? ''
|
|
291
|
+
const condition = ownEntry(when, action)
|
|
292
|
+
if (condition !== undefined) setOwn(artifact, action, conditionArtifact(condition))
|
|
293
|
+
}
|
|
294
|
+
return artifact
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function relationWhenArtifact(
|
|
298
|
+
relationWhen: Readonly<Record<string, Readonly<Record<string, ConditionModel>>>>,
|
|
299
|
+
): Record<string, Record<string, ConditionArtifact>> {
|
|
300
|
+
const artifact: Record<string, Record<string, ConditionArtifact>> = {}
|
|
301
|
+
const relations = ownKeys(relationWhen)
|
|
302
|
+
for (let index = 0; index < relations.length; index++) {
|
|
303
|
+
const relation = relations[index] ?? ''
|
|
304
|
+
const conditions = ownEntry(relationWhen, relation)
|
|
305
|
+
if (conditions !== undefined) setOwn(artifact, relation, whenArtifact(conditions))
|
|
157
306
|
}
|
|
307
|
+
return artifact
|
|
158
308
|
}
|
|
159
309
|
|
|
160
310
|
/** One action as its text, so a single-action policy keeps the artifact and digest it had; several as a list; none as `null`. */
|
package/src/temporal.ts
CHANGED
|
@@ -9,9 +9,10 @@
|
|
|
9
9
|
// and a boolean as 0 or 1: `storedTimestamp` is what a write binds and `answerRows` turns both back into what Postgres's
|
|
10
10
|
// driver answers.
|
|
11
11
|
|
|
12
|
-
import { defineValue, inSet, type NameSet, ownDescriptor, ownKeys } from './builtins'
|
|
12
|
+
import { defineValue, inSet, type NameSet, ownDescriptor, ownEntry, ownKeys } from './builtins'
|
|
13
13
|
import type { SqlDialect } from './database'
|
|
14
14
|
import { QueryRefusedError } from './errors'
|
|
15
|
+
import { type JsonSchema, schemaOutput, sqliteJson } from './json'
|
|
15
16
|
|
|
16
17
|
/** The text Postgres prints for a day in 0001–9999 under its default `DateStyle` (ISO). */
|
|
17
18
|
const DAY_TEXT = /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/
|
|
@@ -144,6 +145,10 @@ export interface RowColumns {
|
|
|
144
145
|
readonly timestamps: NameSet
|
|
145
146
|
/** Booleans: `true` or `false` (SQLite answers 1 or 0). */
|
|
146
147
|
readonly booleans: NameSet
|
|
148
|
+
/** JSON columns: the parsed value (SQLite answers its text). */
|
|
149
|
+
readonly json: NameSet
|
|
150
|
+
/** The JSON columns with a declared schema, by the name each arrives under: a read value is parsed by it. */
|
|
151
|
+
readonly jsonSchemas: Readonly<Record<string, JsonSchema>>
|
|
147
152
|
}
|
|
148
153
|
|
|
149
154
|
function unexpected(where: string, expected: string): QueryRefusedError {
|
|
@@ -167,17 +172,47 @@ function sqliteValue(columns: RowColumns, where: string, key: string, value: unk
|
|
|
167
172
|
}
|
|
168
173
|
return value === 1
|
|
169
174
|
}
|
|
175
|
+
if (inSet(columns.json, key)) {
|
|
176
|
+
return sqliteJson(value, where)
|
|
177
|
+
}
|
|
170
178
|
return typeof value === 'string' && inSet(columns.days, key) ? dayText(value, where) : value
|
|
171
179
|
}
|
|
172
180
|
|
|
173
181
|
/**
|
|
174
182
|
* Every row of a read as the layer answers it, in place: each day as `YYYY-MM-DD` text, and on SQLite each timestamp as
|
|
175
|
-
* a `Date
|
|
183
|
+
* a `Date`, each boolean as `true` or `false` and each JSON column as its parsed value, so a handler reads the same
|
|
184
|
+
* values on both dialects.
|
|
176
185
|
*/
|
|
177
186
|
export function answerRows<R>(dialect: SqlDialect, table: string, columns: RowColumns, rows: R[]): R[] {
|
|
178
|
-
|
|
179
|
-
|
|
187
|
+
return answerJson(table, columns.jsonSchemas, dialect === 'postgres' ? answerDays(table, columns.days, rows) : answerSqlite(table, columns, rows))
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Each value of a JSON column with a declared schema, in place, as the schema parses it; SQL NULL stays `null`. */
|
|
191
|
+
function answerJson<R>(table: string, schemas: Readonly<Record<string, JsonSchema>>, rows: R[]): R[] {
|
|
192
|
+
const keys = ownKeys(schemas)
|
|
193
|
+
if (keys.length === 0) {
|
|
194
|
+
return rows
|
|
180
195
|
}
|
|
196
|
+
for (let index = 0; index < rows.length; index++) {
|
|
197
|
+
const row = rows[index]
|
|
198
|
+
if (typeof row !== 'object' || row === null) {
|
|
199
|
+
continue
|
|
200
|
+
}
|
|
201
|
+
for (let position = 0; position < keys.length; position++) {
|
|
202
|
+
const key = keys[position] ?? ''
|
|
203
|
+
const schema = ownEntry(schemas, key)
|
|
204
|
+
const descriptor = ownDescriptor(row, key)
|
|
205
|
+
const value: unknown = descriptor?.value
|
|
206
|
+
if (schema === undefined || descriptor === undefined || value === null) {
|
|
207
|
+
continue
|
|
208
|
+
}
|
|
209
|
+
defineValue(row, key, schemaOutput(schema, value, `${table}.${key}`))
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return rows
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function answerSqlite<R>(table: string, columns: RowColumns, rows: R[]): R[] {
|
|
181
216
|
for (let index = 0; index < rows.length; index++) {
|
|
182
217
|
const row = rows[index]
|
|
183
218
|
if (typeof row !== 'object' || row === null) {
|