This repository was archived by the owner on Dec 20, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -4,8 +4,16 @@ module.exports = function(patches){
4
4
var update = { } ;
5
5
patches . map ( function ( p ) {
6
6
if ( p . op === 'add' ) {
7
+ var path = toDot ( p . path ) ;
7
8
if ( ! update . $push ) update . $push = { } ;
8
- update . $push [ toDot ( p . path ) ] = p . value ;
9
+ if ( ! update . $push [ path ] ) {
10
+ update . $push [ path ] = p . value ;
11
+ } else if ( update . $push [ path ] ) {
12
+ if ( ! update . $push [ path ] . $each ) {
13
+ update . $push [ path ] = { $each : [ update . $push [ path ] ] } ;
14
+ }
15
+ update . $push [ path ] . $each . push ( p . value ) ;
16
+ }
9
17
}
10
18
else if ( p . op === 'remove' ) {
11
19
if ( ! update . $unset ) update . $unset = { } ;
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ var chai = require('chai');
4
4
5
5
describe ( 'jsonpatch to mongodb' , function ( ) {
6
6
7
- it ( 'should work with add' , function ( ) {
7
+ it ( 'should work with single add' , function ( ) {
8
8
var patches = [ {
9
9
op : 'add' ,
10
10
path : '/name' ,
@@ -20,6 +20,30 @@ describe('jsonpatch to mongodb', function() {
20
20
assert . deepEqual ( toMongodb ( patches ) , expected ) ;
21
21
} ) ;
22
22
23
+ it ( 'should work with multiple adds' , function ( ) {
24
+ var patches = [ {
25
+ op : 'add' ,
26
+ path : '/name' ,
27
+ value : 'dave'
28
+ } , {
29
+ op : 'add' ,
30
+ path : '/name' ,
31
+ value : 'bob'
32
+ } , {
33
+ op : 'add' ,
34
+ path : '/name' ,
35
+ value : 'john'
36
+ } ] ;
37
+
38
+ var expected = {
39
+ $push : {
40
+ name : { $each : [ 'dave' , 'bob' , 'john' ] }
41
+ }
42
+ } ;
43
+
44
+ assert . deepEqual ( toMongodb ( patches ) , expected ) ;
45
+ } ) ;
46
+
23
47
it ( 'should work with remove' , function ( ) {
24
48
var patches = [ {
25
49
op : 'remove' ,
You can’t perform that action at this time.
0 commit comments