Skip to content

Commit 7bb5607

Browse files
Update packages/emmett-event-store-kysely/src/projections/snapshot-projection.ts
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 8d5db73 commit 7bb5607

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

packages/emmett-event-store-kysely/src/projections/snapshot-projection.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,26 @@ export function createSnapshotProjectionWithSnapshotTable<
298298
) => {
299299
const keys = extractKeys(event, partition);
300300

301-
// Infer primary keys from extractKeys on first call
301+
const currentKeys = Object.keys(keys);
302+
const sortedCurrentKeys = [...currentKeys].sort();
303+
304+
// Infer and validate primary keys from extractKeys
302305
if (!inferredPrimaryKeys) {
303-
inferredPrimaryKeys = Object.keys(keys);
306+
// Cache the initially inferred primary keys in a deterministic order
307+
inferredPrimaryKeys = sortedCurrentKeys;
308+
} else {
309+
// Validate that subsequent calls to extractKeys return the same key set
310+
if (
311+
inferredPrimaryKeys.length !== sortedCurrentKeys.length ||
312+
!inferredPrimaryKeys.every((key, index) => key === sortedCurrentKeys[index])
313+
) {
314+
throw new Error(
315+
`Snapshot projection "${tableName}" received inconsistent primary keys from extractKeys. ` +
316+
`Expected keys: ${inferredPrimaryKeys.join(", ")}, ` +
317+
`but received: ${sortedCurrentKeys.join(", ")}. ` +
318+
`Ensure extractKeys returns a consistent set of keys for all events.`,
319+
);
320+
}
304321
}
305322

306323
const primaryKeys = inferredPrimaryKeys;

0 commit comments

Comments
 (0)