Split basic type conversions from other logic in transform.js.
This commit is contained in:
135
transform.js
135
transform.js
@@ -363,23 +363,17 @@ function transformAtom(atom, force, options) {
|
|||||||
objectId: atom.objectId
|
objectId: atom.objectId
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (atom.__type == 'Date') {
|
if (DateCoder.isValidJSON(atom)) {
|
||||||
return new Date(atom.iso);
|
return DateCoder.JSONToDatabase(atom);
|
||||||
}
|
}
|
||||||
if (atom.__type == 'GeoPoint') {
|
if (BytesCoder.isValidJSON(atom)) {
|
||||||
if (!inArray && !inObject) {
|
return BytesCoder.JSONToDatabase(atom);
|
||||||
return [atom.longitude, atom.latitude];
|
|
||||||
}
|
|
||||||
return atom;
|
|
||||||
}
|
}
|
||||||
if (atom.__type == 'Bytes') {
|
if (GeoPointCoder.isValidJSON(atom)) {
|
||||||
return new mongodb.Binary(new Buffer(atom.base64, 'base64'));
|
return (inArray || inObject ? atom : GeoPointCoder.JSONToDatabase(atom));
|
||||||
}
|
}
|
||||||
if (atom.__type == 'File') {
|
if (FileCoder.isValidJSON(atom)) {
|
||||||
if (!inArray && !inObject) {
|
return (inArray || inObject ? atom : FileCoder.JSONToDatabase(atom));
|
||||||
return atom.name;
|
|
||||||
}
|
|
||||||
return atom;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (force) {
|
if (force) {
|
||||||
@@ -620,11 +614,8 @@ function untransformObject(schema, className, mongoObject) {
|
|||||||
return Parse._encode(mongoObject);
|
return Parse._encode(mongoObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mongoObject instanceof mongodb.Binary) {
|
if (BytesCoder.isValidDatabaseObject(mongoObject)) {
|
||||||
return {
|
return BytesCoder.databaseToJSON(mongoObject);
|
||||||
__type: 'Bytes',
|
|
||||||
base64: mongoObject.buffer.toString('base64')
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var restObject = untransformACL(mongoObject);
|
var restObject = untransformACL(mongoObject);
|
||||||
@@ -699,20 +690,14 @@ function untransformObject(schema, className, mongoObject) {
|
|||||||
//} else if (mongoObject[key] === null) {
|
//} else if (mongoObject[key] === null) {
|
||||||
//break;
|
//break;
|
||||||
} else {
|
} else {
|
||||||
var expected = schema.getExpectedType(className, key);
|
var expectedType = schema.getExpectedType(className, key);
|
||||||
if (expected == 'file' && mongoObject[key]) {
|
var value = mongoObject[key];
|
||||||
restObject[key] = {
|
if (expectedType === 'file' && FileCoder.isValidDatabaseObject(value)) {
|
||||||
__type: 'File',
|
restObject[key] = FileCoder.databaseToJSON(value);
|
||||||
name: mongoObject[key]
|
|
||||||
};
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (expected == 'geopoint') {
|
if (expectedType === 'geopoint' && GeoPointCoder.isValidDatabaseObject(value)) {
|
||||||
restObject[key] = {
|
restObject[key] = GeoPointCoder.databaseToJSON(value);
|
||||||
__type: 'GeoPoint',
|
|
||||||
latitude: mongoObject[key][1],
|
|
||||||
longitude: mongoObject[key][0]
|
|
||||||
};
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -726,6 +711,94 @@ 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
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
isValidDatabaseObject(object) {
|
||||||
|
return (typeof object === 'string');
|
||||||
|
},
|
||||||
|
|
||||||
|
JSONToDatabase(json) {
|
||||||
|
return json.name;
|
||||||
|
},
|
||||||
|
|
||||||
|
isValidJSON(value) {
|
||||||
|
return (typeof value === 'object' &&
|
||||||
|
value !== null &&
|
||||||
|
value.__type === 'File'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
transformKey: transformKey,
|
transformKey: transformKey,
|
||||||
transformCreate: transformCreate,
|
transformCreate: transformCreate,
|
||||||
|
|||||||
Reference in New Issue
Block a user