Records containing non-serializable values (e.g. Temporal, Decimal) throw DataCloneError during updates
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 1.1k
- Forks
- 67
- Avg merge
- 9m
- Merged PRs (30d)
- 5
Description
Problem
structuredClone is used internally to copy records before updates, but it throws a DataCloneError for non-serializable values like class instances with function own-properties (e.g. Temporal.PlainDate, Decimal from decimal.js). There's currently no way to provide an alternative cloning strategy.
Proposal
Add an optional clone property to CollectionOptions that allows users to provide a custom clone function (e.g. cloneDeep from es-toolkit). It defaults to structuredClone to preserve existing behavior.
const collection = new Collection({
schema,
clone: cloneDeep, // just an example, cloneDeep maintains references of non-serializable types, custom strategy may be preferred
})
Notes
The persist and sync extensions rely on JSON.stringify and serializeRecord respectively, so records must still be serializable when using those extensions. The clone option only affects how records are copied during updates in the core collection.
Suggested Implementation
I have a working branch with tests if you'd like me to open a PR: https://github.com/danmcdaid/data/tree/feat/custom-clone-option
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at CollectionOptions and the core Collection update path where structuredClone copies records. Review the linked working branch and its tests for the proposed behavior, then verify that the default preserves structuredClone behavior while a supplied clone function is used during updates.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 72/100