Skip to content

Commit 832b21e

Browse files
committed
Update RxDocument patch and incrementalPatch methods to merge the patch into the document using @fastify/deepmerge package.
This prevents an issue that can arise when calling `patch` or `incrementalPatch` with a patch value containing fields deeper that the top level of the document where values will be spliced out if the patch value does not contain a full copy of all fields instead of just the fields being patched.
1 parent 5687094 commit 832b21e

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@
486486
},
487487
"dependencies": {
488488
"@babel/runtime": "7.28.4",
489+
"@fastify/deepmerge": "3.1.0",
489490
"@types/clone": "2.1.4",
490491
"@types/cors": "2.8.19",
491492
"@types/express": "5.0.3",
@@ -634,4 +635,4 @@
634635
"webpack-dev-server": "5.2.2"
635636
},
636637
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
637-
}
638+
}

src/rx-document.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ import { getSchemaByObjectPath } from './rx-schema-helper.ts';
4141
import { getWrittenDocumentsFromBulkWriteResponse, throwIfIsStorageWriteError } from './rx-storage-helper.ts';
4242
import { modifierFromPublicToInternal } from './incremental-write.ts';
4343

44+
import deepmergeLib from '@fastify/deepmerge';
45+
46+
// Create merge function with custom array behavior
47+
const merge = deepmergeLib({
48+
mergeArray: (options) => {
49+
return (target, source) => {
50+
// Replace array entirely with source
51+
return options.clone(source);
52+
};
53+
}
54+
});
55+
4456
export const basePrototype = {
4557
get primaryPath() {
4658
const _this: RxDocument = this as any;
@@ -301,12 +313,7 @@ export const basePrototype = {
301313
patch: Partial<RxDocType>
302314
) {
303315
const oldData = this._data;
304-
const newData = clone(oldData);
305-
Object
306-
.entries(patch)
307-
.forEach(([k, v]) => {
308-
(newData as any)[k] = v;
309-
});
316+
const newData = merge(clone(oldData), patch);
310317
return this._saveData(newData, oldData);
311318
},
312319

@@ -318,12 +325,7 @@ export const basePrototype = {
318325
patch: Partial<RxDocumentType>
319326
): Promise<RxDocument<RxDocumentType>> {
320327
return this.incrementalModify((docData) => {
321-
Object
322-
.entries(patch)
323-
.forEach(([k, v]) => {
324-
(docData as any)[k] = v;
325-
});
326-
return docData;
328+
return merge(docData, patch) as typeof docData;
327329
});
328330
},
329331

0 commit comments

Comments
 (0)