Skip to content

Commit ac839f7

Browse files
drew-grossflovilmart
authored andcommitted
Remove mongo object create format from Parse Server (#1516)
1 parent c57c6b3 commit ac839f7

File tree

4 files changed

+24
-21
lines changed

4 files changed

+24
-21
lines changed

spec/MongoTransform.spec.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ var dummySchema = {
2323
};
2424

2525

26-
describe('transformCreate', () => {
26+
describe('parseObjectToMongoObject', () => {
2727

2828
it('a basic number', (done) => {
2929
var input = {five: 5};
30-
var output = transform.transformCreate(dummySchema, null, input);
30+
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
3131
jequal(input, output);
3232
done();
3333
});
@@ -37,7 +37,7 @@ describe('transformCreate', () => {
3737
createdAt: "2015-10-06T21:24:50.332Z",
3838
updatedAt: "2015-10-06T21:24:50.332Z"
3939
};
40-
var output = transform.transformCreate(dummySchema, null, input);
40+
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
4141
expect(output._created_at instanceof Date).toBe(true);
4242
expect(output._updated_at instanceof Date).toBe(true);
4343
done();
@@ -49,43 +49,43 @@ describe('transformCreate', () => {
4949
objectId: 'myId',
5050
className: 'Blah',
5151
};
52-
var out = transform.transformCreate(dummySchema, null, {pointers: [pointer]});
52+
var out = transform.parseObjectToMongoObject(dummySchema, null, {pointers: [pointer]});
5353
jequal([pointer], out.pointers);
5454
done();
5555
});
5656

5757
it('a delete op', (done) => {
5858
var input = {deleteMe: {__op: 'Delete'}};
59-
var output = transform.transformCreate(dummySchema, null, input);
59+
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
6060
jequal(output, {});
6161
done();
6262
});
6363

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

7171
describe('GeoPoints', () => {
7272
it('plain', (done) => {
7373
var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180};
74-
var out = transform.transformCreate(dummySchema, null, {location: geoPoint});
74+
var out = transform.parseObjectToMongoObject(dummySchema, null, {location: geoPoint});
7575
expect(out.location).toEqual([180, -180]);
7676
done();
7777
});
7878

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

8686
it('in sub-object', (done) => {
8787
var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180};
88-
var out = transform.transformCreate(dummySchema, null, { locations: { start: geoPoint }});
88+
var out = transform.parseObjectToMongoObject(dummySchema, null, { locations: { start: geoPoint }});
8989
expect(out).toEqual({ locations: { start: geoPoint } });
9090
done();
9191
});
@@ -196,7 +196,7 @@ describe('transform schema key changes', () => {
196196
var input = {
197197
somePointer: {__type: 'Pointer', className: 'Micro', objectId: 'oft'}
198198
};
199-
var output = transform.transformCreate(dummySchema, null, input);
199+
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
200200
expect(typeof output._p_somePointer).toEqual('string');
201201
expect(output._p_somePointer).toEqual('Micro$oft');
202202
done();
@@ -206,7 +206,7 @@ describe('transform schema key changes', () => {
206206
var input = {
207207
userPointer: {__type: 'Pointer', className: '_User', objectId: 'qwerty'}
208208
};
209-
var output = transform.transformCreate(dummySchema, null, input);
209+
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
210210
expect(typeof output._p_userPointer).toEqual('string');
211211
expect(output._p_userPointer).toEqual('_User$qwerty');
212212
done();
@@ -219,7 +219,7 @@ describe('transform schema key changes', () => {
219219
"Kevin": { "write": true }
220220
}
221221
};
222-
var output = transform.transformCreate(dummySchema, null, input);
222+
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
223223
expect(typeof output._rperm).toEqual('object');
224224
expect(typeof output._wperm).toEqual('object');
225225
expect(output.ACL).toBeUndefined();

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ export class MongoStorageAdapter {
127127
.then(schemaCollection => schemaCollection.updateSchema(className, schemaUpdate));
128128
}
129129

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

src/Adapters/Storage/Mongo/MongoTransform.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import _ from 'lodash';
33
var mongodb = require('mongodb');
44
var Parse = require('parse/node').Parse;
55

6-
// TODO: Turn this into a helper library for the database adapter.
7-
86
// Transforms a key-value pair from REST API form to Mongo form.
97
// This is the main entry point for converting anything from REST form
108
// to Mongo form; no conversion should happen that doesn't pass
@@ -203,7 +201,7 @@ function transformWhere(schema, className, restWhere, options = {validate: true}
203201
// Main exposed method to create new objects.
204202
// restCreate is the "create" clause in REST API form.
205203
// Returns the mongo form of the object.
206-
function transformCreate(schema, className, restCreate) {
204+
function parseObjectToMongoObject(schema, className, restCreate) {
207205
if (className == '_User') {
208206
restCreate = transformAuthData(restCreate);
209207
}
@@ -940,7 +938,7 @@ var FileCoder = {
940938

941939
module.exports = {
942940
transformKey,
943-
transformCreate,
941+
parseObjectToMongoObject,
944942
transformUpdate,
945943
transformWhere,
946944
transformSelect,

src/Controllers/DatabaseController.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,7 @@ DatabaseController.prototype.create = function(className, object, options = {})
344344
return Promise.resolve();
345345
})
346346
.then(() => this.handleRelationUpdates(className, null, object))
347-
.then(() => this.adapter.adaptiveCollection(className))
348-
.then(coll => {
349-
var mongoObject = this.transform.transformCreate(schema, className, object);
350-
return coll.insertOne(mongoObject);
351-
})
347+
.then(() => this.adapter.createObject(className, object, schema))
352348
.then(result => {
353349
return sanitizeDatabaseResult(originalObject, result.ops[0]);
354350
});

0 commit comments

Comments
 (0)