Skip to content

Remove mongo object create format from Parse Server #1516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions spec/MongoTransform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ var dummySchema = {
};


describe('transformCreate', () => {
describe('parseObjectToMongoObject', () => {

it('a basic number', (done) => {
var input = {five: 5};
var output = transform.transformCreate(dummySchema, null, input);
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
jequal(input, output);
done();
});
Expand All @@ -37,7 +37,7 @@ describe('transformCreate', () => {
createdAt: "2015-10-06T21:24:50.332Z",
updatedAt: "2015-10-06T21:24:50.332Z"
};
var output = transform.transformCreate(dummySchema, null, input);
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
expect(output._created_at instanceof Date).toBe(true);
expect(output._updated_at instanceof Date).toBe(true);
done();
Expand All @@ -49,43 +49,43 @@ describe('transformCreate', () => {
objectId: 'myId',
className: 'Blah',
};
var out = transform.transformCreate(dummySchema, null, {pointers: [pointer]});
var out = transform.parseObjectToMongoObject(dummySchema, null, {pointers: [pointer]});
jequal([pointer], out.pointers);
done();
});

it('a delete op', (done) => {
var input = {deleteMe: {__op: 'Delete'}};
var output = transform.transformCreate(dummySchema, null, input);
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
jequal(output, {});
done();
});

it('basic ACL', (done) => {
var input = {ACL: {'0123': {'read': true, 'write': true}}};
var output = transform.transformCreate(dummySchema, null, input);
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
// This just checks that it doesn't crash, but it should check format.
done();
});

describe('GeoPoints', () => {
it('plain', (done) => {
var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180};
var out = transform.transformCreate(dummySchema, null, {location: geoPoint});
var out = transform.parseObjectToMongoObject(dummySchema, null, {location: geoPoint});
expect(out.location).toEqual([180, -180]);
done();
});

it('in array', (done) => {
var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180};
var out = transform.transformCreate(dummySchema, null, {locations: [geoPoint, geoPoint]});
var out = transform.parseObjectToMongoObject(dummySchema, null, {locations: [geoPoint, geoPoint]});
expect(out.locations).toEqual([geoPoint, geoPoint]);
done();
});

it('in sub-object', (done) => {
var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180};
var out = transform.transformCreate(dummySchema, null, { locations: { start: geoPoint }});
var out = transform.parseObjectToMongoObject(dummySchema, null, { locations: { start: geoPoint }});
expect(out).toEqual({ locations: { start: geoPoint } });
done();
});
Expand Down Expand Up @@ -196,7 +196,7 @@ describe('transform schema key changes', () => {
var input = {
somePointer: {__type: 'Pointer', className: 'Micro', objectId: 'oft'}
};
var output = transform.transformCreate(dummySchema, null, input);
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
expect(typeof output._p_somePointer).toEqual('string');
expect(output._p_somePointer).toEqual('Micro$oft');
done();
Expand All @@ -206,7 +206,7 @@ describe('transform schema key changes', () => {
var input = {
userPointer: {__type: 'Pointer', className: '_User', objectId: 'qwerty'}
};
var output = transform.transformCreate(dummySchema, null, input);
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
expect(typeof output._p_userPointer).toEqual('string');
expect(output._p_userPointer).toEqual('_User$qwerty');
done();
Expand All @@ -219,7 +219,7 @@ describe('transform schema key changes', () => {
"Kevin": { "write": true }
}
};
var output = transform.transformCreate(dummySchema, null, input);
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
expect(typeof output._rperm).toEqual('object');
expect(typeof output._wperm).toEqual('object');
expect(output.ACL).toBeUndefined();
Expand Down
9 changes: 9 additions & 0 deletions src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ export class MongoStorageAdapter {
.then(schemaCollection => schemaCollection.updateSchema(className, schemaUpdate));
}

// TODO: As yet not particularly well specified. Creates an object. Does it really need the schema?
// or can it fetch the schema itself? Also the schema is not currently a Parse format schema, and it
// should be, if we are passing it at all.
createObject(className, object, schema) {
const mongoObject = transform.parseObjectToMongoObject(schema, className, object);
return this.adaptiveCollection(className)
.then(collection => collection.insertOne(mongoObject));
}

get transform() {
return transform;
}
Expand Down
6 changes: 2 additions & 4 deletions src/Adapters/Storage/Mongo/MongoTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import _ from 'lodash';
var mongodb = require('mongodb');
var Parse = require('parse/node').Parse;

// TODO: Turn this into a helper library for the database adapter.

// Transforms a key-value pair from REST API form to Mongo form.
// This is the main entry point for converting anything from REST form
// to Mongo form; no conversion should happen that doesn't pass
Expand Down Expand Up @@ -203,7 +201,7 @@ function transformWhere(schema, className, restWhere, options = {validate: true}
// Main exposed method to create new objects.
// restCreate is the "create" clause in REST API form.
// Returns the mongo form of the object.
function transformCreate(schema, className, restCreate) {
function parseObjectToMongoObject(schema, className, restCreate) {
if (className == '_User') {
restCreate = transformAuthData(restCreate);
}
Expand Down Expand Up @@ -940,7 +938,7 @@ var FileCoder = {

module.exports = {
transformKey,
transformCreate,
parseObjectToMongoObject,
transformUpdate,
transformWhere,
transformSelect,
Expand Down
6 changes: 1 addition & 5 deletions src/Controllers/DatabaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,7 @@ DatabaseController.prototype.create = function(className, object, options = {})
return Promise.resolve();
})
.then(() => this.handleRelationUpdates(className, null, object))
.then(() => this.adapter.adaptiveCollection(className))
.then(coll => {
var mongoObject = this.transform.transformCreate(schema, className, object);
return coll.insertOne(mongoObject);
})
.then(() => this.adapter.createObject(className, object, schema))
.then(result => {
return sanitizeDatabaseResult(originalObject, result.ops[0]);
});
Expand Down