Skip to content

Preserve custom class instances during update() - #1715

Open
obeattie wants to merge 3 commits into
TanStack:mainfrom
obeattie:fix/preserve-custom-class-instances
Open

Preserve custom class instances during update()#1715
obeattie wants to merge 3 commits into
TanStack:mainfrom
obeattie:fix/preserve-custom-class-instances

Conversation

@obeattie

@obeattie obeattie commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

🎯 Changes

Fixes an issue where update() could silently convert untouched custom class instances into plain objects when another field in the same nested object was changed. Although the data retained the same shape, it lost its prototype, causing methods and instanceof checks to fail.

Custom class instances are now treated as opaque values and preserved by reference, matching Immer-style behavior for non-draftable objects. Added both low-level change-tracking coverage and a collection-level regression test.

Fixes #1714

✅ Checklist

  • I have tested this code locally with pnpm test.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes

    • Updated nested data no longer replaces untouched custom class instances with plain objects.
    • Custom objects now retain their class identity and object reference when other fields are changed.
    • Improved handling of arrays, Maps, Sets, and typed arrays during change tracking and updates.
  • Chores

    • Added a patch release entry documenting the fix.

@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: c626376c-e2b8-45bb-9c1b-bd077b80eab0

📥 Commits

Reviewing files that changed from the base of the PR and between 67c840f and 0e5af36.

📒 Files selected for processing (4)
  • .changeset/slow-phones-fix.md
  • packages/db/src/proxy.ts
  • packages/db/tests/collection.test.ts
  • packages/db/tests/proxy.test.ts

📝 Walkthrough

Walkthrough

The update proxy now limits drafting to supported container types and preserves non-plain class instances by reference. Tests verify that untouched Money instances retain identity and class type after nested updates. A patch Changeset documents the fix.

Changes

Custom instance preservation

Layer / File(s) Summary
Proxy and clone semantic preservation
packages/db/src/proxy.ts
Proxy eligibility now uses plain-object rules. deepClone preserves plain-object prototypes and returns non-plain objects by reference.
Update behavior validation
packages/db/tests/collection.test.ts, packages/db/tests/proxy.test.ts, .changeset/slow-phones-fix.md
Tests verify that nested updates preserve untouched Money instances. A patch Changeset documents the fix.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: kyleamathews

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely identifies the primary change: preserving custom class instances during update().
Description check ✅ Passed The description covers the change, motivation, testing checklist, release impact, and changeset requirement.
Linked Issues check ✅ Passed The implementation preserves untouched custom class instances by reference and adds regression tests required by issue #1714.
Out of Scope Changes check ✅ Passed The changes are limited to proxy behavior, regression tests, and the required changeset for issue #1714.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

update() silently corrupts custom class instances in fields the callback doesn't touch

1 participant