mswjs / mswjs/data

Records containing non-serializable values (e.g. Temporal, Decimal) throw DataCloneError during updates

Open
#378 0 comments 0 reactions 0 assignees View on GitHub

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.