@@ -363,23 +363,17 @@ function transformAtom(atom, force, options) {
363
363
objectId : atom . objectId
364
364
} ;
365
365
}
366
- if ( atom . __type == 'Date' ) {
367
- return new Date ( atom . iso ) ;
366
+ if ( DateCoder . isValidJSON ( atom ) ) {
367
+ return DateCoder . JSONToDatabase ( atom ) ;
368
368
}
369
- if ( atom . __type == 'GeoPoint' ) {
370
- if ( ! inArray && ! inObject ) {
371
- return [ atom . longitude , atom . latitude ] ;
372
- }
373
- return atom ;
369
+ if ( BytesCoder . isValidJSON ( atom ) ) {
370
+ return BytesCoder . JSONToDatabase ( atom ) ;
374
371
}
375
- if ( atom . __type == 'Bytes' ) {
376
- return new mongodb . Binary ( new Buffer ( atom . base64 , 'base64' ) ) ;
372
+ if ( GeoPointCoder . isValidJSON ( atom ) ) {
373
+ return ( inArray || inObject ? atom : GeoPointCoder . JSONToDatabase ( atom ) ) ;
377
374
}
378
- if ( atom . __type == 'File' ) {
379
- if ( ! inArray && ! inObject ) {
380
- return atom . name ;
381
- }
382
- return atom ;
375
+ if ( FileCoder . isValidJSON ( atom ) ) {
376
+ return ( inArray || inObject ? atom : FileCoder . JSONToDatabase ( atom ) ) ;
383
377
}
384
378
385
379
if ( force ) {
@@ -620,11 +614,8 @@ function untransformObject(schema, className, mongoObject) {
620
614
return Parse . _encode ( mongoObject ) ;
621
615
}
622
616
623
- if ( mongoObject instanceof mongodb . Binary ) {
624
- return {
625
- __type : 'Bytes' ,
626
- base64 : mongoObject . buffer . toString ( 'base64' )
627
- } ;
617
+ if ( BytesCoder . isValidDatabaseObject ( mongoObject ) ) {
618
+ return BytesCoder . databaseToJSON ( mongoObject ) ;
628
619
}
629
620
630
621
var restObject = untransformACL ( mongoObject ) ;
@@ -701,18 +692,11 @@ function untransformObject(schema, className, mongoObject) {
701
692
} else {
702
693
var expected = schema . getExpectedType ( className , key ) ;
703
694
if ( expected == 'file' && mongoObject [ key ] ) {
704
- restObject [ key ] = {
705
- __type : 'File' ,
706
- name : mongoObject [ key ]
707
- } ;
695
+ restObject [ key ] = FileCoder . databaseToJSON ( mongoObject [ key ] ) ;
708
696
break ;
709
697
}
710
698
if ( expected == 'geopoint' ) {
711
- restObject [ key ] = {
712
- __type : 'GeoPoint' ,
713
- latitude : mongoObject [ key ] [ 1 ] ,
714
- longitude : mongoObject [ key ] [ 0 ]
715
- } ;
699
+ restObject [ key ] = GeoPointCoder . databaseToJSON ( mongoObject [ key ] ) ;
716
700
break ;
717
701
}
718
702
}
@@ -726,6 +710,90 @@ function untransformObject(schema, className, mongoObject) {
726
710
}
727
711
}
728
712
713
+ var DateCoder = {
714
+ JSONToDatabase ( json ) {
715
+ return new Date ( json . iso ) ;
716
+ } ,
717
+
718
+ isValidJSON ( value ) {
719
+ return ( typeof value === 'object' &&
720
+ value !== null &&
721
+ value . __type === 'Date'
722
+ ) ;
723
+ }
724
+ } ;
725
+
726
+ var BytesCoder = {
727
+ databaseToJSON ( object ) {
728
+ return {
729
+ __type : 'Bytes' ,
730
+ base64 : object . buffer . toString ( 'base64' )
731
+ } ;
732
+ } ,
733
+
734
+ isValidDatabaseObject ( object ) {
735
+ return ( object instanceof mongodb . Binary ) ;
736
+ } ,
737
+
738
+ JSONToDatabase ( json ) {
739
+ return new mongodb . Binary ( new Buffer ( json . base64 , 'base64' ) ) ;
740
+ } ,
741
+
742
+ isValidJSON ( value ) {
743
+ return ( typeof value === 'object' &&
744
+ value !== null &&
745
+ value . __type === 'Bytes'
746
+ ) ;
747
+ }
748
+ } ;
749
+
750
+ var GeoPointCoder = {
751
+ databaseToJSON ( object ) {
752
+ return {
753
+ __type : 'GeoPoint' ,
754
+ latitude : object [ 1 ] ,
755
+ longitude : object [ 0 ]
756
+ }
757
+ } ,
758
+
759
+ isValidDatabaseObject ( object ) {
760
+ return ( object instanceof Array &&
761
+ object . length == 2
762
+ ) ;
763
+ } ,
764
+
765
+ JSONToDatabase ( json ) {
766
+ return [ json . longitude , json . latitude ] ;
767
+ } ,
768
+
769
+ isValidJSON ( value ) {
770
+ return ( typeof value === 'object' &&
771
+ value !== null &&
772
+ value . __type === 'GeoPoint'
773
+ ) ;
774
+ }
775
+ } ;
776
+
777
+ var FileCoder = {
778
+ databaseToJSON ( object ) {
779
+ return {
780
+ __type : 'File' ,
781
+ name : object
782
+ }
783
+ } ,
784
+
785
+ JSONToDatabase ( json ) {
786
+ return json . name ;
787
+ } ,
788
+
789
+ isValidJSON ( value ) {
790
+ return ( typeof value === 'object' &&
791
+ value !== null &&
792
+ value . __type === 'File'
793
+ ) ;
794
+ }
795
+ } ;
796
+
729
797
module . exports = {
730
798
transformKey : transformKey ,
731
799
transformCreate : transformCreate ,
0 commit comments