Skip to content

Commit 362a10c

Browse files
committed
Update RxDocument patch and incrementalPatch methods to merge the patch into the document using object-deep-merge 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 362a10c

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@
511511
"mingo": "6.5.6",
512512
"mongodb": "6.20.0",
513513
"nats": "2.29.3",
514+
"object-deep-merge": "1.0.5",
514515
"oblivious-set": "1.4.0",
515516
"reconnecting-websocket": "4.4.0",
516517
"simple-peer": "9.11.1",
@@ -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: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ 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 { merge } from 'object-deep-merge';
45+
4446
export const basePrototype = {
4547
get primaryPath() {
4648
const _this: RxDocument = this as any;
@@ -301,12 +303,7 @@ export const basePrototype = {
301303
patch: Partial<RxDocType>
302304
) {
303305
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-
});
306+
const newData = merge(clone(oldData), patch);
310307
return this._saveData(newData, oldData);
311308
},
312309

@@ -318,12 +315,7 @@ export const basePrototype = {
318315
patch: Partial<RxDocumentType>
319316
): Promise<RxDocument<RxDocumentType>> {
320317
return this.incrementalModify((docData) => {
321-
Object
322-
.entries(patch)
323-
.forEach(([k, v]) => {
324-
(docData as any)[k] = v;
325-
});
326-
return docData;
318+
return merge(docData, patch);
327319
});
328320
},
329321

0 commit comments

Comments
 (0)