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

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.
@@ -1,5 +1,5 @@
1
1
  import { type OperationNode, type ReturningNode } from 'kysely';
2
- import { type MemberReveals, type RelationSources } from './authority';
2
+ import { type MemberReveals, type PathRule, type RelationSources } from './authority';
3
3
  import { type NameSet } from './builtins';
4
4
  import { type AppGrant, type CapabilityAuthority, type Ref, type RefGrant, type Within, type WithinGrant } from './capability';
5
5
  import { type ConditionModel } from './conditions';
@@ -7,7 +7,7 @@ import { type Database, type Session, type SqlDialect } from './database';
7
7
  import { type ComparableColumn, type Compare, type Contains, type NoColumns, type Primitive, type ScopedSelect, type TableShape } from './description';
8
8
  import { type FileBinding } from './file-columns';
9
9
  import type { ForeignKeyPair, GuardContext, RootKind, ViewParentLink } from './guard';
10
- import { type Acknowledgement, type ActionOf, type ChildTableName, type Container, type ContainerOf, type DimensionOf, type DimensionTableName, type InsertValues, type JoinableChildren, type ListDimensionOf, type MoveAcknowledgement, type Operation, type ParentOf, type PolicyRow, type SecondaryPath, type TableModel, type TableModels, type Tables, type UpdateValues, type VersionArguments } from './policy';
10
+ import { type Acknowledgement, type ActionOf, type ChildTableName, type Container, type ContainerOf, type DimensionOf, type DimensionTableName, type InsertValues, type JoinableChildren, type ListDimensionOf, type MoveAcknowledgement, type Operation, type ParentOf, type PathWriter, type PolicyRow, type SecondaryPath, type TableModel, type TableModels, type Tables, type UpdateValues, type VersionArguments } from './policy';
11
11
  import type { ScopeTerms, ScopeUse } from './sql';
12
12
  import { type TablePolicies } from './tables';
13
13
  import { type RowColumns } from './temporal';
@@ -44,6 +44,12 @@ export interface TableContext {
44
44
  readonly subqueries: Readonly<Record<string, readonly string[]>>;
45
45
  /** A child view's parent, which every capability reads the view's rows through; `undefined` for any other table. */
46
46
  readonly through: ViewParent | undefined;
47
+ /** The path columns whose insert hands out actions, each with the table owning the node it names (`pathWriters`). */
48
+ readonly writers: readonly PathBound[];
49
+ }
50
+ /** A path column an insert writes under its bound, and the table that owns the dimension it names. */
51
+ export interface PathBound extends PathWriter {
52
+ readonly owner: TableModel;
47
53
  }
48
54
  /** A child view's parent link, with the parent's context: its model without conditions, as only the view's own narrow a read. */
49
55
  export interface ViewParent extends ViewParentLink {
@@ -77,8 +83,8 @@ export declare function requiredActions(context: TableContext, operation: Operat
77
83
  export declare function checkAction(context: TableContext, operation: Operation, action: string): void;
78
84
  /** The object a Ref of the model's own dimension names, with every link of its chain. */
79
85
  export declare function selfLinks(model: TableModel): readonly Container[];
80
- /** This object, over the whole chain the Ref carries. */
81
- export declare function objectPredicate(context: TableContext, capability: unknown, operation: Operation): OperationNode;
86
+ /** This object, over the whole chain the Ref carries; through the secondary paths `paths` admits, by default its action's. */
87
+ export declare function objectPredicate(context: TableContext, capability: unknown, operation: Operation, paths?: PathRule): OperationNode;
82
88
  /** Rows of a dimension table within the scope values of its list dimension. */
83
89
  export declare function dimensionListPredicate(context: TableContext, capability: unknown, operation: Operation): OperationNode | undefined;
84
90
  /**
@@ -198,7 +204,7 @@ export declare function selectOne<O>(context: TableContext, run: Runner, scope:
198
204
  export declare function answer(context: TableContext): ReturningNode;
199
205
  export declare function keyOf(model: TableModel): string;
200
206
  /** An insert, in a transaction that first locks its destination and judges its writer's bounds and its condition. */
201
- export declare function insertRow<O>(context: TableContext, run: Runner, given: ReadonlyMap<string, Primitive | null>, prepared: Pick<PreparedInsert, 'destination' | 'writer' | 'judged'> | undefined): Promise<O>;
207
+ export declare function insertRow<O>(context: TableContext, run: Runner, given: ReadonlyMap<string, Primitive | null>, prepared: Pick<PreparedInsert, 'destination' | 'writer' | 'judged' | 'narrowed'> | undefined): Promise<O>;
202
208
  /**
203
209
  * `insert(values, { onConflict: { columns, do } })`: on a conflict of `columns` (a unique index of the table), `nothing`
204
210
  * writes nothing and `update` sets the columns the table's update action writes, from `updates`.
@@ -228,7 +234,17 @@ export interface Addressed {
228
234
  * Zero changed rows → one follow-up read in the same transaction → 404 when the object is gone, else 409. An update
229
235
  * that sets no column writes nothing, not even the version: it reads the acknowledgement under the same conditions.
230
236
  */
231
- export declare function updateRow<O>(context: TableContext, run: Runner, target: Addressed, given: ReadonlyMap<string, Primitive | null>, expectedVersion: OperationNode | undefined, destination?: RefGrant): Promise<O>;
237
+ export declare function updateRow<O>(context: TableContext, run: Runner, target: Addressed, given: ReadonlyMap<string, Primitive | null>, expectedVersion: OperationNode | undefined, moved?: MovedRow): Promise<O>;
238
+ /**
239
+ * A move's destination, locked before the write, and the destination's insert conditions, which judge the moved row
240
+ * after it as an insert into the destination judges its row: the insert action's (`judgeInserted`), and each one
241
+ * `relationWhen` puts on a relation the destination `Ref` holds through (`judgeNarrowed`).
242
+ */
243
+ export interface MovedRow {
244
+ readonly destination: RefGrant;
245
+ readonly judged: JudgedInsert | undefined;
246
+ readonly narrowed: readonly NarrowedInsert[];
247
+ }
232
248
  /** Like `updateRow`: under `expectVersion`, a row that changed is 409 and a missing one 404. */
233
249
  export declare function removeRow(context: TableContext, run: Runner, target: Addressed, expectedVersion?: OperationNode): Promise<void>;
234
250
  type Columns<Row> = keyof Row & string;
@@ -274,11 +290,18 @@ export interface PreparedInsert {
274
290
  readonly conflict: OnConflict | undefined;
275
291
  /** The insert action's condition when it follows a path: judged in the insert's transaction, on the rows it names. */
276
292
  readonly judged: JudgedInsert | undefined;
293
+ /** The destination's relations whose `relationWhen` for the insert follows a path: judged on the written row. */
294
+ readonly narrowed: readonly NarrowedInsert[];
277
295
  }
278
296
  export interface JudgedInsert {
279
297
  readonly action: string;
280
298
  readonly condition: ConditionModel;
281
299
  }
300
+ /** A relation the destination `Ref` holds through, which the inserted table narrows for the insert by a condition with a path. */
301
+ export interface NarrowedInsert {
302
+ readonly relation: string;
303
+ readonly condition: ConditionModel;
304
+ }
282
305
  /**
283
306
  * A dimension table's insert over a capability nobody type-checked: into a container (a new UUIDv7 key, the chain from
284
307
  * the container's `Ref`), or for a top-level table the object the `Ref` names.
@@ -316,7 +339,9 @@ export declare class DimensionTable<DB, P, T extends string> {
316
339
  /**
317
340
  * Moves the object to another container: the delete action at the source, the insert action at the target,
318
341
  * whose chain is checked against the database. Every chain column changes; children that copy them follow
319
- * through `ON UPDATE CASCADE` on their composite foreign key. Answers the acknowledgement and the new chain.
342
+ * through `ON UPDATE CASCADE` on their composite foreign key. The moved row is judged by the insert action's
343
+ * condition and by the `relationWhen` of each relation the target holds through, as an insert into the target is.
344
+ * Answers the acknowledgement and the new chain.
320
345
  */
321
346
  move(ref: Ref<DimensionOf<P, T>, ActionOf<P, T, 'delete'>>, to: Ref<ContainerOf<P, T>, ActionOf<P, T, 'insert'>>): Promise<MoveAcknowledgement<P, T>>;
322
347
  aggregate<const S extends AggregateSpec<Row<DB, P, T>>>(within: Within<ListDimensionOf<P, T>, ReadAction<P, T>>, spec: S): Promise<AggregateRow<Row<DB, P, T>, S>[]>;
@@ -1,5 +1,5 @@
1
1
  import type { ConditionPolicy } from './conditions';
2
- import type { ColumnList, ColumnPolicies, ConfersPolicy, FilePolicies, MemberRevealsPolicy, RecordActions, ReferencePolicy, RevealsPolicy, TableAction, TableActions, Tables, VersionColumn } from './policy';
2
+ import type { AccessPolicy, ColumnList, ColumnPolicies, ConfersPolicy, FilePolicies, MemberRevealsPolicy, RecordActions, ReferencePolicy, RevealsPolicy, TableAction, TableActions, Tables, VersionColumn } from './policy';
3
3
  declare const databaseType: unique symbol;
4
4
  declare const viewNames: unique symbol;
5
5
  /**
@@ -106,6 +106,12 @@ interface WritableFields<Col extends string, A extends string, Ver extends Col,
106
106
  readonly when?: {
107
107
  readonly [action in A]?: ConditionPolicy<Col>;
108
108
  };
109
+ /** Relation → action → the condition a row that relation reaches must meet for it; IAM grants are not narrowed. */
110
+ readonly relationWhen?: {
111
+ readonly [relation: string]: {
112
+ readonly [action in A]?: ConditionPolicy<Col>;
113
+ };
114
+ };
109
115
  /** Relation of the table's own dimension → the ancestor node a row it reaches reveals, and for which actions. */
110
116
  readonly reveals?: {
111
117
  readonly [relation: string]: RevealsPolicy<Col, A>;
@@ -117,10 +123,21 @@ interface WritableFields<Col extends string, A extends string, Ver extends Col,
117
123
  readonly readThrough?: {
118
124
  readonly [dimension: string]: Col | `${Col}.${string}`;
119
125
  };
126
+ /**
127
+ * Dimension of `readThrough` → the actions a `Ref` held through that path holds, writes included; whoever inserts the
128
+ * path's column must hold each at the node it names.
129
+ */
130
+ readonly writeThrough?: {
131
+ readonly [dimension: string]: readonly A[];
132
+ };
120
133
  /** The table's own dimension → the ancestor node a row reveals to a grant at its node, for a `Within`'s reads. */
121
134
  readonly memberReveals?: {
122
135
  readonly [dimension: string]: MemberRevealsPolicy<Col, A>;
123
136
  };
137
+ /** Relation → the access view that lists the rows it reaches: the escape path, decided by the view's SQL. */
138
+ readonly access?: {
139
+ readonly [relation: string]: AccessPolicy;
140
+ };
124
141
  }
125
142
  /**
126
143
  * The scope of the dimension the policy states (`S`, its scope as written); for any other name, the dimension alone,
@@ -202,14 +219,20 @@ interface ReadOnlyFields<Col extends string, A extends string> {
202
219
  };
203
220
  }
204
221
  /**
205
- * A view read as a child table: its parent's `Ref` and `Within` read it; nothing writes it (ADR-0072). Its `when` narrows
206
- * its read actions; a path may start at its parent column, to the parent row.
222
+ * A view read as a child table: its parent's `Ref` and `Within` read it; nothing writes it (ADR-0072). Its `when` and
223
+ * `relationWhen` narrow its read actions; a path may start at its parent column, to the parent row.
207
224
  */
208
225
  type AuthoredChildViewPolicy<Table extends string, Col extends string, A extends string, S, P> = ReadOnlyFields<Col, A> & {
209
226
  readonly scope: ChildScopes<Table, Col, S, P>;
210
227
  readonly when?: {
211
228
  readonly [action in A]?: ConditionPolicy<Col>;
212
229
  };
230
+ /** Relation → action → the condition a row the relation reaches through the parent row must meet for it. */
231
+ readonly relationWhen?: {
232
+ readonly [relation: string]: {
233
+ readonly [action in A]?: ConditionPolicy<Col>;
234
+ };
235
+ };
213
236
  };
214
237
  /** A view's policy as written: a read-only child, or a reference table every site reads. */
215
238
  export type VocabularyViewPolicy<Table extends string, Col extends string, A extends string, S = unknown, P = unknown> = S extends {