Skip to content

Commit fcf971f

Browse files
committed
Merge 132026d into 234d009
2 parents 234d009 + 132026d commit fcf971f

File tree

6 files changed

+71
-134
lines changed

6 files changed

+71
-134
lines changed

spec/MongoTransform.spec.js

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

2525

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

2828
it('a basic number', (done) => {
2929
var input = {five: 5};
30-
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input, {
31-
fields: {five: {type: 'Number'}}
32-
});
30+
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
3331
jequal(input, output);
3432
done();
3533
});
@@ -39,7 +37,7 @@ describe('parseObjectToMongoObjectForCreate', () => {
3937
createdAt: "2015-10-06T21:24:50.332Z",
4038
updatedAt: "2015-10-06T21:24:50.332Z"
4139
};
42-
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input);
40+
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
4341
expect(output._created_at instanceof Date).toBe(true);
4442
expect(output._updated_at instanceof Date).toBe(true);
4543
done();
@@ -51,53 +49,43 @@ describe('parseObjectToMongoObjectForCreate', () => {
5149
objectId: 'myId',
5250
className: 'Blah',
5351
};
54-
var out = transform.parseObjectToMongoObjectForCreate(dummySchema, null, {pointers: [pointer]},{
55-
fields: {pointers: {type: 'Array'}}
56-
});
52+
var out = transform.parseObjectToMongoObject(dummySchema, null, {pointers: [pointer]});
5753
jequal([pointer], out.pointers);
5854
done();
5955
});
6056

61-
//TODO: object creation requests shouldn't be seeing __op delete, it makes no sense to
62-
//have __op delete in a new object. Figure out what this should actually be testing.
63-
notWorking('a delete op', (done) => {
57+
it('a delete op', (done) => {
6458
var input = {deleteMe: {__op: 'Delete'}};
65-
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input);
59+
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
6660
jequal(output, {});
6761
done();
6862
});
6963

7064
it('basic ACL', (done) => {
7165
var input = {ACL: {'0123': {'read': true, 'write': true}}};
72-
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input);
66+
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
7367
// This just checks that it doesn't crash, but it should check format.
7468
done();
7569
});
7670

7771
describe('GeoPoints', () => {
7872
it('plain', (done) => {
7973
var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180};
80-
var out = transform.parseObjectToMongoObjectForCreate(dummySchema, null, {location: geoPoint},{
81-
fields: {location: {type: 'GeoPoint'}}
82-
});
74+
var out = transform.parseObjectToMongoObject(dummySchema, null, {location: geoPoint});
8375
expect(out.location).toEqual([180, -180]);
8476
done();
8577
});
8678

8779
it('in array', (done) => {
8880
var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180};
89-
var out = transform.parseObjectToMongoObjectForCreate(dummySchema, null, {locations: [geoPoint, geoPoint]},{
90-
fields: {locations: {type: 'Array'}}
91-
});
81+
var out = transform.parseObjectToMongoObject(dummySchema, null, {locations: [geoPoint, geoPoint]});
9282
expect(out.locations).toEqual([geoPoint, geoPoint]);
9383
done();
9484
});
9585

9686
it('in sub-object', (done) => {
9787
var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180};
98-
var out = transform.parseObjectToMongoObjectForCreate(dummySchema, null, { locations: { start: geoPoint }},{
99-
fields: {locations: {type: 'Object'}}
100-
});
88+
var out = transform.parseObjectToMongoObject(dummySchema, null, { locations: { start: geoPoint }});
10189
expect(out).toEqual({ locations: { start: geoPoint } });
10290
done();
10391
});
@@ -208,9 +196,7 @@ describe('transform schema key changes', () => {
208196
var input = {
209197
somePointer: {__type: 'Pointer', className: 'Micro', objectId: 'oft'}
210198
};
211-
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input, {
212-
fields: {somePointer: {type: 'Pointer'}}
213-
});
199+
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
214200
expect(typeof output._p_somePointer).toEqual('string');
215201
expect(output._p_somePointer).toEqual('Micro$oft');
216202
done();
@@ -220,9 +206,7 @@ describe('transform schema key changes', () => {
220206
var input = {
221207
userPointer: {__type: 'Pointer', className: '_User', objectId: 'qwerty'}
222208
};
223-
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input, {
224-
fields: {userPointer: {type: 'Pointer'}}
225-
});
209+
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
226210
expect(typeof output._p_userPointer).toEqual('string');
227211
expect(output._p_userPointer).toEqual('_User$qwerty');
228212
done();
@@ -235,7 +219,7 @@ describe('transform schema key changes', () => {
235219
"Kevin": { "write": true }
236220
}
237221
};
238-
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input);
222+
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
239223
expect(typeof output._rperm).toEqual('object');
240224
expect(typeof output._wperm).toEqual('object');
241225
expect(output.ACL).toBeUndefined();

spec/Parse.Push.spec.js

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
let request = require('request');
43

54
describe('Parse.Push', () => {
65

@@ -90,57 +89,4 @@ describe('Parse.Push', () => {
9089
done();
9190
});
9291
});
93-
94-
it('should not allow clients to query _PushStatus', done => {
95-
setup()
96-
.then(() => Parse.Push.send({
97-
where: {
98-
deviceType: 'ios'
99-
},
100-
data: {
101-
badge: 'increment',
102-
alert: 'Hello world!'
103-
}
104-
}, {useMasterKey: true}))
105-
.then(() => {
106-
request.get({
107-
url: 'http://localhost:8378/1/classes/_PushStatus',
108-
json: true,
109-
headers: {
110-
'X-Parse-Application-Id': 'test',
111-
},
112-
}, (error, response, body) => {
113-
expect(body.results.length).toEqual(0);
114-
done();
115-
});
116-
});
117-
});
118-
119-
it('should allow master key to query _PushStatus', done => {
120-
setup()
121-
.then(() => Parse.Push.send({
122-
where: {
123-
deviceType: 'ios'
124-
},
125-
data: {
126-
badge: 'increment',
127-
alert: 'Hello world!'
128-
}
129-
}, {useMasterKey: true}))
130-
.then(() => {
131-
request.get({
132-
url: 'http://localhost:8378/1/classes/_PushStatus',
133-
json: true,
134-
headers: {
135-
'X-Parse-Application-Id': 'test',
136-
'X-Parse-Master-Key': 'test',
137-
},
138-
}, (error, response, body) => {
139-
expect(body.results.length).toEqual(1);
140-
expect(body.results[0].query).toEqual('{"deviceType":"ios"}');
141-
expect(body.results[0].payload).toEqual('{"badge":"increment","alert":"Hello world!"}');
142-
done();
143-
});
144-
});
145-
});
14692
});

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,14 @@ export class MongoStorageAdapter {
155155
// this adapter doesn't know about the schema, return a promise that rejects with
156156
// undefined as the reason.
157157
getOneSchema(className) {
158-
return this.schemaCollection()
159-
.then(schemasCollection => schemasCollection._fechOneSchemaFrom_SCHEMA(className));
158+
return this.schemaCollection().then(schemasCollection => schemasCollection._fechOneSchemaFrom_SCHEMA(className));
160159
}
161160

162-
// TODO: As yet not particularly well specified. Creates an object. Shouldn't need the
163-
// schemaController, but MongoTransform still needs it :( maybe shouldn't even need the schema,
164-
// and should infer from the type. Or maybe does need the schema for validations. Or maybe needs
165-
// the schem only for the legacy mongo format. We'll figure that out later.
166-
createObject(className, object, schemaController, parseFormatSchema) {
167-
const mongoObject = transform.parseObjectToMongoObjectForCreate(schemaController, className, object, parseFormatSchema);
161+
// TODO: As yet not particularly well specified. Creates an object. Does it really need the schema?
162+
// or can it fetch the schema itself? Also the schema is not currently a Parse format schema, and it
163+
// should be, if we are passing it at all.
164+
createObject(className, object, schema) {
165+
const mongoObject = transform.parseObjectToMongoObject(schema, className, object);
168166
return this.adaptiveCollection(className)
169167
.then(collection => collection.insertOne(mongoObject));
170168
}

src/Adapters/Storage/Mongo/MongoTransform.js

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,9 @@ var Parse = require('parse/node').Parse;
2121
// validate: true indicates that key names are to be validated.
2222
//
2323
// Returns an object with {key: key, value: value}.
24-
export function transformKeyValue(schema, className, restKey, restValue, {
25-
inArray,
26-
inObject,
27-
query,
28-
update,
29-
validate,
30-
} = {}) {
24+
export function transformKeyValue(schema, className, restKey, restValue, options) {
25+
options = options || {};
26+
3127
// Check if the schema is known since it's a built-in field.
3228
var key = restKey;
3329
var timeField = false;
@@ -66,7 +62,7 @@ export function transformKeyValue(schema, className, restKey, restValue, {
6662
return {key: key, value: restValue};
6763
break;
6864
case '$or':
69-
if (!query) {
65+
if (!options.query) {
7066
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME,
7167
'you can only use $or in queries');
7268
}
@@ -79,7 +75,7 @@ export function transformKeyValue(schema, className, restKey, restValue, {
7975
});
8076
return {key: '$or', value: mongoSubqueries};
8177
case '$and':
82-
if (!query) {
78+
if (!options.query) {
8379
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME,
8480
'you can only use $and in queries');
8581
}
@@ -95,7 +91,7 @@ export function transformKeyValue(schema, className, restKey, restValue, {
9591
// Other auth data
9692
var authDataMatch = key.match(/^authData\.([a-zA-Z0-9_]+)\.id$/);
9793
if (authDataMatch) {
98-
if (query) {
94+
if (options.query) {
9995
var provider = authDataMatch[1];
10096
// Special-case auth data.
10197
return {key: '_auth_data_'+provider+'.id', value: restValue};
@@ -104,7 +100,7 @@ export function transformKeyValue(schema, className, restKey, restValue, {
104100
'can only query on ' + key);
105101
break;
106102
};
107-
if (validate && !key.match(/^[a-zA-Z][a-zA-Z0-9_\.]*$/)) {
103+
if (options.validate && !key.match(/^[a-zA-Z][a-zA-Z0-9_\.]*$/)) {
108104
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME,
109105
'invalid key name: ' + key);
110106
}
@@ -121,24 +117,24 @@ export function transformKeyValue(schema, className, restKey, restValue, {
121117
(!expected && restValue && restValue.__type == 'Pointer')) {
122118
key = '_p_' + key;
123119
}
124-
var expectedTypeIsArray = (expected && expected.type === 'Array');
120+
var inArray = (expected && expected.type === 'Array');
125121

126122
// Handle query constraints
127-
if (query) {
128-
value = transformConstraint(restValue, expectedTypeIsArray);
123+
if (options.query) {
124+
value = transformConstraint(restValue, inArray);
129125
if (value !== CannotTransform) {
130126
return {key: key, value: value};
131127
}
132128
}
133129

134-
if (expectedTypeIsArray && query && !(restValue instanceof Array)) {
130+
if (inArray && options.query && !(restValue instanceof Array)) {
135131
return {
136132
key: key, value: { '$all' : [restValue] }
137133
};
138134
}
139135

140136
// Handle atomic values
141-
var value = transformAtom(restValue, false, { inArray, inObject });
137+
var value = transformAtom(restValue, false, options);
142138
if (value !== CannotTransform) {
143139
if (timeField && (typeof value === 'string')) {
144140
value = new Date(value);
@@ -154,7 +150,7 @@ export function transformKeyValue(schema, className, restKey, restValue, {
154150

155151
// Handle arrays
156152
if (restValue instanceof Array) {
157-
if (query) {
153+
if (options.query) {
158154
throw new Parse.Error(Parse.Error.INVALID_JSON,
159155
'cannot use array as query param');
160156
}
@@ -166,7 +162,7 @@ export function transformKeyValue(schema, className, restKey, restValue, {
166162
}
167163

168164
// Handle update operators
169-
value = transformUpdateOperator(restValue, !update);
165+
value = transformUpdateOperator(restValue, !options.update);
170166
if (value !== CannotTransform) {
171167
return {key: key, value: value};
172168
}
@@ -290,21 +286,16 @@ const parseObjectKeyValueToMongoObjectKeyValue = (
290286

291287
// Main exposed method to create new objects.
292288
// restCreate is the "create" clause in REST API form.
293-
function parseObjectToMongoObjectForCreate(schema, className, restCreate, parseFormatSchema) {
289+
// Returns the mongo form of the object.
290+
function parseObjectToMongoObject(schema, className, restCreate) {
294291
if (className == '_User') {
295292
restCreate = transformAuthData(restCreate);
296293
}
297294
var mongoCreate = transformACL(restCreate);
298-
for (let restKey in restCreate) {
299-
let { key, value } = parseObjectKeyValueToMongoObjectKeyValue(
300-
schema,
301-
className,
302-
restKey,
303-
restCreate[restKey],
304-
parseFormatSchema
305-
);
306-
if (value !== undefined) {
307-
mongoCreate[key] = value;
295+
for (var restKey in restCreate) {
296+
var out = transformKeyValue(schema, className, restKey, restCreate[restKey]);
297+
if (out.value !== undefined) {
298+
mongoCreate[out.key] = out.value;
308299
}
309300
}
310301
return mongoCreate;
@@ -1006,7 +997,7 @@ var FileCoder = {
1006997

1007998
module.exports = {
1008999
transformKey,
1009-
parseObjectToMongoObjectForCreate,
1000+
parseObjectToMongoObject,
10101001
transformUpdate,
10111002
transformWhere,
10121003
transformSelect,

src/Controllers/DatabaseController.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,19 +336,24 @@ DatabaseController.prototype.create = function(className, object, { acl } = {})
336336
let originalObject = object;
337337
object = deepcopy(object);
338338

339+
var schema;
339340
var isMaster = acl === undefined;
340341
var aclGroup = acl || [];
341342

342343
return this.validateClassName(className)
343-
.then(() => this.loadSchema())
344-
.then(schemaController => {
345-
return (isMaster ? Promise.resolve() : schemaController.validatePermission(className, aclGroup, 'create'))
344+
.then(() => this.loadSchema())
345+
.then(s => {
346+
schema = s;
347+
if (!isMaster) {
348+
return schema.validatePermission(className, aclGroup, 'create');
349+
}
350+
return Promise.resolve();
351+
})
346352
.then(() => this.handleRelationUpdates(className, null, object))
347-
.then(() => schemaController.enforceClassExists(className))
348-
.then(() => schemaController.getOneSchema(className))
349-
.then(schema => this.adapter.createObject(className, object, schemaController, schema))
350-
.then(result => sanitizeDatabaseResult(originalObject, result.ops[0]));
351-
})
353+
.then(() => this.adapter.createObject(className, object, schema))
354+
.then(result => {
355+
return sanitizeDatabaseResult(originalObject, result.ops[0]);
356+
});
352357
};
353358

354359
DatabaseController.prototype.canAddField = function(schema, className, object, aclGroup) {

0 commit comments

Comments
 (0)