diff --git a/spec/MongoTransform.spec.js b/spec/MongoTransform.spec.js index 6a6ba9f7b2..f3eba2ffcd 100644 --- a/spec/MongoTransform.spec.js +++ b/spec/MongoTransform.spec.js @@ -156,6 +156,18 @@ describe('parseObjectToMongoObjectForCreate', () => { done(); }); + it('bytes', (done) => { + var input = {binaryData: "aGVsbG8gd29ybGQ="}; + var output = transform.mongoObjectToParseObject(null, input, { + fields: { binaryData: { type: 'Bytes' }}, + }); + expect(typeof output.binaryData).toEqual('object'); + expect(output.binaryData).toEqual( + {__type: 'Bytes', base64: "aGVsbG8gd29ybGQ="} + ); + done(); + }); + it('nested array', (done) => { var input = {arr: [{_testKey: 'testValue' }]}; var output = transform.mongoObjectToParseObject(null, input, { diff --git a/src/Adapters/Storage/Mongo/MongoTransform.js b/src/Adapters/Storage/Mongo/MongoTransform.js index cd7408f1fb..9ea1742d77 100644 --- a/src/Adapters/Storage/Mongo/MongoTransform.js +++ b/src/Adapters/Storage/Mongo/MongoTransform.js @@ -781,6 +781,10 @@ const mongoObjectToParseObject = (className, mongoObject, schema) => { restObject[key] = GeoPointCoder.databaseToJSON(value); break; } + if (schema.fields[key] && schema.fields[key].type === 'Bytes' && BytesCoder.isValidDatabaseObject(value)) { + restObject[key] = BytesCoder.databaseToJSON(value); + break; + } } restObject[key] = nestedMongoObjectToNestedParseObject(mongoObject[key]); } @@ -815,15 +819,29 @@ var DateCoder = { }; var BytesCoder = { + base64Pattern: new RegExp("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"), + isBase64Value(object) { + if (typeof object !== 'string') { + return false; + } + return this.base64Pattern.test(object); + }, + databaseToJSON(object) { + let value; + if (this.isBase64Value(object)) { + value = object; + } else { + value = object.buffer.toString('base64'); + } return { __type: 'Bytes', - base64: object.buffer.toString('base64') + base64: value }; }, - isValidDatabaseObject(object) { - return (object instanceof mongodb.Binary); + isValidDatabaseObject(object) { + return (object instanceof mongodb.Binary) || this.isBase64Value(object); }, JSONToDatabase(json) {