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:
- Explicit
serializeFn: Custom transformer passed by caller/decorator. value.toJSON(): If the object provides atoJSON()method (e.g.RootEntity.toJSON()).- Deep JSON clone (
JSON.parse(JSON.stringify(value))): Clones plain objects and strips live class references/methods if notoJSONexists, guaranteeing isolation parity. - Primitive / nullish pass-through: Returns primitives and null/undefined as-is.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”T
The entity or value to snapshot.
serializeFn?
Section titled “serializeFn?”((value) => unknown) | null
Optional custom serialization function.
Returns
Section titled “Returns”unknown
Pure, detached serializable snapshot.
Examples
Section titled “Examples”Extracting snapshot from domain entity with toJSON
const snapshot = toCacheSnapshot(user);// Returns plain UserSnapshot objectCustom serializer function
const snapshot = toCacheSnapshot(user, (u) => ({ id: u.id, custom: true }));