Skip to content

Commit

Permalink
Split basic type conversions from other logic in transform.js.
Browse files Browse the repository at this point in the history
  • Loading branch information
nlutsenko committed Feb 4, 2016
1 parent ee5e06c commit 32ebc90
Showing 1 changed file with 96 additions and 28 deletions.
124 changes: 96 additions & 28 deletions transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,23 +363,17 @@ function transformAtom(atom, force, options) {
objectId: atom.objectId
};
}
if (atom.__type == 'Date') {
return new Date(atom.iso);
if (DateCoder.isValidJSON(atom)) {
return DateCoder.JSONToDatabase(atom);
}
if (atom.__type == 'GeoPoint') {
if (!inArray && !inObject) {
return [atom.longitude, atom.latitude];
}
return atom;
if (BytesCoder.isValidJSON(atom)) {
return BytesCoder.JSONToDatabase(atom);
}
if (atom.__type == 'Bytes') {
return new mongodb.Binary(new Buffer(atom.base64, 'base64'));
if (GeoPointCoder.isValidJSON(atom)) {
return (inArray || inObject ? atom : GeoPointCoder.JSONToDatabase(atom));
}
if (atom.__type == 'File') {
if (!inArray && !inObject) {
return atom.name;
}
return atom;
if (FileCoder.isValidJSON(atom)) {
return (inArray || inObject ? atom : FileCoder.JSONToDatabase(atom));
}

if (force) {
Expand Down Expand Up @@ -620,11 +614,8 @@ function untransformObject(schema, className, mongoObject) {
return Parse._encode(mongoObject);
}

if (mongoObject instanceof mongodb.Binary) {
return {
__type: 'Bytes',
base64: mongoObject.buffer.toString('base64')
};
if (BytesCoder.isValidDatabaseObject(mongoObject)) {
return BytesCoder.databaseToJSON(mongoObject);
}

var restObject = untransformACL(mongoObject);
Expand Down Expand Up @@ -701,18 +692,11 @@ function untransformObject(schema, className, mongoObject) {
} else {
var expected = schema.getExpectedType(className, key);
if (expected == 'file' && mongoObject[key]) {
restObject[key] = {
__type: 'File',
name: mongoObject[key]
};
restObject[key] = FileCoder.databaseToJSON(mongoObject[key]);
break;
}
if (expected == 'geopoint') {
restObject[key] = {
__type: 'GeoPoint',
latitude: mongoObject[key][1],
longitude: mongoObject[key][0]
};
restObject[key] = GeoPointCoder.databaseToJSON(mongoObject[key]);
break;
}
}
Expand All @@ -726,6 +710,90 @@ function untransformObject(schema, className, mongoObject) {
}
}

var DateCoder = {
JSONToDatabase(json) {
return new Date(json.iso);
},

isValidJSON(value) {
return (typeof value === 'object' &&
value !== null &&
value.__type === 'Date'
);
}
};

var BytesCoder = {
databaseToJSON(object) {
return {
__type: 'Bytes',
base64: object.buffer.toString('base64')
};
},

isValidDatabaseObject(object) {
return (object instanceof mongodb.Binary);
},

JSONToDatabase(json) {
return new mongodb.Binary(new Buffer(json.base64, 'base64'));
},

isValidJSON(value) {
return (typeof value === 'object' &&
value !== null &&
value.__type === 'Bytes'
);
}
};

var GeoPointCoder = {
databaseToJSON(object) {
return {
__type: 'GeoPoint',
latitude: object[1],
longitude: object[0]
}
},

isValidDatabaseObject(object) {
return (object instanceof Array &&
object.length == 2
);
},

JSONToDatabase(json) {
return [ json.longitude, json.latitude ];
},

isValidJSON(value) {
return (typeof value === 'object' &&
value !== null &&
value.__type === 'GeoPoint'
);
}
};

var FileCoder = {
databaseToJSON(object) {
return {
__type: 'File',
name: object
}
},

JSONToDatabase(json) {
return json.name;
},

isValidJSON(value) {
return (typeof value === 'object' &&
value !== null &&
value.__type === 'File'
);
}
};

module.exports = {
transformKey: transformKey,
transformCreate: transformCreate,
Expand Down

0 comments on commit 32ebc90

Please sign in to comment.