@ultimat3/entity 31.23.0 → 31.24.0
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/CLAUDE.md +2 -1
- package/README.md +64 -0
- package/package.json +5 -5
- package/src/all-truncated.ts +52 -8
- package/src/counter-observed.ts +10 -4
- package/src/counter.ts +85 -8
- package/src/define-driver.ts +2 -0
- package/src/describe.ts +29 -10
- package/src/driver-kit-types.ts +5 -0
- package/src/driver-scope.ts +23 -1
- package/src/entity-error.ts +5 -0
- package/src/entity.ts +8 -1
- package/src/index.ts +12 -1
- package/src/measures.ts +3 -1
- package/src/memory-lifecycle.ts +3 -2
- package/src/memory-match.ts +13 -0
- package/src/memory-repo.ts +9 -7
- package/src/memory-via.ts +78 -0
- package/src/pg-sql.ts +43 -2
- package/src/plan.ts +7 -1
- package/src/predicate-node.ts +148 -0
- package/src/predicate-screen.ts +79 -4
- package/src/query.ts +11 -0
- package/src/registry.ts +2 -0
- package/src/sealed-repo.ts +9 -0
- package/src/tenancy.ts +6 -1
- package/src/type-pins-node.ts +31 -0
- package/src/types.ts +16 -2
- package/src/via-scope.ts +73 -0
- package/src/write-where.ts +32 -2
package/src/types.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
// A column carries its TypeScript type in `$parse`, which is what lets the row type be derived
|
|
7
7
|
// from the column set instead of being written a second time as a hand-maintained schema.
|
|
8
8
|
|
|
9
|
+
import type { Expr } from './expr';
|
|
9
10
|
import type { PersonalDataKind } from './personal-data-kind';
|
|
10
11
|
|
|
11
12
|
/**
|
|
@@ -69,9 +70,22 @@ export interface ReferenceOptions {
|
|
|
69
70
|
* on the key, because the child already names its parent — a thunk on the parent naming the child
|
|
70
71
|
* would make the two declarations each other's type and neither could be inferred.
|
|
71
72
|
*/
|
|
72
|
-
readonly countedIn?:
|
|
73
|
+
readonly countedIn?: CountedIn;
|
|
73
74
|
}
|
|
74
75
|
|
|
76
|
+
/**
|
|
77
|
+
* What `countedIn` names: the parent integer alone, or with a `where` — a predicate over THIS
|
|
78
|
+
* (child) row, in the invariant language, so only the rows it holds over are counted. A thunk like
|
|
79
|
+
* `to`, resolved once both modules evaluated: `where: (): Expr => whereColumns(likes).kind.eq('up')` —
|
|
80
|
+
* the `: Expr` is required, or TypeScript reads the entity naming itself in its own initializer
|
|
81
|
+
* as a cycle (TS7022).
|
|
82
|
+
* Compiled once into the trigger's condition and the recount's truth; the memory driver evaluates
|
|
83
|
+
* the same `Expr`. A NULL operand is not counted, as a SQL `where` decides it.
|
|
84
|
+
*/
|
|
85
|
+
export type CountedIn =
|
|
86
|
+
| (() => AnyColumn)
|
|
87
|
+
| { readonly to: () => AnyColumn; readonly where?: (() => Expr) | undefined };
|
|
88
|
+
|
|
75
89
|
/**
|
|
76
90
|
* The single value a money column puts on the row. Two physical columns back it.
|
|
77
91
|
*
|
|
@@ -188,7 +202,7 @@ export interface ColumnMeta {
|
|
|
188
202
|
* `references(() => posts.id, { countedIn: () => posts.likeCount })`: the parent integer the
|
|
189
203
|
* database keeps equal to the number of rows pointing there. `counter.ts` reads it.
|
|
190
204
|
*/
|
|
191
|
-
readonly countedIn?:
|
|
205
|
+
readonly countedIn?: CountedIn;
|
|
192
206
|
/**
|
|
193
207
|
* `json(schema)`'s schema, beside the `$parse` that validates through it, so a reader that must
|
|
194
208
|
* PRODUCE a storable value (`@ultimat3/testing`'s seed values) draws from the declaration rather
|
package/src/via-scope.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// Single responsibility: tenancy INSIDE a `via` hop. The subselect reads another entity's table, so
|
|
2
|
+
// it is planned through that entity's own `scopedPlan` — the acting actor's tenant applied, or the
|
|
3
|
+
// plan refused — exactly as a top-level read of it would be. A hop is the one way a read reaches a
|
|
4
|
+
// second table, and a second table read without its guard is a cross-tenant read.
|
|
5
|
+
|
|
6
|
+
import type { EntityCore } from './entity';
|
|
7
|
+
import { tenancyUnscoped } from './errors';
|
|
8
|
+
import { isGroup, orMembers, type ViaValue, viaValue } from './predicate-node';
|
|
9
|
+
import { viaTarget } from './predicate-screen';
|
|
10
|
+
import { getEntity } from './registry';
|
|
11
|
+
import { type Predicate, type QueryPlan, scopedPlan } from './tenancy';
|
|
12
|
+
|
|
13
|
+
/** Every `via` value this file planned. A driver compiles no other: one built by hand is refused. */
|
|
14
|
+
const scoped = new WeakSet<ViaValue>();
|
|
15
|
+
|
|
16
|
+
/** Whether `value` went through `scopeVias` — the drivers' check before they compile a hop. */
|
|
17
|
+
export const isScopedVia = (value: unknown): boolean =>
|
|
18
|
+
typeof value === 'object' && value !== null && scoped.has(value as ViaValue);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* An untenanted target behind a tenanted source must say why it belongs to no tenant — `shared:`
|
|
22
|
+
* (it hangs off a tenant's row) or `global:` (it is every tenant's alike), the two stances
|
|
23
|
+
* `describe.ts`'s `assertShared` accepts. Otherwise the hop is an unscoped read reached from inside
|
|
24
|
+
* a tenant, and it is refused as one.
|
|
25
|
+
*/
|
|
26
|
+
const assertReachable = (source: EntityCore, target: EntityCore, operation: string): void => {
|
|
27
|
+
if (source.$tenantColumn === null || target.$tenantColumn !== null) return;
|
|
28
|
+
const described = getEntity(target.$name)?.describe();
|
|
29
|
+
if (described?.shared !== undefined || described?.global !== undefined) return;
|
|
30
|
+
throw tenancyUnscoped(target.$name, `${operation} via ${source.$name}`);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const scopeLevel = (
|
|
34
|
+
source: EntityCore,
|
|
35
|
+
where: readonly Predicate[],
|
|
36
|
+
operation: string,
|
|
37
|
+
): readonly Predicate[] =>
|
|
38
|
+
where.map((predicate) => {
|
|
39
|
+
if (isGroup(predicate)) {
|
|
40
|
+
return { ...predicate, value: scopeLevel(source, orMembers(predicate), operation) };
|
|
41
|
+
}
|
|
42
|
+
if (predicate.op !== 'via') return predicate;
|
|
43
|
+
const target = viaTarget(source, predicate);
|
|
44
|
+
assertReachable(source, target, operation);
|
|
45
|
+
const hop = viaValue(predicate);
|
|
46
|
+
const inner: QueryPlan = {
|
|
47
|
+
entity: target.$name,
|
|
48
|
+
where: scopeLevel(target, hop.where, operation),
|
|
49
|
+
orderBy: [],
|
|
50
|
+
limit: 1,
|
|
51
|
+
};
|
|
52
|
+
const value: ViaValue = {
|
|
53
|
+
...hop,
|
|
54
|
+
where: scopedPlan(target.$name, target.$tenantColumn, `${operation} via`, inner).where,
|
|
55
|
+
};
|
|
56
|
+
scoped.add(value);
|
|
57
|
+
return { ...predicate, value };
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* `plan` with every hop scoped to its target's tenant, at every depth. Called by `readPlan` after
|
|
62
|
+
* the source's own scoping, so every read, count, aggregate and filtered write carries it.
|
|
63
|
+
*/
|
|
64
|
+
export const scopeVias = <Row>(
|
|
65
|
+
entity: EntityCore<Row>,
|
|
66
|
+
plan: QueryPlan,
|
|
67
|
+
operation: string,
|
|
68
|
+
): QueryPlan => {
|
|
69
|
+
if (!plan.where.some(hasNode)) return plan;
|
|
70
|
+
return { ...plan, where: scopeLevel(entity as EntityCore, plan.where, operation) };
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const hasNode = (predicate: Predicate): boolean => isGroup(predicate) || predicate.op === 'via';
|
package/src/write-where.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import type { EntityCore } from './entity';
|
|
7
7
|
import { describeCommand, EntityError, patchEmpty, writeUnfiltered } from './errors';
|
|
8
8
|
import { namedColumns, readPlan } from './plan';
|
|
9
|
+
import { isGroup, orMembers } from './predicate-node';
|
|
9
10
|
import type { FilteredWriteOptions } from './repo';
|
|
10
11
|
import { SEARCH_PROPERTY } from './search';
|
|
11
12
|
import type { Predicate, QueryPlan } from './tenancy';
|
|
@@ -39,6 +40,33 @@ const boundedBy = <Row>(
|
|
|
39
40
|
const verbOf = (op: string): 'updateAll' | 'deleteAll' =>
|
|
40
41
|
op.startsWith('update') ? 'updateAll' : 'deleteAll';
|
|
41
42
|
|
|
43
|
+
/**
|
|
44
|
+
* The first predicate with no value, at any depth of a group. A `via` is not walked: its inner level
|
|
45
|
+
* is a READ of another table, whose `undefined` cannot widen this write's rows past the hop.
|
|
46
|
+
*/
|
|
47
|
+
const firstUnnamed = (where: readonly Predicate[]): Predicate | undefined => {
|
|
48
|
+
for (const predicate of where) {
|
|
49
|
+
if (isGroup(predicate)) {
|
|
50
|
+
const inner = firstUnnamed(orMembers(predicate));
|
|
51
|
+
if (inner !== undefined) return inner;
|
|
52
|
+
} else if (!named(predicate)) {
|
|
53
|
+
return predicate;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return undefined;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Whether a predicate holds for EVERY row — bounds nothing. An empty `and` does, an `and` of only
|
|
61
|
+
* such groups does, and an `or` with any such arm does (the arm admits every row). An empty `or`
|
|
62
|
+
* admits none, a leaf or a `via` bounds: neither is unbounded.
|
|
63
|
+
*/
|
|
64
|
+
const boundsNothing = (predicate: Predicate): boolean => {
|
|
65
|
+
if (predicate.op === 'and') return orMembers(predicate).every(boundsNothing);
|
|
66
|
+
if (predicate.op === 'or') return orMembers(predicate).some(boundsNothing);
|
|
67
|
+
return false;
|
|
68
|
+
};
|
|
69
|
+
|
|
42
70
|
/**
|
|
43
71
|
* A chain predicate written with `undefined` is refused, not dropped. Dropped, `.where({ orgId,
|
|
44
72
|
* postId }).deleteAll()` on a `postId` that came back undefined deletes every row the org has —
|
|
@@ -50,7 +78,7 @@ const chainUndefined = <Row>(
|
|
|
50
78
|
op: string,
|
|
51
79
|
where: readonly Predicate[],
|
|
52
80
|
): EntityError | undefined => {
|
|
53
|
-
const missing = where
|
|
81
|
+
const missing = firstUnnamed(where);
|
|
54
82
|
if (missing === undefined) return undefined;
|
|
55
83
|
const verb = verbOf(op);
|
|
56
84
|
return new EntityError({
|
|
@@ -89,7 +117,9 @@ export const writeWherePlan = <Row>(
|
|
|
89
117
|
const refused = chainUndefined(entity, op, options?.where ?? []);
|
|
90
118
|
if (refused !== undefined) throw refused;
|
|
91
119
|
const where = boundedBy(filter, options);
|
|
92
|
-
|
|
120
|
+
// A tree that holds for every row is no filter: `andWhereNode({ and: [] }).deleteAll()` would
|
|
121
|
+
// reach the tenant's whole table exactly as an empty chain would, so it is refused as one.
|
|
122
|
+
if (where.every(boundsNothing)) throw unfiltered(entity, op, options?.where !== undefined);
|
|
93
123
|
const { where: _chain, ...rest } = options ?? {};
|
|
94
124
|
return readPlan(entity, { ...rest, where }, op);
|
|
95
125
|
};
|