File tree Expand file tree Collapse file tree
packages/emmett-event-store-kysely/src/projections Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ;
You can’t perform that action at this time.
0 commit comments