effect-dynamodb 1.39.0 → 1.40.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/dist/Aggregate.d.ts +119 -15
- package/dist/Aggregate.d.ts.map +1 -1
- package/dist/Aggregate.js +2021 -379
- package/dist/Aggregate.js.map +1 -1
- package/dist/Batch.d.ts +9 -5
- package/dist/Batch.d.ts.map +1 -1
- package/dist/Batch.js +28 -44
- package/dist/Batch.js.map +1 -1
- package/dist/Collection.d.ts.map +1 -1
- package/dist/Collection.js +12 -5
- package/dist/Collection.js.map +1 -1
- package/dist/ConsumedCapacity.js +1 -1
- package/dist/ConsumedCapacity.js.map +1 -1
- package/dist/DynamoClient.d.ts +22 -8
- package/dist/DynamoClient.d.ts.map +1 -1
- package/dist/DynamoClient.js +243 -87
- package/dist/DynamoClient.js.map +1 -1
- package/dist/Entity.d.ts +87 -22
- package/dist/Entity.d.ts.map +1 -1
- package/dist/Entity.js +2750 -514
- package/dist/Entity.js.map +1 -1
- package/dist/EventStore.d.ts +37 -15
- package/dist/EventStore.d.ts.map +1 -1
- package/dist/EventStore.js +215 -79
- package/dist/EventStore.js.map +1 -1
- package/dist/Marshaller.d.ts +39 -9
- package/dist/Marshaller.d.ts.map +1 -1
- package/dist/Marshaller.js +421 -70
- package/dist/Marshaller.js.map +1 -1
- package/dist/Query.d.ts +23 -0
- package/dist/Query.d.ts.map +1 -1
- package/dist/Query.js +49 -3
- package/dist/Query.js.map +1 -1
- package/dist/StorageEncoding.d.ts +7 -1
- package/dist/StorageEncoding.d.ts.map +1 -1
- package/dist/StorageEncoding.js +8 -2
- package/dist/StorageEncoding.js.map +1 -1
- package/dist/Table.d.ts +69 -8
- package/dist/Table.d.ts.map +1 -1
- package/dist/Transaction.d.ts.map +1 -1
- package/dist/Transaction.js +10 -4
- package/dist/Transaction.js.map +1 -1
- package/dist/change-feed/ChangeFeed.d.ts +8 -3
- package/dist/change-feed/ChangeFeed.d.ts.map +1 -1
- package/dist/change-feed/ChangeFeed.js +76 -9
- package/dist/change-feed/ChangeFeed.js.map +1 -1
- package/dist/change-feed/CheckpointStore.js +1 -1
- package/dist/change-feed/CheckpointStore.js.map +1 -1
- package/dist/change-feed/DynamoStreamsClient.d.ts +12 -5
- package/dist/change-feed/DynamoStreamsClient.d.ts.map +1 -1
- package/dist/change-feed/DynamoStreamsClient.js +15 -9
- package/dist/change-feed/DynamoStreamsClient.js.map +1 -1
- package/dist/internal/BoundCrud.d.ts +40 -32
- package/dist/internal/BoundCrud.d.ts.map +1 -1
- package/dist/internal/BoundCrud.js.map +1 -1
- package/dist/internal/BoundQuery.d.ts +1 -1
- package/dist/internal/BoundQuery.d.ts.map +1 -1
- package/dist/internal/BoundQuery.js +22 -12
- package/dist/internal/BoundQuery.js.map +1 -1
- package/dist/internal/BoundVectorQuery.d.ts.map +1 -1
- package/dist/internal/BoundVectorQuery.js +2 -1
- package/dist/internal/BoundVectorQuery.js.map +1 -1
- package/dist/internal/Capacity.d.ts +44 -4
- package/dist/internal/Capacity.d.ts.map +1 -1
- package/dist/internal/Capacity.js +89 -15
- package/dist/internal/Capacity.js.map +1 -1
- package/dist/internal/ClientOptions.d.ts +72 -0
- package/dist/internal/ClientOptions.d.ts.map +1 -0
- package/dist/internal/ClientOptions.js +79 -0
- package/dist/internal/ClientOptions.js.map +1 -0
- package/dist/internal/Cursor.d.ts +19 -10
- package/dist/internal/Cursor.d.ts.map +1 -1
- package/dist/internal/Cursor.js +25 -10
- package/dist/internal/Cursor.js.map +1 -1
- package/dist/internal/EntityCombinators.d.ts +22 -7
- package/dist/internal/EntityCombinators.d.ts.map +1 -1
- package/dist/internal/EntityCombinators.js +6 -2
- package/dist/internal/EntityCombinators.js.map +1 -1
- package/dist/internal/EntityOps.d.ts +11 -1
- package/dist/internal/EntityOps.d.ts.map +1 -1
- package/dist/internal/EntityOps.js.map +1 -1
- package/dist/internal/EventProjection.d.ts +15 -3
- package/dist/internal/EventProjection.d.ts.map +1 -1
- package/dist/internal/EventProjection.js +27 -8
- package/dist/internal/EventProjection.js.map +1 -1
- package/dist/internal/Expr.d.ts.map +1 -1
- package/dist/internal/Expr.js +53 -2
- package/dist/internal/Expr.js.map +1 -1
- package/dist/internal/ExpressionLimits.d.ts +57 -1
- package/dist/internal/ExpressionLimits.d.ts.map +1 -1
- package/dist/internal/ExpressionLimits.js +90 -1
- package/dist/internal/ExpressionLimits.js.map +1 -1
- package/dist/internal/ItemSize.d.ts +108 -4
- package/dist/internal/ItemSize.d.ts.map +1 -1
- package/dist/internal/ItemSize.js +190 -3
- package/dist/internal/ItemSize.js.map +1 -1
- package/dist/internal/MultiProjection.d.ts +7 -2
- package/dist/internal/MultiProjection.d.ts.map +1 -1
- package/dist/internal/MultiProjection.js +9 -3
- package/dist/internal/MultiProjection.js.map +1 -1
- package/dist/internal/PathBuilder.d.ts +15 -4
- package/dist/internal/PathBuilder.d.ts.map +1 -1
- package/dist/internal/PathBuilder.js +20 -10
- package/dist/internal/PathBuilder.js.map +1 -1
- package/dist/internal/ProjectionEngine.d.ts +76 -2
- package/dist/internal/ProjectionEngine.d.ts.map +1 -1
- package/dist/internal/ProjectionEngine.js +358 -28
- package/dist/internal/ProjectionEngine.js.map +1 -1
- package/dist/internal/Rekey.d.ts +21 -1
- package/dist/internal/Rekey.d.ts.map +1 -1
- package/dist/internal/Rekey.js +67 -0
- package/dist/internal/Rekey.js.map +1 -1
- package/dist/internal/RoundTrip.d.ts +36 -0
- package/dist/internal/RoundTrip.d.ts.map +1 -0
- package/dist/internal/RoundTrip.js +79 -0
- package/dist/internal/RoundTrip.js.map +1 -0
- package/dist/internal/SentRequest.d.ts +14 -0
- package/dist/internal/SentRequest.d.ts.map +1 -0
- package/dist/internal/SentRequest.js +31 -0
- package/dist/internal/SentRequest.js.map +1 -0
- package/dist/internal/TransactWriteOps.d.ts +69 -15
- package/dist/internal/TransactWriteOps.d.ts.map +1 -1
- package/dist/internal/TransactWriteOps.js +144 -107
- package/dist/internal/TransactWriteOps.js.map +1 -1
- package/dist/internal/TransactableOps.d.ts +115 -23
- package/dist/internal/TransactableOps.d.ts.map +1 -1
- package/dist/internal/TransactableOps.js +244 -80
- package/dist/internal/TransactableOps.js.map +1 -1
- package/dist/internal/WriteDiagnosis.d.ts +31 -3
- package/dist/internal/WriteDiagnosis.d.ts.map +1 -1
- package/dist/internal/WriteDiagnosis.js +128 -11
- package/dist/internal/WriteDiagnosis.js.map +1 -1
- package/package.json +2 -2
|
@@ -16,14 +16,20 @@
|
|
|
16
16
|
*/
|
|
17
17
|
import { isConfiguredModel } from "@effect-dynamodb/schema/DynamoModel.js";
|
|
18
18
|
import { ConcurrentModification, isAwsConditionalCheckFailed, isAwsTransactionCancelled, ProjectionSourceConflict, ValidationError, } from "@effect-dynamodb/schema/Errors.js";
|
|
19
|
+
import { keyFormLiterals, keyFormValueKind, } from "@effect-dynamodb/schema/internal/CompositeCodec.js";
|
|
20
|
+
import { readingStreamValues, writingTuplePositions, } from "@effect-dynamodb/schema/internal/EntitySchemas.js";
|
|
21
|
+
import { decodingAsBefore } from "@effect-dynamodb/schema/internal/FailingAsBefore.js";
|
|
19
22
|
import { getSchemaFields } from "@effect-dynamodb/schema/internal/SchemaAccessors.js";
|
|
23
|
+
import * as KeyComposer from "@effect-dynamodb/schema/KeyComposer.js";
|
|
20
24
|
import { DateTime, Duration as Dur, Effect, Option, Random, Result, Schema as S, Schedule, } from "effect";
|
|
21
25
|
import { decodeMarshalledItem } from "../Entity.js";
|
|
22
26
|
import { fromAttributeMap, toAttributeMap } from "../Marshaller.js";
|
|
23
27
|
import { resolveTtlAttributeName } from "../Table.js";
|
|
24
28
|
import * as Capacity from "./Capacity.js";
|
|
25
29
|
import { condition as entityCondition } from "./EntityCombinators.js";
|
|
26
|
-
import {
|
|
30
|
+
import { handlerOf } from "./Rekey.js";
|
|
31
|
+
import { notReadBack, readBackOf } from "./RoundTrip.js";
|
|
32
|
+
import { addProjectionLayout, OUTBOX_CLAIM as CLAIM_TOKEN, OUTBOX_CLAIMED_UNTIL as CLAIMED_UNTIL, composePrimaryKey, KEY_TEXT, marshalOrFail, projectionTtlChecked, projectionTtlConflict, OUTBOX_ROW_PK as ROW_PK, OUTBOX_ROW_SK as ROW_SK, serializeSparseOrFail, validateAndBuildPutItem, } from "./TransactableOps.js";
|
|
27
33
|
// ---------------------------------------------------------------------------
|
|
28
34
|
// Removal (#154)
|
|
29
35
|
// ---------------------------------------------------------------------------
|
|
@@ -84,7 +90,260 @@ export const modelFields = (model) => {
|
|
|
84
90
|
const fields = getSchemaFields(schema);
|
|
85
91
|
return fields === undefined ? undefined : Object.keys(fields);
|
|
86
92
|
};
|
|
87
|
-
/**
|
|
93
|
+
/**
|
|
94
|
+
* @internal The outboxes on other tables of the read model's projections (`_projectionOutboxes`,
|
|
95
|
+
* the entries with a `table`): several projections of one read model may each have one. A local
|
|
96
|
+
* outbox is on the read model's own table, where its rows are.
|
|
97
|
+
*/
|
|
98
|
+
export const remoteOutboxesOf = (entity) => (entity
|
|
99
|
+
._projectionOutboxes ?? []).filter((outbox) => outbox.table !== undefined);
|
|
100
|
+
/**
|
|
101
|
+
* @internal The physical name of the table `tag` names, the one requests are sent to:
|
|
102
|
+
* `undefined` when the context doesn't provide it.
|
|
103
|
+
*/
|
|
104
|
+
export const physicalTableName = (tag) => Effect.map(Effect.serviceOption(tag), (config) => Option.isSome(config) ? config.value.name : undefined);
|
|
105
|
+
/**
|
|
106
|
+
* What a composite of `model` can be in a key (`KeyComposer.CompositeValues`), by its schema, as
|
|
107
|
+
* one side sees it:
|
|
108
|
+
*
|
|
109
|
+
* - `"written"`, the keys its rows are written under: the key text of its literals (a literal
|
|
110
|
+
* union), of `true` and `false` (a boolean), or a number's or bigint's key text; any other field
|
|
111
|
+
* (a string, a field the model doesn't declare) may hold any text, `#` included.
|
|
112
|
+
* - `"read"`, the keys `rekey`'s reader takes as its rows' (`KeyText136.readIndexKeys`): a number,
|
|
113
|
+
* a bigint or a boolean only as text its value composes again, and a literal composite as any
|
|
114
|
+
* text, which it reads as a string.
|
|
115
|
+
*/
|
|
116
|
+
const compositeValues = (model, side) => (composite) => {
|
|
117
|
+
if (model === undefined || model === null)
|
|
118
|
+
return "open";
|
|
119
|
+
const schema = isConfiguredModel(model) ? model.model : model;
|
|
120
|
+
const fields = getSchemaFields(schema);
|
|
121
|
+
if (fields === undefined || !Object.hasOwn(fields, composite))
|
|
122
|
+
return "open";
|
|
123
|
+
const field = fields[composite];
|
|
124
|
+
const literals = keyFormLiterals(field);
|
|
125
|
+
if (side === "written" && literals !== undefined)
|
|
126
|
+
return { values: literals };
|
|
127
|
+
const kind = keyFormValueKind(field);
|
|
128
|
+
if (kind === "number" || kind === "bigint")
|
|
129
|
+
return "number";
|
|
130
|
+
if (kind === "boolean")
|
|
131
|
+
return { values: [true, false] };
|
|
132
|
+
return "open";
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* The key space an entity's rows are under: the text its primary key's partition and sort keys
|
|
136
|
+
* start with, the composites' labels, and what each composite's value can be, composed by the call
|
|
137
|
+
* its writes make (`KeyComposer.composeKeySpace`): the schema's name and version, the primary
|
|
138
|
+
* collection (or the entity type), under a collection the entity type and its version, and each
|
|
139
|
+
* composite's name. Each value is what its type can compose (`compositeValues`), by `view`:
|
|
140
|
+
*
|
|
141
|
+
* - `"plain"`, the keys its rows are written under with values that hold no `#`: every composite is
|
|
142
|
+
* walked, to the end of the key, under the schema's setting.
|
|
143
|
+
* - `"written"` and `"read"`, the keys `rekey` meets on its table, under any setting: the walk
|
|
144
|
+
* stops at the first composite whose value may hold `#` (`"open"`), and anything follows it.
|
|
145
|
+
*
|
|
146
|
+
* `undefined` for an entity on no table.
|
|
147
|
+
*/
|
|
148
|
+
const keySpaceOf = (entity, view) => {
|
|
149
|
+
const member = entity;
|
|
150
|
+
const primary = member.indexes?.primary;
|
|
151
|
+
if (member._schema === undefined || member.entityType === undefined || primary === undefined) {
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
const values = compositeValues(member.model, view === "read" ? "read" : "written");
|
|
155
|
+
try {
|
|
156
|
+
return KeyComposer.composeKeySpace(member._schema, member.entityType, member._entityVersion ?? 1, primary, view === "plain"
|
|
157
|
+
? (composite) => {
|
|
158
|
+
const of = values(composite);
|
|
159
|
+
return of === "open" ? "text" : of;
|
|
160
|
+
}
|
|
161
|
+
: values, { everyForm: view !== "plain" });
|
|
162
|
+
}
|
|
163
|
+
catch {
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* The key space of a read model's outbox items: its rows' partition keys, and its rows' sort keys
|
|
169
|
+
* followed by `#outbox#` (`<rowSk>#outbox#<revision>#<n>`), so any text after a whole sort key.
|
|
170
|
+
*/
|
|
171
|
+
const outboxSpaceOf = (space) => {
|
|
172
|
+
const { sk } = space;
|
|
173
|
+
if (sk.open)
|
|
174
|
+
return space;
|
|
175
|
+
const last = sk.after.length - 1;
|
|
176
|
+
return {
|
|
177
|
+
pk: space.pk,
|
|
178
|
+
sk: last < 0
|
|
179
|
+
? { text: `${sk.text}#outbox#`, after: [], values: [], open: true }
|
|
180
|
+
: { ...sk, after: [...sk.after.slice(0, last), `${sk.after[last]}#outbox#`], open: true },
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
/** Whether a key of one key space can be a key of the other, in both halves. */
|
|
184
|
+
const spacesMeet = (a, b) => KeyComposer.keySpacesMeet(a.pk, b.pk) && KeyComposer.keySpacesMeet(a.sk, b.sk);
|
|
185
|
+
/**
|
|
186
|
+
* Whether the outbox items of one entity can be read by `rekey` as the other's, either way round:
|
|
187
|
+
* the keys one's are written under (`"written"`) against the keys the other's reader takes
|
|
188
|
+
* (`"read"`).
|
|
189
|
+
*/
|
|
190
|
+
const readAsEachOther = (a, b) => {
|
|
191
|
+
const meet = (writer, reader) => {
|
|
192
|
+
const written = keySpaceOf(writer, "written");
|
|
193
|
+
const read = keySpaceOf(reader, "read");
|
|
194
|
+
return (written !== undefined &&
|
|
195
|
+
read !== undefined &&
|
|
196
|
+
spacesMeet(outboxSpaceOf(written), outboxSpaceOf(read)));
|
|
197
|
+
};
|
|
198
|
+
return meet(a, b) || meet(b, a);
|
|
199
|
+
};
|
|
200
|
+
/**
|
|
201
|
+
* Whether two entities compose every key the same way: one schema (name, version, casing,
|
|
202
|
+
* `escapeKeys`), entity type and version, primary key composites, casing and collection, and each
|
|
203
|
+
* composite of one type. Then either one's reader moves a row as the other's would.
|
|
204
|
+
*/
|
|
205
|
+
const sameKeys = (a, b) => {
|
|
206
|
+
const x = a;
|
|
207
|
+
const y = b;
|
|
208
|
+
const px = x.indexes?.primary;
|
|
209
|
+
const py = y.indexes?.primary;
|
|
210
|
+
const sx = x._schema;
|
|
211
|
+
const sy = y._schema;
|
|
212
|
+
if (px === undefined || py === undefined || sx === undefined || sy === undefined)
|
|
213
|
+
return false;
|
|
214
|
+
const fieldsOf = (model) => model === undefined || model === null
|
|
215
|
+
? undefined
|
|
216
|
+
: getSchemaFields(isConfiguredModel(model) ? model.model : model);
|
|
217
|
+
const fx = fieldsOf(x.model);
|
|
218
|
+
const fy = fieldsOf(y.model);
|
|
219
|
+
const typeOf = (fields, composite) => {
|
|
220
|
+
if (fields === undefined || !Object.hasOwn(fields, composite))
|
|
221
|
+
return undefined;
|
|
222
|
+
const field = fields[composite];
|
|
223
|
+
return JSON.stringify([keyFormValueKind(field), keyFormLiterals(field)]);
|
|
224
|
+
};
|
|
225
|
+
const same = (u, v) => u.length === v.length && u.every((name, i) => name === v[i]);
|
|
226
|
+
return (x.entityType === y.entityType &&
|
|
227
|
+
(x._entityVersion ?? 1) === (y._entityVersion ?? 1) &&
|
|
228
|
+
sx.name === sy.name &&
|
|
229
|
+
sx.version === sy.version &&
|
|
230
|
+
sx.casing === sy.casing &&
|
|
231
|
+
(sx.escapeKeys === true) === (sy.escapeKeys === true) &&
|
|
232
|
+
same(px.pk.composite, py.pk.composite) &&
|
|
233
|
+
same(px.sk.composite, py.sk.composite) &&
|
|
234
|
+
px.casing === py.casing &&
|
|
235
|
+
JSON.stringify(px.collection) === JSON.stringify(py.collection) &&
|
|
236
|
+
px.type === py.type &&
|
|
237
|
+
[...px.pk.composite, ...px.sk.composite].every((composite) => {
|
|
238
|
+
const t = typeOf(fx, composite);
|
|
239
|
+
return t !== undefined && t === typeOf(fy, composite);
|
|
240
|
+
}));
|
|
241
|
+
};
|
|
242
|
+
/**
|
|
243
|
+
* @internal `[EDD-9134]`: among `members` (each named as `site`'s message shows it), a read model
|
|
244
|
+
* whose projection's outbox is on another table, where another read model in the same key space
|
|
245
|
+
* has its outbox too. Tables are compared by the physical name the context resolves (two handles
|
|
246
|
+
* on one table are one table), and by service when one isn't provided. The problem, or `undefined`.
|
|
247
|
+
*
|
|
248
|
+
* Two questions, by `scope`:
|
|
249
|
+
*
|
|
250
|
+
* - `"plain"` (where read models are bound: `DynamoClient.make`, `EventStore.processBatch`): can a
|
|
251
|
+
* key of one be a key of the other with values that hold no `#`, each value what its type can
|
|
252
|
+
* compose? Then their items overwrite each other whenever their values match, in normal
|
|
253
|
+
* operation. Read models whose keys meet only through a value holding `#` bind and run, as on
|
|
254
|
+
* 1.39.0, as do two whose keys part where a value of one can't hold the other's text (a number
|
|
255
|
+
* beside a label).
|
|
256
|
+
* - a table (`rekey` of it): can `rekey` read an item of one as the other's at all, a string value
|
|
257
|
+
* holding `#` included, and a literal composite read as any text, as its reader reads it? Then it
|
|
258
|
+
* couldn't tell whose an item on that table is. Only outboxes on that table are looked at. The
|
|
259
|
+
* table's own members (`scope.members`) are looked at too: an entity claims the outbox items of
|
|
260
|
+
* its entity type on its table (its own projections' outbox there), so one that can read a read
|
|
261
|
+
* model's items as its own would move them by its key. One whose keys are composed exactly as
|
|
262
|
+
* the read model's (`sameKeys`) moves them where the read model would, and isn't refused.
|
|
263
|
+
*
|
|
264
|
+
* Read models that never meet at a site that sees several at once can't be checked: their key
|
|
265
|
+
* spaces must be kept apart by whoever defines them.
|
|
266
|
+
*/
|
|
267
|
+
export const sharedOutboxKeySpace = (site, members, scope) => Effect.gen(function* () {
|
|
268
|
+
const plain = scope === "plain";
|
|
269
|
+
// Every table a member's outbox is on, by its physical name.
|
|
270
|
+
const names = new Map();
|
|
271
|
+
for (const [, entity] of members) {
|
|
272
|
+
for (const { table } of remoteOutboxesOf(entity)) {
|
|
273
|
+
if (!names.has(table.Tag.key)) {
|
|
274
|
+
names.set(table.Tag.key, yield* physicalTableName(table.Tag));
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
const oneTable = (a, b) => {
|
|
279
|
+
if (a.key === b.key)
|
|
280
|
+
return true;
|
|
281
|
+
const name = names.get(a.key);
|
|
282
|
+
return name !== undefined && name === names.get(b.key);
|
|
283
|
+
};
|
|
284
|
+
const startOf = (space) => `(partition keys starting "${space.pk.text}", sort keys starting "${space.sk.text}": the ` +
|
|
285
|
+
"same schema name and version, primary collection, entity type and first composite)";
|
|
286
|
+
for (const [name, entity] of members) {
|
|
287
|
+
const space = keySpaceOf(entity, plain ? "plain" : "written");
|
|
288
|
+
if (space === undefined)
|
|
289
|
+
continue;
|
|
290
|
+
for (const outbox of remoteOutboxesOf(entity)) {
|
|
291
|
+
const into = outbox.table.Tag;
|
|
292
|
+
if (!plain && into.key !== scope.key && names.get(into.key) !== scope.name)
|
|
293
|
+
continue;
|
|
294
|
+
const physical = names.get(into.key);
|
|
295
|
+
for (const [otherName, other] of members) {
|
|
296
|
+
if (other === entity)
|
|
297
|
+
continue;
|
|
298
|
+
if (!remoteOutboxesOf(other).some((o) => oneTable(o.table.Tag, into)))
|
|
299
|
+
continue;
|
|
300
|
+
if (plain) {
|
|
301
|
+
const otherSpace = keySpaceOf(other, "plain");
|
|
302
|
+
if (otherSpace === undefined ||
|
|
303
|
+
!spacesMeet(outboxSpaceOf(space), outboxSpaceOf(otherSpace))) {
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
else if (!readAsEachOther(entity, other))
|
|
308
|
+
continue;
|
|
309
|
+
return (`[EDD-9134] ${site}: ${name} writes its projection's outbox items into another ` +
|
|
310
|
+
`table${physical === undefined ? "" : ` ("${physical}")`}, where ${otherName} ` +
|
|
311
|
+
`writes its own under the same key space ${startOf(space)}. ` +
|
|
312
|
+
(plain
|
|
313
|
+
? "Their items would overwrite each other whenever their keys match, and rekey " +
|
|
314
|
+
"couldn't tell whose an item is. "
|
|
315
|
+
: "rekey reads an item's key back by its key space, and couldn't tell whose an " +
|
|
316
|
+
"item on this table is. ") +
|
|
317
|
+
"Give one of them its own entity type, collection, schema or key composites.");
|
|
318
|
+
}
|
|
319
|
+
if (plain)
|
|
320
|
+
continue;
|
|
321
|
+
// The members of the table being rekeyed that claim this read model's outbox items.
|
|
322
|
+
for (const [memberName, member] of scope.members) {
|
|
323
|
+
if (member === entity)
|
|
324
|
+
continue;
|
|
325
|
+
const claims = handlerOf(member)?.types.includes(`${readModelType(entity)}#outbox`);
|
|
326
|
+
if (claims !== true || sameKeys(member, entity) || !readAsEachOther(entity, member)) {
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
return (`[EDD-9134] ${site}: ${name} writes its projection's outbox items into another ` +
|
|
330
|
+
`table${physical === undefined ? "" : ` ("${physical}")`}, whose member ` +
|
|
331
|
+
`${memberName} claims the outbox items of its entity type there under the same key ` +
|
|
332
|
+
`space ${startOf(space)}, and composes its keys differently. rekey reads an item's ` +
|
|
333
|
+
"key back by its key space, and couldn't tell whose an item on this table is. Give " +
|
|
334
|
+
"one of them its own entity type, collection, schema or key composites.");
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return undefined;
|
|
339
|
+
});
|
|
340
|
+
/** An entity's type, as its rows' `__edd_e__` names it. */
|
|
341
|
+
const readModelType = (entity) => entity.entityType;
|
|
342
|
+
/**
|
|
343
|
+
* @internal `[EDD-9134]` ({@link sharedOutboxKeySpace}) as a defect, from the sites that define
|
|
344
|
+
* what runs (`DynamoClient.make`, `EventStore.processBatch`), before anything runs.
|
|
345
|
+
*/
|
|
346
|
+
export const refuseSharedOutboxKeySpace = (site, members) => Effect.flatMap(sharedOutboxKeySpace(site, members, "plain"), (problem) => problem === undefined ? Effect.void : Effect.die(new Error(problem)));
|
|
88
347
|
export const makeEngine = (config) => {
|
|
89
348
|
const { readModel } = config;
|
|
90
349
|
const entityType = readModel.entityType;
|
|
@@ -119,25 +378,60 @@ export const makeEngine = (config) => {
|
|
|
119
378
|
}
|
|
120
379
|
return [...kept];
|
|
121
380
|
})();
|
|
122
|
-
//
|
|
123
|
-
//
|
|
124
|
-
//
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
381
|
+
// Each attribute the projection keeps on a row, with its role: the checkpoints, then the
|
|
382
|
+
// revision, as a tombstone stores them. An attribute in two roles is listed in each, and refused
|
|
383
|
+
// (`[EDD-9133]`, `addProjectionLayout`) when the read model's rows may be tombstones.
|
|
384
|
+
const roles = [];
|
|
385
|
+
const giveRole = (field, role) => {
|
|
386
|
+
if (!roles.some(([named, held]) => named === field && held === role))
|
|
387
|
+
roles.push([field, role]);
|
|
388
|
+
};
|
|
389
|
+
if (config.checkpoints.kind === "scalar")
|
|
390
|
+
giveRole(config.checkpoints.field, "checkpoint");
|
|
391
|
+
else {
|
|
392
|
+
if (config.checkpoints.map !== undefined)
|
|
393
|
+
giveRole(config.checkpoints.map, "checkpoint map");
|
|
394
|
+
for (const f of Object.values(config.checkpoints.fields)) {
|
|
395
|
+
giveRole(f.field, "checkpoint");
|
|
396
|
+
giveRole(f.instanceField, "checkpoint owner");
|
|
397
|
+
}
|
|
132
398
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
399
|
+
if (config.revision !== undefined)
|
|
400
|
+
giveRole(config.revision.field, "revision");
|
|
401
|
+
// Recorded beside the layouts of the read model's other projections, whether or not it has
|
|
402
|
+
// `removal`, for the checks of `[EDD-9133]`. Rows may be tombstones (#154) when one has
|
|
403
|
+
// `removal`: the entity's `create` takes one's place, keeping the checkpoints of the projection
|
|
404
|
+
// with `removal` that wrote it, with its revision one higher (`overTombstone`).
|
|
405
|
+
addProjectionLayout(readModel, {
|
|
406
|
+
label: config.label,
|
|
407
|
+
roles,
|
|
408
|
+
removal: config.removal !== undefined,
|
|
409
|
+
ttl: config.removal?.ttl !== undefined,
|
|
410
|
+
});
|
|
411
|
+
// Every projection's outbox, kept on the read model, one entry per distinct layout, the one defined
|
|
412
|
+
// last at the end: a second projection doesn't hide the first's, which stays callable, and one
|
|
413
|
+
// defined again adds nothing. One on another table (`table`) is moved by that table's `rekey`
|
|
414
|
+
// (#171). Wherever it is, an aggregate declaring `ownsRows` is checked against its items at bind
|
|
415
|
+
// (DESIGN.md §7 "Which rows in its partition are an aggregate's"), with the index keys it writes
|
|
416
|
+
// on them.
|
|
417
|
+
if (config.outbox !== undefined) {
|
|
418
|
+
const holder = readModel;
|
|
419
|
+
const layout = {
|
|
137
420
|
table: config.outbox.table,
|
|
138
421
|
pk: config.outbox.pk,
|
|
139
422
|
sk: config.outbox.sk,
|
|
423
|
+
index: config.outbox.index,
|
|
140
424
|
};
|
|
425
|
+
const same = (other) => other.table?.Tag === layout.table?.Tag &&
|
|
426
|
+
other.pk === layout.pk &&
|
|
427
|
+
other.sk === layout.sk &&
|
|
428
|
+
other.index?.name === layout.index?.name &&
|
|
429
|
+
other.index?.pk === layout.index?.pk &&
|
|
430
|
+
other.index?.sk === layout.index?.sk;
|
|
431
|
+
holder._projectionOutboxes = [
|
|
432
|
+
...(holder._projectionOutboxes ?? []).filter((other) => !same(other)),
|
|
433
|
+
layout,
|
|
434
|
+
];
|
|
141
435
|
}
|
|
142
436
|
const tableName = Effect.suspend(() => Effect.map(readModel._tableTag, (tc) => tc.name));
|
|
143
437
|
/** The row, read strongly consistent — raw, so a tombstone reads as one. */
|
|
@@ -327,9 +621,14 @@ export const makeEngine = (config) => {
|
|
|
327
621
|
? { pk: primary.pk.field, sk: primary.sk.field }
|
|
328
622
|
: { pk: config.outbox.pk, sk: config.outbox.sk };
|
|
329
623
|
const pad = (n, width) => String(n).padStart(width, "0");
|
|
624
|
+
const outboxReadBack = config.outbox === undefined
|
|
625
|
+
? undefined
|
|
626
|
+
: readBackOf(config.outbox.schema, () => readingStreamValues(config.outbox.schema));
|
|
330
627
|
const outboxItems = (rowKey, revision, enqueued, mark) => Effect.forEach(enqueued, (e, i) => Effect.gen(function* () {
|
|
331
628
|
const outbox = config.outbox;
|
|
332
|
-
const encoded = yield* S.encodeUnknownEffect(outbox.schema)(e.effect).pipe(Effect.
|
|
629
|
+
const encoded = yield* S.encodeUnknownEffect(writingTuplePositions(outbox.schema))(e.effect).pipe(Effect.tap((wire) => outboxReadBack === undefined
|
|
630
|
+
? Effect.void
|
|
631
|
+
: outboxReadBack(wire).pipe(Effect.mapError((issue) => notReadBack("The outbox effect", issue)))), Effect.mapError((cause) => new ValidationError({ entityType, operation: `${config.label}.outbox`, cause })));
|
|
333
632
|
const keys = outboxKeys();
|
|
334
633
|
const rowPk = String(rowKey[primary.pk.field]);
|
|
335
634
|
const rowSk = String(rowKey[primary.sk.field]);
|
|
@@ -351,7 +650,7 @@ export const makeEngine = (config) => {
|
|
|
351
650
|
item[outbox.index.pk] = outboxMarker(entityType);
|
|
352
651
|
item[outbox.index.sk] = `${rowPk}#${sk}`;
|
|
353
652
|
}
|
|
354
|
-
return toAttributeMap(item);
|
|
653
|
+
return yield* marshalOrFail(entityType, `${config.label}.outbox`, () => toAttributeMap(item));
|
|
355
654
|
}));
|
|
356
655
|
/** A lost race, from a single write's or a transaction's failure: the row's Put cancelled. */
|
|
357
656
|
const lostRaceOf = (error, transaction) => {
|
|
@@ -389,15 +688,14 @@ export const makeEngine = (config) => {
|
|
|
389
688
|
if (config.removal?.ttl !== undefined) {
|
|
390
689
|
tomb[resolveTtlAttributeName(tc)] = Math.floor((DateTime.toEpochMillis(now) + Dur.toMillis(config.removal.ttl)) / 1000);
|
|
391
690
|
}
|
|
392
|
-
|
|
691
|
+
// A sparse-map key is refused as the row's own write refuses it. The check flattens a
|
|
692
|
+
// copy: what's stored is the tombstone as 1.39.0 stored it, an empty map whole.
|
|
693
|
+
yield* serializeSparseOrFail(readModel, { ...tomb }, `${config.label}.write`);
|
|
694
|
+
rowItem = yield* marshalOrFail(entityType, `${config.label}.write`, () => toAttributeMap(tomb));
|
|
393
695
|
}
|
|
394
696
|
else if (enqueued.length > 0) {
|
|
395
|
-
const built = yield* validateAndBuildPutItem(readModel, target.value, `${config.label}.write
|
|
396
|
-
|
|
397
|
-
if (createdAtField !== null && DateTime.isDateTime(createdAt)) {
|
|
398
|
-
item[dbName(createdAtField)] = generateTimestampPrimitive(DateTime.toUtc(createdAt), readModel.systemFields.createdAtEncoding);
|
|
399
|
-
}
|
|
400
|
-
rowItem = toAttributeMap(item);
|
|
697
|
+
const built = yield* validateAndBuildPutItem(readModel, target.value, `${config.label}.write`, { createdAt: DateTime.isDateTime(createdAt) ? DateTime.toUtc(createdAt) : undefined });
|
|
698
|
+
rowItem = toAttributeMap(built.item);
|
|
401
699
|
}
|
|
402
700
|
if (rowItem === undefined) {
|
|
403
701
|
// The row alone, through the entity: `put` conditioned on the guard
|
|
@@ -465,6 +763,28 @@ export const makeEngine = (config) => {
|
|
|
465
763
|
`${to}: a strongly consistent read did not return them. Was the stream written ` +
|
|
466
764
|
"outside `append`?",
|
|
467
765
|
}));
|
|
766
|
+
/**
|
|
767
|
+
* `[EDD-9133]` once the table's TTL attribute is known: an attribute a projection keeps on the
|
|
768
|
+
* read model's rows that is stored as the TTL attribute, which a projection's tombstones set. The
|
|
769
|
+
* two would overwrite each other, and a row could expire. Every entry point that writes a row
|
|
770
|
+
* runs it before it sends anything, whatever the data: `applyRow` for each row, and `catchUp` and
|
|
771
|
+
* `rebuild` first (`operation` names the entry point). The drains write no row and don't run it:
|
|
772
|
+
* a worker providing only its outbox's table delivers as on 1.39.0.
|
|
773
|
+
*/
|
|
774
|
+
const preflight = (operation) => Effect.suspend(() =>
|
|
775
|
+
// The table is resolved only when there is something to check.
|
|
776
|
+
!projectionTtlChecked(readModel)
|
|
777
|
+
? Effect.void
|
|
778
|
+
: Effect.flatMap(readModel._tableTag, (tc) => {
|
|
779
|
+
const problem = projectionTtlConflict(readModel, resolveTtlAttributeName(tc));
|
|
780
|
+
return problem === undefined
|
|
781
|
+
? Effect.void
|
|
782
|
+
: Effect.fail(new ValidationError({
|
|
783
|
+
entityType,
|
|
784
|
+
operation: `${config.label}.${operation}`,
|
|
785
|
+
cause: problem,
|
|
786
|
+
}));
|
|
787
|
+
}));
|
|
468
788
|
const withRetry = (attempt) => Effect.retry(attempt, {
|
|
469
789
|
schedule: config.retry,
|
|
470
790
|
while: (error) => error instanceof LostRace,
|
|
@@ -787,7 +1107,7 @@ export const makeEngine = (config) => {
|
|
|
787
1107
|
failed,
|
|
788
1108
|
};
|
|
789
1109
|
});
|
|
790
|
-
return withRetry(attempt);
|
|
1110
|
+
return Effect.andThen(preflight("apply"), withRetry(attempt));
|
|
791
1111
|
};
|
|
792
1112
|
/** The fields the library owns, re-applied after `toItem`. */
|
|
793
1113
|
const pick = (target) => {
|
|
@@ -800,6 +1120,8 @@ export const makeEngine = (config) => {
|
|
|
800
1120
|
// -------------------------------------------------------------------------
|
|
801
1121
|
// Outbox drain (#155)
|
|
802
1122
|
// -------------------------------------------------------------------------
|
|
1123
|
+
// A decode that dies on an absent attribute fails as 1.39.0's did (`decodingAsBefore`).
|
|
1124
|
+
const decodeEffect = decodingAsBefore(readingStreamValues(config.outbox?.schema ?? S.Unknown), config.outbox?.schema ?? S.Unknown);
|
|
803
1125
|
/**
|
|
804
1126
|
* Deliver a row's pending effects in order: claim the head item, deliver it,
|
|
805
1127
|
* delete it, repeat. A claim another drainer holds stops the drain; a crash
|
|
@@ -843,7 +1165,7 @@ export const makeEngine = (config) => {
|
|
|
843
1165
|
// Another drainer holds the head: it delivers in order; stop here.
|
|
844
1166
|
if (!claimed)
|
|
845
1167
|
return { delivered, undelivered: Option.none() };
|
|
846
|
-
const effect = yield*
|
|
1168
|
+
const effect = yield* decodeEffect(raw.effect).pipe(Effect.mapError((cause) => new ValidationError({ entityType, operation: `${config.label}.drain`, cause })));
|
|
847
1169
|
const meta = {
|
|
848
1170
|
source: String(raw.source),
|
|
849
1171
|
streamId: String(raw.streamId),
|
|
@@ -924,7 +1246,15 @@ export const makeEngine = (config) => {
|
|
|
924
1246
|
}
|
|
925
1247
|
return { delivered, undelivered };
|
|
926
1248
|
});
|
|
927
|
-
return {
|
|
1249
|
+
return {
|
|
1250
|
+
readRow,
|
|
1251
|
+
// A key the row can't compose is that row's failure, not the batch's.
|
|
1252
|
+
applyRow: (key, work, options) => Capacity.refusalsFail(`${config.label}.apply`, { target: entityType }, applyRow(key, work, options)),
|
|
1253
|
+
drain,
|
|
1254
|
+
drainPending,
|
|
1255
|
+
preflight,
|
|
1256
|
+
call,
|
|
1257
|
+
};
|
|
928
1258
|
};
|
|
929
1259
|
/**
|
|
930
1260
|
* The default lost-race policy: up to 12 retries after a jittered delay
|