@fabrika/db 0.9.0-dev.5 → 0.9.0-dev.51

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.
Files changed (75) hide show
  1. package/package.json +9 -5
  2. package/src/authority.ts +560 -0
  3. package/src/builtins.ts +21 -0
  4. package/src/capability.ts +51 -2
  5. package/src/codegen-sqlite.ts +62 -22
  6. package/src/codegen.ts +181 -42
  7. package/src/condition-nodes.ts +119 -0
  8. package/src/conditions.ts +683 -0
  9. package/src/description.ts +37 -8
  10. package/src/errors.ts +8 -4
  11. package/src/file-columns.ts +52 -10
  12. package/src/framework.ts +5 -2
  13. package/src/guard.ts +365 -15
  14. package/src/index.ts +8 -0
  15. package/src/job-leases.ts +19 -2
  16. package/src/job-wakes.ts +183 -33
  17. package/src/json.ts +124 -0
  18. package/src/list-cursor.ts +82 -0
  19. package/src/migrations-sqlite.ts +93 -0
  20. package/src/migrations.ts +99 -0
  21. package/src/nodes.ts +21 -6
  22. package/src/notifications.ts +152 -0
  23. package/src/orm-types.ts +208 -74
  24. package/src/orm.ts +368 -146
  25. package/src/policy.ts +1973 -141
  26. package/src/push-subscriptions.ts +287 -0
  27. package/src/registry.ts +50 -1
  28. package/src/request-receipts.ts +107 -0
  29. package/src/sql.ts +39 -11
  30. package/src/sqlite-bun.ts +41 -14
  31. package/src/sqlite-migrate.ts +102 -1
  32. package/src/sqlite.ts +46 -1
  33. package/src/store.ts +1930 -167
  34. package/src/tables.ts +156 -6
  35. package/src/temporal.ts +8 -1
  36. package/src/testing-shared.ts +758 -140
  37. package/src/testing-sqlite.ts +212 -40
  38. package/src/testing.ts +151 -22
  39. package/src/vocabulary-tables.ts +159 -27
  40. package/types/src/authority.d.ts +147 -0
  41. package/types/src/builtins.d.ts +2 -0
  42. package/types/src/capability.d.ts +50 -2
  43. package/types/src/codegen-sqlite.d.ts +8 -1
  44. package/types/src/codegen.d.ts +54 -10
  45. package/types/src/condition-nodes.d.ts +9 -0
  46. package/types/src/conditions.d.ts +151 -0
  47. package/types/src/description.d.ts +26 -8
  48. package/types/src/errors.d.ts +7 -4
  49. package/types/src/file-columns.d.ts +6 -4
  50. package/types/src/framework.d.ts +5 -2
  51. package/types/src/guard.d.ts +24 -0
  52. package/types/src/index.d.ts +4 -3
  53. package/types/src/job-leases.d.ts +8 -1
  54. package/types/src/job-wakes.d.ts +28 -5
  55. package/types/src/json.d.ts +18 -0
  56. package/types/src/list-cursor.d.ts +13 -0
  57. package/types/src/migrations.d.ts +5 -0
  58. package/types/src/nodes.d.ts +4 -0
  59. package/types/src/notifications.d.ts +37 -0
  60. package/types/src/orm-types.d.ts +165 -73
  61. package/types/src/orm.d.ts +21 -0
  62. package/types/src/policy.d.ts +347 -23
  63. package/types/src/push-subscriptions.d.ts +49 -0
  64. package/types/src/registry.d.ts +36 -1
  65. package/types/src/request-receipts.d.ts +22 -0
  66. package/types/src/sql.d.ts +2 -2
  67. package/types/src/sqlite-bun.d.ts +11 -3
  68. package/types/src/sqlite-migrate.d.ts +32 -0
  69. package/types/src/store.d.ts +118 -31
  70. package/types/src/tables.d.ts +8 -2
  71. package/types/src/temporal.d.ts +4 -1
  72. package/types/src/testing-shared.d.ts +112 -23
  73. package/types/src/testing-sqlite.d.ts +17 -2
  74. package/types/src/testing.d.ts +1 -1
  75. package/types/src/vocabulary-tables.d.ts +169 -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 { ColumnPolicyArtifact, TableActionArtifact, TablePolicyArtifact, TableScope } from '@fabrika/auth-core'
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 { modelOf, type ScopeModel, type TableModel, tableModels, type Tables } from './policy'
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
- const read = ownEntry(model.columnReads, name)
138
- const write = ownEntry(model.columnWrites, name)
139
- setOwn(columns, name, { ...(read === undefined ? {} : { read }), ...(write === undefined ? {} : { write }) })
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
@@ -12,6 +12,7 @@
12
12
  import { defineValue, inSet, type NameSet, ownDescriptor, ownKeys } from './builtins'
13
13
  import type { SqlDialect } from './database'
14
14
  import { QueryRefusedError } from './errors'
15
+ import { 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,8 @@ 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
147
150
  }
148
151
 
149
152
  function unexpected(where: string, expected: string): QueryRefusedError {
@@ -167,12 +170,16 @@ function sqliteValue(columns: RowColumns, where: string, key: string, value: unk
167
170
  }
168
171
  return value === 1
169
172
  }
173
+ if (inSet(columns.json, key)) {
174
+ return sqliteJson(value, where)
175
+ }
170
176
  return typeof value === 'string' && inSet(columns.days, key) ? dayText(value, where) : value
171
177
  }
172
178
 
173
179
  /**
174
180
  * 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` and each boolean as `true` or `false`, so a handler reads the same values on both dialects.
181
+ * a `Date`, each boolean as `true` or `false` and each JSON column as its parsed value, so a handler reads the same
182
+ * values on both dialects.
176
183
  */
177
184
  export function answerRows<R>(dialect: SqlDialect, table: string, columns: RowColumns, rows: R[]): R[] {
178
185
  if (dialect === 'postgres') {