Skip to content

Commit 32ebc90

Browse files
committed
Split basic type conversions from other logic in transform.js.
1 parent ee5e06c commit 32ebc90

File tree

1 file changed

+96
-28
lines changed

1 file changed

+96
-28
lines changed

transform.js

Lines changed: 96 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -363,23 +363,17 @@ function transformAtom(atom, force, options) {
363363
objectId: atom.objectId
364364
};
365365
}
366-
if (atom.__type == 'Date') {
367-
return new Date(atom.iso);
366+
if (DateCoder.isValidJSON(atom)) {
367+
return DateCoder.JSONToDatabase(atom);
368368
}
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);
374371
}
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));
377374
}
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));
383377
}
384378

385379
if (force) {
@@ -620,11 +614,8 @@ function untransformObject(schema, className, mongoObject) {
620614
return Parse._encode(mongoObject);
621615
}
622616

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);
628619
}
629620

630621
var restObject = untransformACL(mongoObject);
@@ -701,18 +692,11 @@ function untransformObject(schema, className, mongoObject) {
701692
} else {
702693
var expected = schema.getExpectedType(className, key);
703694
if (expected == 'file' && mongoObject[key]) {
704-
restObject[key] = {
705-
__type: 'File',
706-
name: mongoObject[key]
707-
};
695+
restObject[key] = FileCoder.databaseToJSON(mongoObject[key]);
708696
break;
709697
}
710698
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]);
716700
break;
717701
}
718702
}
@@ -726,6 +710,90 @@ function untransformObject(schema, className, mongoObject) {
726710
}
727711
}
728712

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+
729797
module.exports = {
730798
transformKey: transformKey,
731799
transformCreate: transformCreate,

0 commit comments

Comments
 (0)