@ultimat3/entity 31.47.1 → 31.49.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 +5 -0
- package/README.md +11 -4
- package/package.json +6 -5
- package/src/aggregate.ts +4 -3
- package/src/batch-read.ts +62 -4
- package/src/bulk-write.ts +1 -19
- package/src/coalesce.ts +3 -2
- package/src/columns.ts +5 -2
- package/src/cross-tenant.ts +2 -1
- package/src/entity-patch.ts +3 -0
- package/src/errors.ts +34 -8
- package/src/index.ts +1 -0
- package/src/instant-range.ts +38 -0
- package/src/instant.ts +14 -0
- package/src/jit-preload.ts +3 -2
- package/src/memory-journal.ts +8 -0
- package/src/memory-repo.ts +9 -6
- package/src/memory-tx-entry.ts +5 -0
- package/src/pattern-portability.ts +60 -8
- package/src/pg-sql-aggregate.ts +26 -4
- package/src/pg-sql-in.ts +44 -0
- package/src/pg-sql-measures.ts +2 -2
- package/src/pg-sql.ts +7 -23
- package/src/predicate-operand.ts +109 -11
- package/src/row-observer.ts +71 -4
- package/src/search-repo.ts +36 -16
- package/src/seed.ts +15 -2
- package/src/uuid-text.ts +22 -0
package/src/search-repo.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
// The ONE search path: a chain's `.search(term)` asks the INSTALLED `SearchIndex`. The built-in
|
|
2
2
|
// Postgres index answers inside the statement (its `matches` predicate, today's SQL, untouched);
|
|
3
|
-
// any other index is asked for ids first
|
|
4
|
-
// tenancy, soft delete, order and cursor
|
|
3
|
+
// any other index is asked for ids first — per tenant the chain names — and the chain then reads
|
|
4
|
+
// those rows with its own tenancy, soft delete, order and cursor: a hit never widens what it sees.
|
|
5
5
|
|
|
6
6
|
import { tryUseContext } from '@ultimat3/core';
|
|
7
|
+
import { crossTenantReason } from './cross-tenant';
|
|
7
8
|
import type { EntityCore } from './entity';
|
|
8
9
|
import { EntityError } from './errors';
|
|
9
10
|
import { MAX_PAGE_SIZE } from './plan';
|
|
@@ -16,13 +17,28 @@ import type { Predicate } from './tenancy';
|
|
|
16
17
|
const isMatch = (predicate: Predicate): boolean =>
|
|
17
18
|
predicate.column === SEARCH_PROPERTY && predicate.op === 'matches';
|
|
18
19
|
|
|
19
|
-
/**
|
|
20
|
-
|
|
20
|
+
/**
|
|
21
|
+
* The tenants the index is asked for: the chain's own `eq` on the tenant column, else — under
|
|
22
|
+
* `crossTenant` only — each tenant of its `in` list, else the actor's. Never the actor's under a
|
|
23
|
+
* lifted guard: the chain reads every tenant there, and the actor's org alone is a silent sample.
|
|
24
|
+
*/
|
|
25
|
+
const tenantsOf = (entity: EntityCore<unknown>, where: readonly Predicate[]): (string | null)[] => {
|
|
21
26
|
const column = entity.$tenantColumn;
|
|
22
|
-
if (column === null) return null;
|
|
27
|
+
if (column === null) return [null];
|
|
23
28
|
const named = where.find((each) => each.column === column && each.op === 'eq');
|
|
24
|
-
if (typeof named?.value === 'string') return named.value;
|
|
25
|
-
|
|
29
|
+
if (typeof named?.value === 'string') return [named.value];
|
|
30
|
+
const reason = crossTenantReason();
|
|
31
|
+
if (reason === undefined) return [tryUseContext()?.actor.orgId ?? null];
|
|
32
|
+
const listed = where.find((each) => each.column === column && each.op === 'in');
|
|
33
|
+
const tenants = Array.isArray(listed?.value) ? listed.value : undefined;
|
|
34
|
+
if (tenants?.every((tenant): tenant is string => typeof tenant === 'string')) {
|
|
35
|
+
return [...new Set(tenants)];
|
|
36
|
+
}
|
|
37
|
+
throw new EntityError({
|
|
38
|
+
code: 'X_INVARIANT_VIOLATED',
|
|
39
|
+
cause: `${entity.$name}.search(<term>) ran inside crossTenant('${reason}') through an external search index, which is asked one tenant at a time — and the chain names no ${column} to ask for`,
|
|
40
|
+
fix: `${entity.$name}.andWhere('${column}', 'in', [<orgId>, …]).search('<term>') # or installSearchIndex(postgresSearchIndex()), which matches every tenant inside the statement`,
|
|
41
|
+
});
|
|
26
42
|
};
|
|
27
43
|
|
|
28
44
|
/** The single key a hit's id is read back by; a composite key has no `in` an index id can name. */
|
|
@@ -58,21 +74,25 @@ const resolved = async (entity: EntityCore<unknown>, args: FindManyArgs): Promis
|
|
|
58
74
|
const where = args.where ?? [];
|
|
59
75
|
const index = searchIndex();
|
|
60
76
|
if (answersInStatement(index)) return args;
|
|
61
|
-
const
|
|
77
|
+
const tenants = tenantsOf(entity, where);
|
|
62
78
|
const swapped: Predicate[] = [];
|
|
63
79
|
for (const predicate of where) {
|
|
64
80
|
if (!isMatch(predicate)) {
|
|
65
81
|
swapped.push(predicate);
|
|
66
82
|
continue;
|
|
67
83
|
}
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
84
|
+
const ids: string[] = [];
|
|
85
|
+
for (const tenant of tenants) {
|
|
86
|
+
const hits = await index.query({
|
|
87
|
+
entity: entity.$name,
|
|
88
|
+
tenant,
|
|
89
|
+
term: String(predicate.value ?? ''),
|
|
90
|
+
limit: MAX_PAGE_SIZE,
|
|
91
|
+
});
|
|
92
|
+
if (hits.length >= MAX_PAGE_SIZE) throw tooManyHits(entity, index.name);
|
|
93
|
+
ids.push(...hits.map((hit) => hit.id));
|
|
94
|
+
}
|
|
95
|
+
swapped.push({ column: keyOf(entity), op: 'in', value: ids });
|
|
76
96
|
}
|
|
77
97
|
return { ...args, where: swapped };
|
|
78
98
|
};
|
package/src/seed.ts
CHANGED
|
@@ -188,16 +188,29 @@ export const isSeed = (value: unknown): value is Seed =>
|
|
|
188
188
|
const cellOf = (row: unknown, property: string): unknown =>
|
|
189
189
|
(row as Readonly<Record<string, unknown>>)[property];
|
|
190
190
|
|
|
191
|
+
/** A plain object's keys in one order: jsonb hands an object back in ITS order, not the seed's. */
|
|
192
|
+
const sortedKeys = (_key: string, value: unknown): unknown => {
|
|
193
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) return value;
|
|
194
|
+
const prototype = Object.getPrototypeOf(value);
|
|
195
|
+
if (prototype !== Object.prototype && prototype !== null) return value;
|
|
196
|
+
return Object.fromEntries(
|
|
197
|
+
Object.entries(value as Record<string, unknown>).sort(([a], [b]) =>
|
|
198
|
+
a < b ? -1 : a > b ? 1 : 0,
|
|
199
|
+
),
|
|
200
|
+
);
|
|
201
|
+
};
|
|
202
|
+
|
|
191
203
|
/**
|
|
192
204
|
* Two cells, as a replay has to compare them: a `Date` is not `===` a `Date` and money is an
|
|
193
|
-
* object, so both are compared by value
|
|
205
|
+
* object, so both are compared by value — an object's keys in any order, an array's in its own —
|
|
206
|
+
* and everything else by identity.
|
|
194
207
|
*/
|
|
195
208
|
const sameCell = (left: unknown, right: unknown): boolean => {
|
|
196
209
|
if (left instanceof Date || right instanceof Date) {
|
|
197
210
|
return left instanceof Date && right instanceof Date && left.getTime() === right.getTime();
|
|
198
211
|
}
|
|
199
212
|
if (typeof left === 'object' && left !== null && typeof right === 'object' && right !== null) {
|
|
200
|
-
return JSON.stringify(left) === JSON.stringify(right);
|
|
213
|
+
return JSON.stringify(left, sortedKeys) === JSON.stringify(right, sortedKeys);
|
|
201
214
|
}
|
|
202
215
|
return left === right;
|
|
203
216
|
};
|
package/src/uuid-text.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Single responsibility: the text Postgres parses as a `uuid`, and the one spelling it prints —
|
|
2
|
+
// so an operand no `uuid` column can hold is known before a statement carries it, and a spelling
|
|
3
|
+
// the server would have matched is the same value to both drivers.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* `uuid_in`'s grammar: thirty-two hex digits, a hyphen allowed after any group of four but the
|
|
7
|
+
* last, the whole optionally in braces. No blanks — the server refuses a padded value.
|
|
8
|
+
*/
|
|
9
|
+
const UUID_TEXT = /^(?:[0-9a-f]{4}-?){7}[0-9a-f]{4}$/i;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The lower-cased `8-4-4-4-12` spelling of `value`, or `undefined` when it is not a `uuid`'s text.
|
|
13
|
+
* Bound as a parameter the latter is `22P02`, a 500 for an id straight out of a URL.
|
|
14
|
+
*/
|
|
15
|
+
export const canonicalUuid = (value: unknown): string | undefined => {
|
|
16
|
+
if (typeof value !== 'string') return undefined;
|
|
17
|
+
const braced = value.startsWith('{') && value.endsWith('}');
|
|
18
|
+
const text = braced ? value.slice(1, -1) : value;
|
|
19
|
+
if (!UUID_TEXT.test(text)) return undefined;
|
|
20
|
+
const hex = text.replaceAll('-', '').toLowerCase();
|
|
21
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
22
|
+
};
|