Skip to content

isCacheNewer

isCacheNewer(cached, incoming): boolean

Defined in: packages/ddd-core/persistence/helpers/cache-version.helper.ts:32

Checks whether a cached snapshot or record is strictly newer than an incoming record based on explicit version numbers, generation counters, or updatedAt timestamps.

Returns true only if cached is strictly newer than incoming. Returns false if incoming is newer, equal, or if no meaningful ordering can be determined.

Comparison precedence:

  1. CacheMutationBarrier: a barrier outranks any snapshot. A cached barrier is newer than an incoming snapshot, an incoming barrier always replaces a cached snapshot, and between two barriers the cached one is newer unless the incoming one has a later createdAt.
  2. Numeric version (optimistic concurrency version)
  3. Numeric __gen (generation sequence counter)
  4. updatedAt (Date instance or ISO 8601 string / timestamp)

unknown

The existing data in cache.

unknown

The incoming data being written or compared.

boolean

true if cached is strictly newer than incoming, false otherwise.

Comparing versioned aggregate snapshots

isCacheNewer({ id: '1', version: 3 }, { id: '1', version: 2 }); // true
isCacheNewer({ id: '1', version: 2 }, { id: '1', version: 3 }); // false
isCacheNewer({ id: '1', version: 2 }, { id: '1', version: 2 }); // false