Skip to content

toCacheSnapshot

toCacheSnapshot<T>(value, serializeFn?): unknown

Defined in: packages/ddd-core/persistence/helpers/cache-snapshot.helper.ts:29

Extracts a detached, serializable snapshot from a domain entity, aggregate root, or arbitrary value before storing in cache or performing version comparisons.

Serialization precedence:

  1. Explicit serializeFn: Custom transformer passed by caller/decorator.
  2. value.toJSON(): If the object provides a toJSON() method (e.g. RootEntity.toJSON()).
  3. Deep JSON clone (JSON.parse(JSON.stringify(value))): Clones plain objects and strips live class references/methods if no toJSON exists, guaranteeing isolation parity.
  4. Primitive / nullish pass-through: Returns primitives and null/undefined as-is.

T

T

The entity or value to snapshot.

((value) => unknown) | null

Optional custom serialization function.

unknown

Pure, detached serializable snapshot.

Extracting snapshot from domain entity with toJSON

const snapshot = toCacheSnapshot(user);
// Returns plain UserSnapshot object

Custom serializer function

const snapshot = toCacheSnapshot(user, (u) => ({ id: u.id, custom: true }));