@fabrika/db 0.9.0-dev.11 → 0.9.0-dev.13

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/src/tables.ts CHANGED
@@ -1,11 +1,31 @@
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
+ ColumnPolicyArtifact,
6
+ ConditionArtifact,
7
+ ConfersArtifact,
8
+ MemberRevealsArtifact,
9
+ RevealsArtifact,
10
+ TableActionArtifact,
11
+ TablePolicyArtifact,
12
+ TableScope,
13
+ } from '@fabrika/auth-core'
5
14
  import { type ApplicationRowsPlan, planApplicationRows } from './application-rows'
6
15
  import { freeze, namesOf, ownEntry, ownKeys, setOwn } from './builtins'
16
+ import { conditionArtifact, type ConditionModel } from './conditions'
7
17
  import { PolicyError } from './errors'
8
- import { modelOf, type ScopeModel, type TableModel, tableModels, type Tables } from './policy'
18
+ import {
19
+ accessTablesOf,
20
+ type MemberRevealModel,
21
+ modelOf,
22
+ type RelationSource,
23
+ type RevealModel,
24
+ type ScopeModel,
25
+ type TableModel,
26
+ tableModels,
27
+ type Tables,
28
+ } from './policy'
9
29
 
10
30
  export { applicationRowRefusalText } from './application-rows'
11
31
  export type { ApplicationRowRefusal, ApplicationRowsPlan } from './application-rows'
@@ -85,6 +105,7 @@ export {
85
105
  } from './policy'
86
106
  export type {
87
107
  ColumnTypes,
108
+ ColumnViews,
88
109
  DefineVocabularyPolicies,
89
110
  NormalizedPolicies,
90
111
  TableVocabulary,
@@ -97,6 +118,7 @@ export type {
97
118
  VocabularyDimensionScope,
98
119
  VocabularyPolicyMap,
99
120
  VocabularyTablePolicy,
121
+ VocabularyViewPolicy,
100
122
  } from './vocabulary-tables'
101
123
 
102
124
  /**
@@ -112,6 +134,15 @@ export function applicationRowsPlan<DB, P>(
112
134
  return planApplicationRows(tableModels(tables), rows)
113
135
  }
114
136
 
137
+ /**
138
+ * The tables whose rows hand out access, lowercased: a relation source or a path reveal, a member-revealing table, and
139
+ * the tables a principal path or a relation's condition reads. Only the data layer writes them; the migration classifier
140
+ * refuses database code that does.
141
+ */
142
+ export function accessTables<DB, P>(tables: Tables<DB, P>): readonly string[] {
143
+ return accessTablesOf(tableModels(tables))
144
+ }
145
+
115
146
  /** Every table's policy in the form the contract artifact carries, by table name. */
116
147
  export type TablePolicies = Readonly<Record<string, TablePolicyArtifact>>
117
148
 
@@ -154,7 +185,78 @@ function artifactOf(model: TableModel): TablePolicyArtifact {
154
185
  createdBy: model.createdBy ?? null,
155
186
  columns,
156
187
  ...(ownKeys(model.files).length === 0 ? {} : { files: { ...model.files } }),
188
+ ...(model.view ? { view: true } : {}),
189
+ ...(model.confers.length === 0 ? {} : { confers: confersArtifact(model.confers) }),
190
+ ...(ownKeys(model.when).length === 0 ? {} : { when: whenArtifact(model.when) }),
191
+ ...(model.reveals.length === 0 ? {} : { reveals: revealsArtifact(model.reveals) }),
192
+ ...(model.readThrough.length === 0 ? {} : { readThrough: readThroughArtifact(model) }),
193
+ ...(model.memberReveals.length === 0 ? {} : { memberReveals: memberRevealsArtifact(model.memberReveals) }),
194
+ }
195
+ }
196
+
197
+ function memberRevealsArtifact(reveals: readonly MemberRevealModel[]): Record<string, MemberRevealsArtifact> {
198
+ const artifact: Record<string, MemberRevealsArtifact> = {}
199
+ for (let index = 0; index < reveals.length; index++) {
200
+ const reveal = reveals[index]
201
+ if (reveal === undefined) continue
202
+ setOwn(artifact, reveal.source, {
203
+ to: reveal.dimension,
204
+ actions: [...reveal.actions],
205
+ ...(reveal.when === undefined ? {} : { when: conditionArtifact(reveal.when) }),
206
+ })
207
+ }
208
+ return artifact
209
+ }
210
+
211
+ function readThroughArtifact(model: TableModel): Record<string, string> {
212
+ const artifact: Record<string, string> = {}
213
+ for (let index = 0; index < model.readThrough.length; index++) {
214
+ const path = model.readThrough[index]
215
+ if (path !== undefined) setOwn(artifact, path.dimension, path.via === undefined ? path.column : `${path.via.column}.${path.column}`)
216
+ }
217
+ return artifact
218
+ }
219
+
220
+ function revealsArtifact(reveals: readonly RevealModel[]): Record<string, RevealsArtifact> {
221
+ const artifact: Record<string, RevealsArtifact> = {}
222
+ for (let index = 0; index < reveals.length; index++) {
223
+ const reveal = reveals[index]
224
+ if (reveal === undefined) continue
225
+ setOwn(artifact, reveal.relation, {
226
+ to: reveal.dimension,
227
+ actions: [...reveal.actions],
228
+ ...(reveal.when === undefined ? {} : { when: conditionArtifact(reveal.when) }),
229
+ ...(reveal.assign === undefined ? {} : { assign: { self: reveal.assign.self } }),
230
+ })
231
+ }
232
+ return artifact
233
+ }
234
+
235
+ function confersArtifact(sources: readonly RelationSource[]): Record<string, ConfersArtifact> {
236
+ const confers: Record<string, ConfersArtifact> = {}
237
+ for (let index = 0; index < sources.length; index++) {
238
+ const source = sources[index]
239
+ if (source === undefined) continue
240
+ setOwn(confers, source.relation, {
241
+ principal: source.principalPath === undefined ? source.principal : `${source.principal}.${source.principalPath.column}`,
242
+ node: source.node,
243
+ expires: source.expires ?? null,
244
+ ...(source.when === undefined ? {} : { when: conditionArtifact(source.when) }),
245
+ assign: source.assign === undefined ? null : { maxDays: source.assign.maxDays ?? null, self: source.assign.self },
246
+ })
247
+ }
248
+ return confers
249
+ }
250
+
251
+ function whenArtifact(when: Readonly<Record<string, ConditionModel>>): Record<string, ConditionArtifact> {
252
+ const artifact: Record<string, ConditionArtifact> = {}
253
+ const actions = ownKeys(when)
254
+ for (let index = 0; index < actions.length; index++) {
255
+ const action = actions[index] ?? ''
256
+ const condition = ownEntry(when, action)
257
+ if (condition !== undefined) setOwn(artifact, action, conditionArtifact(condition))
157
258
  }
259
+ return artifact
158
260
  }
159
261
 
160
262
  /** One action as its text, so a single-action policy keeps the artifact and digest it had; several as a list; none as `null`. */
@@ -378,6 +378,9 @@ function scopedModel<DB, P>(tables: Tables<DB, P>, table: string): { readonly mo
378
378
  if (model === undefined) {
379
379
  throw new Error(`${table} is not a table of the policies`)
380
380
  }
381
+ if (model.view) {
382
+ throw new Error(`${table} is a view, whose rows come from the tables it reads: seed those instead`)
383
+ }
381
384
  return { models, model }
382
385
  }
383
386
 
@@ -1,20 +1,44 @@
1
1
  // The table policies of the vocabulary form, `defineTables(vocabulary, columns)`: how an application writes them, with
2
2
  // containment taken from the vocabulary, and the normalized policies the kit and the handles read. Types only.
3
3
 
4
- import type { ColumnList, ColumnPolicies, FilePolicies, RecordActions, ReferencePolicy, TableActions, Tables, VersionColumn } from './policy'
4
+ import type { ConditionPolicy } from './conditions'
5
+ import type {
6
+ ColumnList,
7
+ ColumnPolicies,
8
+ ConfersPolicy,
9
+ FilePolicies,
10
+ MemberRevealsPolicy,
11
+ RecordActions,
12
+ ReferencePolicy,
13
+ RevealsPolicy,
14
+ TableAction,
15
+ TableActions,
16
+ Tables,
17
+ VersionColumn,
18
+ } from './policy'
5
19
 
6
20
  declare const databaseType: unique symbol
21
+ declare const viewNames: unique symbol
7
22
 
8
23
  /**
9
- * The generated column list (`src/db/schema.generated.ts`), which also carries the schema's row types, so that
10
- * `defineTables(vocabulary, columns)` takes no type argument. The brand is type-only: no value has the property.
24
+ * The generated column list (`src/db/schema.generated.ts`), which also carries the schema's row types and the names of
25
+ * its views, so that `defineTables(vocabulary, columns)` takes no type argument. The brands are type-only: no value
26
+ * has either property.
11
27
  */
12
- export type TypedColumnList<DB> = ColumnList<DB> & { readonly [databaseType]?: DB }
28
+ export type TypedColumnList<DB, Views extends keyof DB & string = never> = ColumnList<DB> & {
29
+ readonly [databaseType]?: DB
30
+ readonly [viewNames]?: { readonly views: Views }
31
+ }
13
32
 
14
33
  /** The row types a typed column list carries; `never` for a list without them. */
15
34
  export type ColumnTypes<C> = typeof databaseType extends keyof C ? C extends { readonly [databaseType]?: infer DB } ? Exclude<DB, undefined> : never
16
35
  : never
17
36
 
37
+ /** The views a typed column list names; `never` for a list without views. */
38
+ export type ColumnViews<C> = typeof viewNames extends keyof C
39
+ ? C extends { readonly [viewNames]?: { readonly views: infer Views extends string } } ? Views : never
40
+ : never
41
+
18
42
  /**
19
43
  * What `defineTables(vocabulary, columns)` reads of `@fabrika/app/contract`'s `defineVocabulary` result: the action
20
44
  * and dimension names, and each dimension's container. `@fabrika/db` does not import `@fabrika/app`.
@@ -22,6 +46,7 @@ export type ColumnTypes<C> = typeof databaseType extends keyof C ? C extends { r
22
46
  export interface TableVocabulary {
23
47
  readonly actions: { readonly [action: string]: string }
24
48
  readonly dimensions: { readonly [dimension: string]: { readonly in?: string } }
49
+ readonly relations?: { readonly [relation: string]: { readonly dimension: string } }
25
50
  }
26
51
 
27
52
  export type VocabularyAction<V> = V extends { readonly actions: infer A } ? keyof A & string : never
@@ -76,6 +101,19 @@ interface WritableFields<Col extends string, A extends string, Ver extends Col,
76
101
  readonly columns?: ColumnPolicies<Col, A>
77
102
  /** The file columns, each with the file kind it holds (`c.files`): a `uuid` foreign key to `fabrika_files`. */
78
103
  readonly files?: FilePolicies<Col>
104
+ /** Relation → how a row of this table confers it: whom it names, at which node, until when. */
105
+ readonly confers?: { readonly [relation: string]: ConfersPolicy<Col> }
106
+ /** Action → the condition a row must meet for it, whoever holds it. */
107
+ readonly when?: { readonly [action in A]?: ConditionPolicy<Col> }
108
+ /** Relation of the table's own dimension → the ancestor node a row it reaches reveals, and for which actions. */
109
+ readonly reveals?: { readonly [relation: string]: RevealsPolicy<Col, A> }
110
+ /**
111
+ * Dimension outside the chain → the foreign key column a `Within`'s read reaches a row through, no update writes it; or
112
+ * `<nearest container column>.<its column>`, the nearest container's own path.
113
+ */
114
+ readonly readThrough?: { readonly [dimension: string]: Col | `${Col}.${string}` }
115
+ /** The table's own dimension → the ancestor node a row reveals to a grant at its node, for a `Within`'s reads. */
116
+ readonly memberReveals?: { readonly [dimension: string]: MemberRevealsPolicy<Col, A> }
79
117
  }
80
118
 
81
119
  /**
@@ -126,6 +164,47 @@ type AuthoredChildPolicy<Table extends string, Col extends string, A extends str
126
164
  & {
127
165
  readonly scope: ChildScopes<Table, Col, S, P>
128
166
  }
167
+ /** What a view's policy holds besides its scope: read actions only, no write list and no stamped column. */
168
+ type ViewIsReadOnly = 'a view is never written: its actions are read, appRead and appCount'
169
+
170
+ /** The write actions a view's policy may not name, each typed as the message: a literal is not checked for excess keys. */
171
+ interface NoWriteActions {
172
+ readonly insert?: ViewIsReadOnly
173
+ readonly update?: ViewIsReadOnly
174
+ readonly delete?: ViewIsReadOnly
175
+ }
176
+
177
+ interface ReadOnlyFields<Col extends string, A extends string> {
178
+ readonly actions: { readonly read: TableAction<A>; readonly appRead?: readonly A[]; readonly appCount?: readonly A[] } & NoWriteActions
179
+ readonly insert?: never
180
+ readonly update?: never
181
+ readonly version?: never
182
+ readonly createdBy?: never
183
+ readonly queue?: never
184
+ readonly files?: never
185
+ readonly columns?: { readonly [C in Col]?: { readonly read: A; readonly write?: never } }
186
+ }
187
+
188
+ /**
189
+ * A view read as a child table: its parent's `Ref` and `Within` read it; nothing writes it (ADR-0072). Its `when` narrows
190
+ * its read actions; a path may start at its parent column, to the parent row.
191
+ */
192
+ type AuthoredChildViewPolicy<Table extends string, Col extends string, A extends string, S, P> = ReadOnlyFields<Col, A> & {
193
+ readonly scope: ChildScopes<Table, Col, S, P>
194
+ readonly when?: { readonly [action in A]?: ConditionPolicy<Col> }
195
+ }
196
+
197
+ /** A view's policy as written: a read-only child, or a reference table every site reads. */
198
+ export type VocabularyViewPolicy<Table extends string, Col extends string, A extends string, S = unknown, P = unknown> = S extends
199
+ { readonly child: unknown } ? AuthoredChildViewPolicy<Table, Col, A, S, P>
200
+ : S extends { readonly reference: unknown } ? ReferenceViewPolicy<Col, A>
201
+ : AuthoredChildViewPolicy<Table, Col, A, S, P> | ReferenceViewPolicy<Col, A>
202
+
203
+ type ReferenceViewPolicy<Col extends string, A extends string> = ReferencePolicy<Col, A> & {
204
+ readonly actions: NoWriteActions
205
+ readonly columns?: { readonly [C in Col]?: { readonly read: A; readonly write?: never } }
206
+ }
207
+
129
208
  /** An application table's scope: why its rows belong to no dimension; `key` defaults to `id`. */
130
209
  type AuthoredApplicationPolicy<Col extends string, A extends string, Ver extends Col> = WritableFields<Col, A, Ver> & {
131
210
  readonly scope: { readonly app: string } & KeyField<Col>
@@ -157,15 +236,24 @@ type AnyTablePolicy<DB, V, T extends keyof DB & string> = VocabularyTablePolicy<
157
236
  VersionColumn<DB[T]>
158
237
  >
159
238
 
239
+ /** The policy of view `T` with nothing stated: both forms, any parent. */
240
+ type AnyViewPolicy<DB, V, T extends keyof DB & string> = VocabularyViewPolicy<keyof DB & string, keyof DB[T] & string, VocabularyAction<V>>
241
+
160
242
  /**
161
- * Every table of the schema, and no other, maps to a policy; a misspelt action or dimension fails at its own line.
162
- * `P` is the policies as written, which picks each table's form by the scope it states; a policy written under this
163
- * map's own annotation states nothing narrower, so it is checked against every form.
243
+ * Every table of the schema, and no other, maps to a policy; a view `Views` names may (ADR-0072). A misspelt action or
244
+ * dimension fails at its own line. `P` is the policies as written, which picks each table's form by the scope it
245
+ * states; a policy written under this map's own annotation states nothing narrower, so it is checked against every
246
+ * form.
164
247
  */
165
- export type VocabularyPolicyMap<DB, V, P = unknown> = {
166
- readonly [T in keyof DB & string]: AnyTablePolicy<DB, V, T> extends (T extends keyof P ? P[T] : unknown) ? AnyTablePolicy<DB, V, T>
167
- : VocabularyTablePolicy<keyof DB & string, keyof DB[T] & string, VocabularyAction<V>, V, VersionColumn<DB[T]>, StatedScope<P, T>, P>
168
- }
248
+ export type VocabularyPolicyMap<DB, V, P = unknown, Views extends string = never> =
249
+ & {
250
+ readonly [T in Exclude<keyof DB & string, Views>]: AnyTablePolicy<DB, V, T> extends (T extends keyof P ? P[T] : unknown) ? AnyTablePolicy<DB, V, T>
251
+ : VocabularyTablePolicy<keyof DB & string, keyof DB[T] & string, VocabularyAction<V>, V, VersionColumn<DB[T]>, StatedScope<P, T>, P>
252
+ }
253
+ & {
254
+ readonly [T in Views & keyof DB & string]?: AnyViewPolicy<DB, V, T> extends (T extends keyof P ? P[T] : unknown) ? AnyViewPolicy<DB, V, T>
255
+ : VocabularyViewPolicy<keyof DB & string, keyof DB[T] & string, VocabularyAction<V>, StatedScope<P, T>, P>
256
+ }
169
257
 
170
258
  type Flat<T> = { [K in keyof T]: T[K] }
171
259
 
@@ -192,15 +280,20 @@ type NormalScope<S, V, P> = S extends { readonly dimension: infer D extends stri
192
280
  : S extends { readonly app: infer R extends string } ? { readonly app: R; readonly key: KeyOf<S> }
193
281
  : S
194
282
 
283
+ /** A view's normalized policy says so, which the handle types read: no site writes it. */
284
+ type ViewMark<T, Views> = T extends Views ? { readonly view: true } : unknown
285
+
195
286
  /**
196
287
  * The policies as the layer models them: every dimension table's key and container, and every child's key and
197
- * column, stated as the vocabulary and the conventions derive them.
288
+ * column, stated as the vocabulary and the conventions derive them; each view marked.
198
289
  */
199
- export type NormalizedPolicies<P, V> = {
200
- readonly [T in keyof P]: P[T] extends { readonly scope: infer S } ? Flat<Omit<P[T], 'scope'> & { readonly scope: NormalScope<S, V, P> }> : P[T]
290
+ export type NormalizedPolicies<P, V, Views = never> = {
291
+ readonly [T in keyof P]: P[T] extends { readonly scope: infer S }
292
+ ? Flat<Omit<P[T], 'scope' | 'view'> & { readonly scope: NormalScope<S, V, P> } & ViewMark<T, Views>>
293
+ : P[T]
201
294
  }
202
295
 
203
- /** Takes the policies of exactly the tables of the schema, written in the vocabulary form. */
204
- export type DefineVocabularyPolicies<DB, V> = <const P extends VocabularyPolicyMap<DB, V, P>>(
296
+ /** Takes the policies of exactly the tables of the schema, and of any of its views, written in the vocabulary form. */
297
+ export type DefineVocabularyPolicies<DB, V, Views extends string = never> = <const P extends VocabularyPolicyMap<DB, V, P, Views>>(
205
298
  policies: P & { readonly [K in Exclude<keyof P, keyof DB>]: never },
206
- ) => Tables<DB, NormalizedPolicies<P, V>>
299
+ ) => Tables<DB, NormalizedPolicies<P, V, Views>>
@@ -0,0 +1,66 @@
1
+ import { type OperationNode } from 'kysely';
2
+ import type { RefGrant, WithinGrant } from './capability';
3
+ import type { ViewParentLink } from './guard';
4
+ import { type Container, type MemberRevealModel, type RelationSource, type RevealModel, type SecondaryPath, type TableModel } from './policy';
5
+ /** A relation of the layer: its source table, and the tables whose rows it reaches reveal an ancestor node. */
6
+ export interface LayerRelation extends RelationSource {
7
+ readonly reveals: readonly RevealModel[];
8
+ }
9
+ /** Every relation of the layer, by name, with its source table. */
10
+ export type RelationSources = Readonly<Record<string, LayerRelation>>;
11
+ /** Every member reveal of the layer, by the dimension whose grants it reveals to. */
12
+ export type MemberReveals = Readonly<Record<string, readonly MemberRevealModel[]>>;
13
+ export declare function relationSource(relations: RelationSources, relation: string): LayerRelation;
14
+ /** A source row that confers its relation now: unexpired, and admitted by the relation's own condition. */
15
+ export declare function activeRow(source: RelationSource, now: Date): readonly OperationNode[];
16
+ /**
17
+ * A source row naming `principal`: `S.<principal> = $me`, or through its path `S.<column> in (select P.<key> from P where
18
+ * P.<column> = $me)`. Every check of a source row's principal is built here.
19
+ */
20
+ export declare function namesPrincipal(source: RelationSource, principal: string): OperationNode;
21
+ /**
22
+ * The rows of `table` whose `path` column names one of `nodes` of `dimension`: `T.<column> in (nodes)` at the path's own
23
+ * dimension, `T.<column> in (select D.<key> from D where D.<link> in (nodes))` at one above it; `undefined` elsewhere. A
24
+ * path through the nearest container reads the container's: `T.<link> in (select C.<key> from C where <C's term>)`.
25
+ */
26
+ export declare function throughPath(table: string, path: SecondaryPath, dimension: string, nodes: OperationNode): OperationNode | undefined;
27
+ /** `select R.<ancestor> from R where R.<key> in (<the relation's nodes>) and <when>`: the nodes a reveal holds `principal` at. */
28
+ export declare function revealedNodes(source: RelationSource, reveal: RevealModel, principal: string, now: Date): OperationNode;
29
+ /**
30
+ * The rows of `model` a relation reaches for `principal` under `action`: `T.<node> in (select S.<node> from S where
31
+ * S.<principal> = $me and <active>)` where `model` lies at a node of the relation's dimension, and one
32
+ * `T.<ancestor> in (<revealed nodes>)` per reveal of `action` to an ancestor `model` lies at. Empty where neither holds.
33
+ * A `Within`'s read (`reads`) also follows the secondary paths: the column `model` reaches the relation's dimension
34
+ * through, and the reveals matched through one. Every other statement follows containment only.
35
+ */
36
+ export declare function relationTerms(relations: RelationSources, model: TableModel, relation: string, principal: string, action: string, now: Date, reads: boolean): readonly OperationNode[];
37
+ /**
38
+ * The rows of `model` a `Within`'s read reaches through member reveals: per grant it carries at a revealing dimension,
39
+ * `T.<ancestor> in (select R.<ancestor> from R where R.<key> in (values) and <when>)` for each reveal of `action` to an
40
+ * ancestor `model` lies at. A grant at a dimension that reveals no node for `action` is refused: the framework passes
41
+ * none.
42
+ */
43
+ export declare function memberTerms(members: MemberReveals, model: TableModel, grant: WithinGrant, now: Date): readonly OperationNode[];
44
+ /** The condition `action` holds a row of `model` under, or `undefined` when the action is not narrowed there. */
45
+ export declare function actionCondition(model: TableModel, action: string, now: Date): OperationNode | undefined;
46
+ /**
47
+ * `branch`, narrowed by the condition of `action`: `<reach> and (<when>)`, for a global grant `true and (<when>)`, the
48
+ * one shape the guard admits a condition in, so a condition never stands in for the reach.
49
+ */
50
+ export declare function conditioned(model: TableModel, action: string, branch: OperationNode | 'all', now: Date): OperationNode;
51
+ /**
52
+ * A Ref's predicate over `links` of `model`: one equality per link of its chain (after `row`, the equality that
53
+ * addresses one row by its key), then, when the Ref is held through relations, a row of one of them at the node, and
54
+ * its action's condition. A relation that does not reach `model` leaves the Ref unenforceable there, never
55
+ * unconditioned.
56
+ */
57
+ export declare function refScope(relations: RelationSources, model: TableModel, links: readonly Container[], grant: RefGrant, now: Date, row?: OperationNode): OperationNode;
58
+ /**
59
+ * The rows of a child view whose parent row `reached` admits: `V.<column> in (select P.<key> from P where <reached>)`,
60
+ * after `row` when it addresses one row, then the view's own condition of `action`. For `all`, every parent row, so a
61
+ * row whose parent column names none is still read by no capability. No column of the view places a row: its
62
+ * containment rests on the parent's foreign keys (ADR-0072).
63
+ */
64
+ export declare function viewScope(model: TableModel, parent: ViewParentLink, action: string, reached: OperationNode | 'all', now: Date, row?: OperationNode): OperationNode;
65
+ /** The relation rows a Ref held through relations needs at `model`, or `undefined` for a Ref an IAM grant holds. */
66
+ export declare function relationGate(relations: RelationSources, model: TableModel, grant: RefGrant, now: Date): OperationNode | undefined;
@@ -40,3 +40,5 @@ export declare function setOwn<V>(record: Record<string, V>, key: string, value:
40
40
  export declare function defineValue(target: object, key: string, value: unknown): void;
41
41
  /** An own property of a record, or undefined: an inherited one is never read. */
42
42
  export declare function ownEntry<V>(record: Readonly<Record<string, V>>, key: string): V | undefined;
43
+ /** A copy of a plain object's own data properties; anything else (an instance, a symbol key, an accessor) is refused. */
44
+ export declare function plainRecord(value: unknown, what: string): Readonly<Record<string, unknown>>;
@@ -47,6 +47,11 @@ export interface RefGrant {
47
47
  readonly id: string;
48
48
  /** The values of the containers this object lies in, by dimension (`workspace → w1` for a note). */
49
49
  readonly chain: ReadonlyMap<string, string>;
50
+ /**
51
+ * The relations the caller holds the action through on this object or a container, when no grant holds it: every
52
+ * statement of the Ref then also requires a row of one of them. Empty for a Ref an IAM grant holds.
53
+ */
54
+ readonly relations: readonly string[];
50
55
  }
51
56
  /** The values of one container dimension in which the caller holds the action: `organization → [o1]`. */
52
57
  export interface ContainerGrant {
@@ -75,6 +80,16 @@ export interface WithinGrant {
75
80
  * the action: a row that lies in one of their values is listed, as a single-object read of it is allowed.
76
81
  */
77
82
  readonly contained: readonly ContainerGrant[];
83
+ /**
84
+ * The relations through which the caller also holds the action (`~rel` entries of its token): a row lies in the
85
+ * `Within` too where a row of one of them names the caller at the row's node, read by the statement itself.
86
+ */
87
+ readonly relations: readonly string[];
88
+ /**
89
+ * The caller's grants of the action at nodes whose rows reveal the `Within`'s dimension or a container of it to their
90
+ * members (`memberReveals`): a row under a node such a row reveals is read too, the statement judging the revealing row.
91
+ */
92
+ readonly revealing: readonly ContainerGrant[];
78
93
  }
79
94
  /** An `app` endpoint's grant: the action everywhere in the application, which the proxy checked was held globally. */
80
95
  export interface AppGrant {
@@ -14,6 +14,7 @@ export declare class CodegenError extends Error {
14
14
  }
15
15
  declare const catalogColumnSchema: z.ZodObject<{
16
16
  table_name: z.ZodString;
17
+ view: z.ZodBoolean;
17
18
  column_name: z.ZodString;
18
19
  type_name: z.ZodString;
19
20
  type_type: z.ZodString;
@@ -61,20 +62,27 @@ export interface DescribedColumn {
61
62
  /** The column list field that names the column: a day, a timestamp, a JSON column, or (on SQLite) a boolean. */
62
63
  readonly list: 'dates' | 'timestamps' | 'booleans' | 'json' | undefined;
63
64
  }
64
- /** A column `describe` leaves out of the module: a generated column of a type the data layer does not read. */
65
+ /**
66
+ * A column `describe` leaves out of the module: a generated column, or a view's column, of a type the data layer does
67
+ * not read.
68
+ */
65
69
  export interface LeftOut {
66
- /** The column's type, as its author wrote it. */
70
+ /** The column's type, as its author wrote it (empty: a SQLite view's expression, which has none). */
67
71
  readonly leftOut: string;
72
+ readonly of: LeftOutKind;
68
73
  }
74
+ /** Why a column is left out rather than refused: it is computed, by a generated column or by a view. */
75
+ export type LeftOutKind = 'generated' | 'view';
69
76
  /** A column the module leaves out; only `ctx.sql` reaches it. */
70
77
  export interface LeftOutColumn {
71
78
  readonly table: string;
72
79
  readonly column: string;
73
80
  /** Its type, as its author wrote it. */
74
81
  readonly type: string;
82
+ readonly of: LeftOutKind;
75
83
  }
76
84
  /** What codegen says of a left-out column: in the module, `name` is the column; in its output, `table.column`. */
77
- export declare function leftOutText(name: string, type: string): string;
85
+ export declare function leftOutText(name: string, type: string, of: LeftOutKind): string;
78
86
  /** Why a left-out column refuses the schema: the table would lose a key, or a foreign key a column. */
79
87
  export declare function leftOutRefusal(path: string, what: string): CodegenError;
80
88
  /** The text of `SCHEMA_FILE`, and the columns it leaves out, for the command to name. */
@@ -94,20 +102,25 @@ export interface NamedColumn {
94
102
  readonly table_name: string;
95
103
  readonly column_name: string;
96
104
  }
105
+ /** A column of a table or of a view, as `renderTables` reads it. */
106
+ export interface RelationColumn extends NamedColumn {
107
+ /** The relation is a view: read-only, with no key or foreign key. */
108
+ readonly view: boolean;
109
+ }
97
110
  /**
98
- * The generated module: one interface per application table, the `DB` map Kysely and `defineTables` take, and the
99
- * runtime column list with each table's foreign keys to application tables; with the columns it leaves out.
100
- * Deterministic for a given catalog, so `fabrika db codegen --verify` can compare text.
111
+ * The generated module: one interface per application table and view, the `DB` map Kysely and `defineTables` take,
112
+ * and the runtime column list with each table's foreign keys to application tables and each view marked; with the
113
+ * columns it leaves out. Deterministic for a given catalog, so `fabrika db codegen --verify` can compare text.
101
114
  */
102
115
  export declare function renderSchema(catalog: readonly CatalogColumn[], foreignKeys?: readonly CatalogForeignKey[]): GeneratedModule;
103
116
  /**
104
117
  * `renderSchema` over columns of any database: `describe` gives each column's types and list field, refuses a type the
105
- * data layer does not read, or answers `LeftOut` for a generated column of such a type. That column is left out of the
106
- * module but for a comment where it stands, so no handle reads or writes it and only `ctx.sql` reaches it; a foreign
107
- * key that names it, and a table it would leave without a column, are refused. A foreign key's `constraint_name` is
108
- * not rendered; keys keep the given order.
118
+ * data layer does not read, or answers `LeftOut` for a generated column or a view's column of such a type. That column
119
+ * is left out of the module but for a comment where it stands, so no handle reads or writes it and only `ctx.sql`
120
+ * reaches it; a foreign key that names it, and a table it would leave without a column, are refused, and a view left
121
+ * without a column is left out whole. A foreign key's `constraint_name` is not rendered; keys keep the given order.
109
122
  */
110
- export declare function renderTables<C extends NamedColumn>(catalog: readonly C[], foreignKeys: readonly ForeignKeyColumns[], describe: (column: C) => DescribedColumn | LeftOut): GeneratedModule;
123
+ export declare function renderTables<C extends RelationColumn>(catalog: readonly C[], foreignKeys: readonly ForeignKeyColumns[], describe: (column: C) => DescribedColumn | LeftOut): GeneratedModule;
111
124
  /** Introspects the migrated schema, checked against the migrations' manifest, and renders the module for `SCHEMA_FILE`. */
112
125
  export declare function generateSchema(url: string, schema: string, objects: readonly DatabaseObject[]): Promise<GeneratedModule>;
113
126
  export {};
@@ -0,0 +1,9 @@
1
+ import { type OperationNode } from 'kysely';
2
+ import { type ConditionModel } from './conditions';
3
+ /** The predicate of `condition` over `table`'s rows; `now` is the statement's clock, bound as a value. */
4
+ export declare function conditionNode(table: string, condition: ConditionModel, now: Date): OperationNode;
5
+ /**
6
+ * `select table.key from table where <where>`: the one subquery shape the guard admits in a scope predicate. Without
7
+ * `where`, every row's key: only a child view's parent subquery takes that form.
8
+ */
9
+ export declare function subquery(table: string, key: string, where?: OperationNode): OperationNode;