@fabrika/db 0.9.0-dev.46 → 0.9.0-dev.48

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabrika/db",
3
- "version": "0.9.0-dev.46",
3
+ "version": "0.9.0-dev.48",
4
4
  "type": "module",
5
5
  "sideEffects": [
6
6
  "./src/testing.ts",
@@ -96,9 +96,9 @@
96
96
  "test": "bun test"
97
97
  },
98
98
  "dependencies": {
99
- "@fabrika/auth-core": "0.9.0-dev.46",
100
- "@fabrika/platform": "0.9.0-dev.46",
101
- "@fabrika/platform-node": "0.9.0-dev.46",
99
+ "@fabrika/auth-core": "0.9.0-dev.48",
100
+ "@fabrika/platform": "0.9.0-dev.48",
101
+ "@fabrika/platform-node": "0.9.0-dev.48",
102
102
  "kysely": "0.29.6",
103
103
  "kysely-postgres-js": "5.0.1",
104
104
  "zod": "^3.25.76"
package/src/orm-types.ts CHANGED
@@ -10,7 +10,6 @@ import type {
10
10
  ActionOf,
11
11
  ApplicationTableName,
12
12
  ChildTableName,
13
- ContainerOf,
14
13
  DimensionOf,
15
14
  DimensionTableName,
16
15
  InsertValues,
@@ -113,7 +112,7 @@ export interface IndexedTable {
113
112
  readonly chain: string
114
113
  /** The dimensions whose `Ref` reaches the table: a dimension table's own and every dimension of its chain. */
115
114
  readonly refs: string
116
- /** The dimensions whose `Within` lists the table: every dimension of its chain, and a top-level table's own. */
115
+ /** The dimensions whose `Within` lists the table: every dimension of its chain, and a dimension table's own. */
117
116
  readonly reach: string
118
117
  readonly versioned: boolean
119
118
  /** A queue table: its rows are claimed and finished through `row.ref`. */
@@ -176,8 +175,8 @@ type ChainDimensions<P, T, V> = PolicyOf<P, T> extends { readonly scope: infer S
176
175
  : never
177
176
  : never
178
177
 
179
- /** The dimensions whose `Within` lists table `T`: every dimension of its chain, and a top-level table's own. */
180
- type ReachOf<P, T, V> = [ContainerOf<P, T>] extends [never] ? DimensionOf<P, T> | ChainDimensions<P, T, V> : ChainDimensions<P, T, V>
178
+ /** The dimensions whose `Within` lists table `T`: every dimension of its chain, and a dimension table's own. */
179
+ type ReachOf<P, T, V> = DimensionOf<P, T> | ChainDimensions<P, T, V>
181
180
 
182
181
  /**
183
182
  * The dimensions outside its chain whose `Within` reads table `T` through a foreign key (`readThrough`); a child view's
@@ -216,7 +215,7 @@ export type DbIndex<DB, P, V = unknown> = {
216
215
  /** Dimension → the dimension tables listed in it: those it contains, and a top-level table its own. */
217
216
  readonly listed: { readonly [T in DimensionTableName<P> as ListDimensionOf<P, T>]: T }
218
217
  /**
219
- * Dimension → the tables an `over` of it lists: each whose containment chain holds it, a top-level table its own, and
218
+ * Dimension → the tables an `over` of it lists: each whose containment chain holds it, a dimension table its own, and
220
219
  * each that reads through it (`readThrough`).
221
220
  */
222
221
  readonly within: { readonly [T in DimensionTableName<P> | ChildTableName<P> as ReachOf<P, T, V> | ReadThroughOf<P, T>]: T }
@@ -533,7 +532,7 @@ type ListRefusal<T extends string, E extends IndexedTable, A extends string> = `
533
532
 
534
533
  /**
535
534
  * `ctx.db.within` of an `over` endpoint: every table whose containment chain holds its dimension (tasks two levels
536
- * down through their stamped `org_id`), and a top-level table its own; child tables included.
535
+ * down through their stamped `org_id`), and a dimension table its own; child tables included.
537
536
  */
538
537
  export type WithinTables<Ix extends AnyDbIndex, D extends string, A extends string, K extends DataKind> = {
539
538
  readonly [T in WithinIn<Ix, D>]: WithinRows<T, Ix['tables'][T], A, K>
package/src/store.ts CHANGED
@@ -747,37 +747,12 @@ export function childListPredicate(context: TableContext, capability: unknown, o
747
747
  }
748
748
 
749
749
  /**
750
- * The column that places a row of `model` in `dimension`: the column of that link of its containment chain, or a
751
- * top-level table's own key; `undefined` when no `Within` of the dimension reaches the table.
752
- */
753
- export function reachColumn(model: TableModel, dimension: string): string | undefined {
754
- if (model.scope.mode === 'dimension' && model.scope.container === undefined && model.scope.dimension === dimension) {
755
- return model.scope.key
756
- }
757
- for (let index = 0; index < model.chain.length; index++) {
758
- const link = model.chain[index]
759
- if (link?.dimension === dimension) {
760
- return link.column
761
- }
762
- }
763
- return undefined
764
- }
765
-
766
- /**
767
- * Whether a `Within` of `dimension` lists the table: through its containment chain or a secondary path, and a child
768
- * view through its parent's, its parent's own dimension included.
750
+ * Whether a `Within` of `dimension` lists the table: a dimension table its own dimension's, then through its containment
751
+ * chain or a secondary path; a child view through its parent's.
769
752
  */
770
753
  export function reachedBy(context: TableContext, dimension: string): boolean {
771
754
  const model = context.through?.context.model ?? context.model
772
- return parentColumn(model, context.through !== undefined, dimension) !== undefined || secondaryPathsOf(model, dimension).length > 0
773
- }
774
-
775
- /** The column that places a row of `model` in `dimension`, a view's parent's own key among them (`own`). */
776
- function parentColumn(model: TableModel, own: boolean, dimension: string): string | undefined {
777
- if (own && model.scope.mode === 'dimension' && model.scope.dimension === dimension) {
778
- return model.scope.key
779
- }
780
- return reachColumn(model, dimension)
755
+ return nodeColumnOf(model, dimension) !== undefined || secondaryPathsOf(model, dimension).length > 0
781
756
  }
782
757
 
783
758
  /** The secondary read paths of `model` that reach `dimension`, a dimension outside its chain: at their node or above it. */
@@ -811,14 +786,14 @@ export function reachPredicate(context: TableContext, capability: unknown): Oper
811
786
  )
812
787
  }
813
788
  if (context.through !== undefined) {
814
- return throughParent(context, context.through, grant, (parent, part) => withinReach(parent, part, true))
789
+ return throughParent(context, context.through, grant, withinReach)
815
790
  }
816
- return withinReach(context, grant, false)
791
+ return withinReach(context, grant)
817
792
  }
818
793
 
819
794
  /** A `Within`'s read of a table it reaches: through the column that places a row in its dimension, or its secondary paths. */
820
- function withinReach(context: TableContext, grant: WithinGrant, own: boolean): OperationNode | undefined {
821
- const column = parentColumn(context.model, own, grant.dimension)
795
+ function withinReach(context: TableContext, grant: WithinGrant): OperationNode | undefined {
796
+ const column = nodeColumnOf(context.model, grant.dimension)
822
797
  if (column !== undefined) {
823
798
  return withinPredicate(context, column, grant, 'read')
824
799
  }
@@ -972,7 +947,7 @@ export function scopeTerms(context: TableContext, capability: unknown, use: Scop
972
947
  }
973
948
  return [tests]
974
949
  }
975
- const column = reachColumn(model, grant.dimension)
950
+ const column = nodeColumnOf(model, grant.dimension)
976
951
  if (column === undefined) {
977
952
  throw new CapabilityError(
978
953
  'out_of_reach',
@@ -1324,7 +1299,7 @@ export function claimScope(
1324
1299
  throw new CapabilityError('table_kind', `${model.name} is an application table: claim it through ctx.db.app`)
1325
1300
  }
1326
1301
  checkAction(context, 'update', grant.action)
1327
- const column = reachColumn(model, grant.dimension)
1302
+ const column = nodeColumnOf(model, grant.dimension)
1328
1303
  if (column === undefined) {
1329
1304
  throw new CapabilityError(
1330
1305
  'out_of_reach',
@@ -2,7 +2,7 @@ import type { Selectable, Updateable } from 'kysely';
2
2
  import type { RowRef } from './capability';
3
3
  import type { ComparableColumn, ConditionTuple } from './description';
4
4
  import type { FileReference } from './file-references';
5
- import type { ActionOf, ApplicationTableName, ChildTableName, ContainerOf, DimensionOf, DimensionTableName, InsertValues, ListDimensionOf, ParentOf, ReadsAny, Tables, UpdateValues } from './policy';
5
+ import type { ActionOf, ApplicationTableName, ChildTableName, DimensionOf, DimensionTableName, InsertValues, ListDimensionOf, ParentOf, ReadsAny, Tables, UpdateValues } from './policy';
6
6
  import type { AggregateRow, AggregateSpec } from './store';
7
7
  import type { VocabularyContainerOf } from './vocabulary-tables';
8
8
  /** A member an endpoint may not use. It has no call signature, so a call fails at its line with this message. */
@@ -105,7 +105,7 @@ export interface IndexedTable {
105
105
  readonly chain: string;
106
106
  /** The dimensions whose `Ref` reaches the table: a dimension table's own and every dimension of its chain. */
107
107
  readonly refs: string;
108
- /** The dimensions whose `Within` lists the table: every dimension of its chain, and a top-level table's own. */
108
+ /** The dimensions whose `Within` lists the table: every dimension of its chain, and a dimension table's own. */
109
109
  readonly reach: string;
110
110
  readonly versioned: boolean;
111
111
  /** A queue table: its rows are claimed and finished through `row.ref`. */
@@ -182,8 +182,8 @@ type ChainDimensions<P, T, V> = PolicyOf<P, T> extends {
182
182
  } ? C | ([OwnerTable<P, C>] extends [never] ? VocabularyAncestors<V, C> : ChainDimensions<P, OwnerTable<P, C>, V>) : S extends {
183
183
  readonly child: infer Parent extends string;
184
184
  } ? DimensionOf<P, Parent> | ChainDimensions<P, Parent, V> : never : never;
185
- /** The dimensions whose `Within` lists table `T`: every dimension of its chain, and a top-level table's own. */
186
- type ReachOf<P, T, V> = [ContainerOf<P, T>] extends [never] ? DimensionOf<P, T> | ChainDimensions<P, T, V> : ChainDimensions<P, T, V>;
185
+ /** The dimensions whose `Within` lists table `T`: every dimension of its chain, and a dimension table's own. */
186
+ type ReachOf<P, T, V> = DimensionOf<P, T> | ChainDimensions<P, T, V>;
187
187
  /**
188
188
  * The dimensions outside its chain whose `Within` reads table `T` through a foreign key (`readThrough`); a child view's
189
189
  * are its parent's, as it is read through its parent row.
@@ -238,7 +238,7 @@ export type DbIndex<DB, P, V = unknown> = {
238
238
  readonly [T in DimensionTableName<P> as ListDimensionOf<P, T>]: T;
239
239
  };
240
240
  /**
241
- * Dimension → the tables an `over` of it lists: each whose containment chain holds it, a top-level table its own, and
241
+ * Dimension → the tables an `over` of it lists: each whose containment chain holds it, a dimension table its own, and
242
242
  * each that reads through it (`readThrough`).
243
243
  */
244
244
  readonly within: {
@@ -489,7 +489,7 @@ type WithinRead<T extends string, E extends IndexedTable, A extends string, Memb
489
489
  type ListRefusal<T extends string, E extends IndexedTable, A extends string> = `a list of ${T} needs its read action '${E['actions']['read'] & string}', and this endpoint holds '${A}'`;
490
490
  /**
491
491
  * `ctx.db.within` of an `over` endpoint: every table whose containment chain holds its dimension (tasks two levels
492
- * down through their stamped `org_id`), and a top-level table its own; child tables included.
492
+ * down through their stamped `org_id`), and a dimension table its own; child tables included.
493
493
  */
494
494
  export type WithinTables<Ix extends AnyDbIndex, D extends string, A extends string, K extends DataKind> = {
495
495
  readonly [T in WithinIn<Ix, D>]: WithinRows<T, Ix['tables'][T], A, K>;
@@ -94,13 +94,8 @@ export declare function dimensionListPredicate(context: TableContext, capability
94
94
  export declare function parentPredicate(context: TableContext, grant: RefGrant, key?: string): OperationNode;
95
95
  export declare function childListPredicate(context: TableContext, capability: unknown, operation: Operation): OperationNode | undefined;
96
96
  /**
97
- * The column that places a row of `model` in `dimension`: the column of that link of its containment chain, or a
98
- * top-level table's own key; `undefined` when no `Within` of the dimension reaches the table.
99
- */
100
- export declare function reachColumn(model: TableModel, dimension: string): string | undefined;
101
- /**
102
- * Whether a `Within` of `dimension` lists the table: through its containment chain or a secondary path, and a child
103
- * view through its parent's, its parent's own dimension included.
97
+ * Whether a `Within` of `dimension` lists the table: a dimension table its own dimension's, then through its containment
98
+ * chain or a secondary path; a child view through its parent's.
104
99
  */
105
100
  export declare function reachedBy(context: TableContext, dimension: string): boolean;
106
101
  /** The secondary read paths of `model` that reach `dimension`, a dimension outside its chain: at their node or above it. */