@ultimat3/entity 31.7.0 → 31.9.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 +1 -1
- package/README.md +30 -1
- package/package.json +5 -5
- package/src/adapter.ts +2 -2
- package/src/append-only-errors.ts +2 -1
- package/src/append-only.ts +16 -1
- package/src/batch-bytes.ts +34 -0
- package/src/bulk-write-send.ts +38 -5
- package/src/copy-plan.ts +40 -0
- package/src/declared-index.ts +3 -2
- package/src/describe.ts +3 -2
- package/src/driver-repo-reads.ts +5 -4
- package/src/driver-repo-writes.ts +81 -5
- package/src/entity-error.ts +2 -0
- package/src/index-identity.ts +13 -8
- package/src/index-init.ts +3 -0
- package/src/index.ts +6 -2
- package/src/memory-observed.ts +4 -0
- package/src/memory-repo.ts +43 -5
- package/src/memory-update.ts +10 -3
- package/src/page-bytes.ts +127 -0
- package/src/pg-bulk-repo.ts +163 -0
- package/src/pg-copy.ts +87 -0
- package/src/pg-driver.ts +18 -71
- package/src/pg-update-many-sql.ts +103 -0
- package/src/pg-values-list.ts +55 -0
- package/src/pg-write-sql.ts +10 -22
- package/src/plan.ts +1 -56
- package/src/query-write.ts +169 -0
- package/src/query.ts +24 -47
- package/src/registry.ts +4 -3
- package/src/repo.ts +40 -2
- package/src/row-observer.ts +39 -1
- package/src/sealed-repo.ts +48 -7
- package/src/search-mirrored-repo.ts +53 -0
- package/src/type-pins.ts +9 -0
- package/src/types.ts +5 -2
- package/src/update-many-send.ts +66 -0
- package/src/update-many.ts +170 -0
- package/src/view-repo.ts +4 -0
- package/src/write-where.ts +161 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// Single responsibility: the plan a FILTERED write runs — `updateWhere(filter, …)`, `deleteWhere(
|
|
2
|
+
// filter)` and a chain's `.updateAll(patch)`/`.deleteAll()` — built once, so the two spellings are one
|
|
3
|
+
// write. Every driver already executes any predicate a read plan holds; what a write adds is the
|
|
4
|
+
// refusal of the empty filter and of chain state (a page, a cursor, a search) it cannot honour.
|
|
5
|
+
|
|
6
|
+
import type { EntityCore } from './entity';
|
|
7
|
+
import { describeCommand, EntityError, patchEmpty, writeUnfiltered } from './errors';
|
|
8
|
+
import { namedColumns, readPlan } from './plan';
|
|
9
|
+
import type { FilteredWriteOptions } from './repo';
|
|
10
|
+
import { SEARCH_PROPERTY } from './search';
|
|
11
|
+
import type { Predicate, QueryPlan } from './tenancy';
|
|
12
|
+
import type { RowPatch } from './types';
|
|
13
|
+
|
|
14
|
+
/** What a chain carries into a write terminal. */
|
|
15
|
+
export interface WriteChain {
|
|
16
|
+
readonly where: readonly Predicate[];
|
|
17
|
+
readonly limit: number | undefined;
|
|
18
|
+
readonly cursor: string | null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A NULL test names its column and no value; every other predicate is only a filter with one. */
|
|
22
|
+
const named = (predicate: Predicate): boolean =>
|
|
23
|
+
predicate.op === 'is-null' || predicate.op === 'is-not-null' || predicate.value !== undefined;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The filter's equalities, then the chain's predicates. In the FILTER an `undefined` value is
|
|
27
|
+
* dropped, never bound — the rule `updateWhere` has always had, kept. On a CHAIN it is refused
|
|
28
|
+
* (`chainUndefined`), so it never reaches here.
|
|
29
|
+
*/
|
|
30
|
+
const boundedBy = <Row>(
|
|
31
|
+
filter: RowPatch<Row>,
|
|
32
|
+
options: FilteredWriteOptions | undefined,
|
|
33
|
+
): Predicate[] => [
|
|
34
|
+
...namedColumns(filter).map(([column, value]): Predicate => ({ column, op: 'eq', value })),
|
|
35
|
+
...(options?.where ?? []),
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
const verbOf = (op: string): 'updateAll' | 'deleteAll' =>
|
|
39
|
+
op.startsWith('update') ? 'updateAll' : 'deleteAll';
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* A chain predicate written with `undefined` is refused, not dropped. Dropped, `.where({ orgId,
|
|
43
|
+
* postId }).deleteAll()` on a `postId` that came back undefined deletes every row the org has —
|
|
44
|
+
* the remaining predicate still bounds it, so the empty-filter refusal never fires. A chain is
|
|
45
|
+
* written predicate by predicate; one with no value is a bug at that line, never an intent.
|
|
46
|
+
*/
|
|
47
|
+
const chainUndefined = <Row>(
|
|
48
|
+
entity: EntityCore<Row>,
|
|
49
|
+
op: string,
|
|
50
|
+
where: readonly Predicate[],
|
|
51
|
+
): EntityError | undefined => {
|
|
52
|
+
const missing = where.find((predicate) => !named(predicate));
|
|
53
|
+
if (missing === undefined) return undefined;
|
|
54
|
+
const verb = verbOf(op);
|
|
55
|
+
return new EntityError({
|
|
56
|
+
code: 'X_WRITE_UNFILTERED',
|
|
57
|
+
cause: `${entity.$name}.where(…).${verb}() — the predicate on ${missing.column} has an undefined value, and dropping it would widen the write to every row the rest of the chain matches`,
|
|
58
|
+
fix: `${entity.$name}.where({ ${missing.column} }).${verb}(${verb === 'updateAll' ? '…' : ''}) # guard ${missing.column} so it is defined before the write; for SQL NULL write .andWhere('${missing.column}', 'is-null')`,
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The fix spelled as the call the author wrote. `X_WRITE_UNFILTERED` is one situation whichever
|
|
64
|
+
* spelling hit it, so it stays one code; only the line to paste differs.
|
|
65
|
+
*/
|
|
66
|
+
const unfiltered = <Row>(entity: EntityCore<Row>, op: string, chained: boolean): EntityError => {
|
|
67
|
+
if (!chained) return writeUnfiltered(entity.$name, op, entity.$primaryKey);
|
|
68
|
+
const verb = verbOf(op);
|
|
69
|
+
return new EntityError({
|
|
70
|
+
code: 'X_WRITE_UNFILTERED',
|
|
71
|
+
cause: `${entity.$name}.where(…).${verb}() named no filter — an empty chain would reach every row`,
|
|
72
|
+
fix: `${entity.$name}.where({ ${entity.$primaryKey.join(', ')} }).${verb}(${verb === 'updateAll' ? '…' : ''}) # name the columns that bound it. A deliberate whole-table write is a migration: x db gen "<name>"`,
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The plan for a filtered delete. The empty filter is refused BEFORE tenancy runs: the org
|
|
78
|
+
* predicate the framework adds bounds the blast radius to one tenant, which is still every row
|
|
79
|
+
* that tenant has. Then the read's own plan — tenancy, operator screening — so a chain's
|
|
80
|
+
* `.deleteAll()` reaches exactly the rows the same chain's `.all()` would read, and no other.
|
|
81
|
+
*/
|
|
82
|
+
export const writeWherePlan = <Row>(
|
|
83
|
+
entity: EntityCore<Row>,
|
|
84
|
+
filter: RowPatch<Row>,
|
|
85
|
+
options: FilteredWriteOptions | undefined,
|
|
86
|
+
op: string,
|
|
87
|
+
): QueryPlan => {
|
|
88
|
+
const refused = chainUndefined(entity, op, options?.where ?? []);
|
|
89
|
+
if (refused !== undefined) throw refused;
|
|
90
|
+
const where = boundedBy(filter, options);
|
|
91
|
+
if (where.length === 0) throw unfiltered(entity, op, options?.where !== undefined);
|
|
92
|
+
const { where: _chain, ...rest } = options ?? {};
|
|
93
|
+
return readPlan(entity, { ...rest, where }, op);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The plan for a filtered update: the delete's, after the patch is judged non-empty — "4 rows
|
|
98
|
+
* updated" for a statement that set nothing is the answer nobody can act on. Filter first, so the
|
|
99
|
+
* order of the two refusals is the order of the arguments.
|
|
100
|
+
*/
|
|
101
|
+
export const updateWherePlan = <Row>(
|
|
102
|
+
entity: EntityCore<Row>,
|
|
103
|
+
filter: RowPatch<Row>,
|
|
104
|
+
patch: RowPatch<Row>,
|
|
105
|
+
options: FilteredWriteOptions | undefined,
|
|
106
|
+
op: string,
|
|
107
|
+
): QueryPlan => {
|
|
108
|
+
const plan = writeWherePlan(entity, filter, options, op);
|
|
109
|
+
if (namedColumns(patch).length > 0) return plan;
|
|
110
|
+
const columns = Object.keys(entity.$columns);
|
|
111
|
+
if (options?.where === undefined) throw patchEmpty(entity.$name, op, columns);
|
|
112
|
+
throw new EntityError({
|
|
113
|
+
code: 'X_PATCH_EMPTY',
|
|
114
|
+
cause: `${entity.$name}.where(…).updateAll() named no columns to write`,
|
|
115
|
+
fix: `${entity.$name}.where(filter).updateAll({ <column>: <value> }) # pick a column from: ${columns.join(', ')}`,
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const unwritable = (entityName: string, verb: string, what: string, instead: string) =>
|
|
120
|
+
new EntityError({
|
|
121
|
+
code: 'X_INVARIANT_VIOLATED',
|
|
122
|
+
cause: `${entityName}.where(…).${what}.${verb}() — ${instead}`,
|
|
123
|
+
fix: `${describeCommand(entityName)} # the entity whose write chain carries ${what}: drop it, or sweep with .where(filter).inBatches(1000) and write each batch by its keys`,
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A write has no page. `limit(n)` would read as "the first n" and `after(cursor)` as "past this
|
|
128
|
+
* row", and neither Postgres's UPDATE/DELETE nor any other driver's honours either — so a chain
|
|
129
|
+
* carrying one is refused rather than written in full. A `.search()` is refused too: an external
|
|
130
|
+
* index answers at most `MAX_PAGE_SIZE` ids, and a write over a sample of the matches is a write
|
|
131
|
+
* nobody asked for. The order, the projection and preloads change no row, and are ignored.
|
|
132
|
+
*/
|
|
133
|
+
export const assertWritableChain = (
|
|
134
|
+
entityName: string,
|
|
135
|
+
verb: 'updateAll' | 'deleteAll',
|
|
136
|
+
chain: WriteChain,
|
|
137
|
+
): void => {
|
|
138
|
+
if (chain.limit !== undefined) {
|
|
139
|
+
throw unwritable(entityName, verb, 'limit()', 'a write has no page and reaches every match');
|
|
140
|
+
}
|
|
141
|
+
if (chain.cursor !== null) {
|
|
142
|
+
throw unwritable(entityName, verb, 'after()', 'a write has no page and reaches every match');
|
|
143
|
+
}
|
|
144
|
+
if (chain.where.some((each) => each.column === SEARCH_PROPERTY)) {
|
|
145
|
+
throw unwritable(
|
|
146
|
+
entityName,
|
|
147
|
+
verb,
|
|
148
|
+
'search()',
|
|
149
|
+
'an external search index answers a bounded sample of ids, never every match',
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
/** Every column a filtered write is bounded by — what `sealed-repo.ts` refuses a sealed one from. */
|
|
155
|
+
export const predicateColumns = (
|
|
156
|
+
filter: unknown,
|
|
157
|
+
where: readonly Predicate[] | undefined,
|
|
158
|
+
): readonly string[] => [
|
|
159
|
+
...namedColumns(filter).map(([column]) => column),
|
|
160
|
+
...(where ?? []).map((each) => each.column),
|
|
161
|
+
];
|