Skip to content

Commit 4371ca1

Browse files
committed
Pass parse format schema to transformWhere
1 parent 608cba9 commit 4371ca1

File tree

3 files changed

+77
-38
lines changed

3 files changed

+77
-38
lines changed

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,15 @@ export class MongoStorageAdapter {
174174
// If there is some other error, reject with INTERNAL_SERVER_ERROR.
175175

176176
// Currently accepts the schemaController, and validate for lecacy reasons
177-
deleteObjectsByQuery(className, query, schemaController, validate) {
177+
deleteObjectsByQuery(className, query, schemaController, validate, parseFormatSchema) {
178178
return this.adaptiveCollection(className)
179179
.then(collection => {
180180
let mongoWhere = transform.transformWhere(
181181
schemaController,
182182
className,
183183
query,
184-
{ validate }
184+
{ validate },
185+
parseFormatSchema
185186
);
186187
return collection.deleteMany(mongoWhere)
187188
})

src/Adapters/Storage/Mongo/MongoTransform.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ const valueAsDate = value => {
139139
return false;
140140
}
141141

142-
function transformQueryKeyValue(schema, className, key, value, { validate } = {}) {
142+
function transformQueryKeyValue(schema, className, key, value, { validate } = {}, parseFormatSchema) {
143143
switch(key) {
144144
case 'createdAt':
145145
if (valueAsDate(value)) {
@@ -168,12 +168,12 @@ function transformQueryKeyValue(schema, className, key, value, { validate } = {}
168168
if (!(value instanceof Array)) {
169169
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'bad $or format - use an array value');
170170
}
171-
return {key: '$or', value: value.map(subQuery => transformWhere(schema, className, subQuery))};
171+
return {key: '$or', value: value.map(subQuery => transformWhere(schema, className, subQuery, parseFormatSchema))};
172172
case '$and':
173173
if (!(value instanceof Array)) {
174174
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'bad $and format - use an array value');
175175
}
176-
return {key: '$and', value: value.map(subQuery => transformWhere(schema, className, subQuery))};
176+
return {key: '$and', value: value.map(subQuery => transformWhere(schema, className, subQuery, parseFormatSchema))};
177177
default:
178178
// Other auth data
179179
const authDataMatch = key.match(/^authData\.([a-zA-Z0-9_]+)\.id$/);
@@ -218,13 +218,13 @@ function transformQueryKeyValue(schema, className, key, value, { validate } = {}
218218
// restWhere is the "where" clause in REST API form.
219219
// Returns the mongo form of the query.
220220
// Throws a Parse.Error if the input query is invalid.
221-
function transformWhere(schema, className, restWhere, { validate = true } = {}) {
221+
function transformWhere(schema, className, restWhere, { validate = true } = {}, parseFormatSchema) {
222222
let mongoWhere = {};
223223
if (restWhere['ACL']) {
224224
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Cannot query on ACL.');
225225
}
226226
for (let restKey in restWhere) {
227-
let out = transformQueryKeyValue(schema, className, restKey, restWhere[restKey], { validate });
227+
let out = transformQueryKeyValue(schema, className, restKey, restWhere[restKey], { validate }, parseFormatSchema);
228228
mongoWhere[out.key] = out.value;
229229
}
230230
return mongoWhere;

src/Controllers/DatabaseController.js

Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -174,25 +174,37 @@ DatabaseController.prototype.update = function(className, query, update, {
174174
if (acl) {
175175
query = addWriteACL(query, acl);
176176
}
177-
var mongoWhere = this.transform.transformWhere(
178-
schemaController,
179-
className,
180-
query,
181-
{validate: !this.skipValidation}
182-
);
183-
mongoUpdate = this.transform.transformUpdate(
184-
schemaController,
185-
className,
186-
update,
187-
{validate: !this.skipValidation}
188-
);
189-
if (many) {
190-
return collection.updateMany(mongoWhere, mongoUpdate);
191-
} else if (upsert) {
192-
return collection.upsertOne(mongoWhere, mongoUpdate);
193-
} else {
194-
return collection.findOneAndUpdate(mongoWhere, mongoUpdate);
195-
}
177+
return schemaController.getOneSchema(className)
178+
.catch(error => {
179+
// If the schema doesn't exist, pretend it exists with no fields. This behaviour
180+
// will likely need revisiting.
181+
if (error === undefined) {
182+
return { fields: {} };
183+
}
184+
throw error;
185+
})
186+
.then(parseFormatSchema => {
187+
var mongoWhere = this.transform.transformWhere(
188+
schemaController,
189+
className,
190+
query,
191+
{validate: !this.skipValidation},
192+
parseFormatSchema
193+
);
194+
mongoUpdate = this.transform.transformUpdate(
195+
schemaController,
196+
className,
197+
update,
198+
{validate: !this.skipValidation}
199+
);
200+
if (many) {
201+
return collection.updateMany(mongoWhere, mongoUpdate);
202+
} else if (upsert) {
203+
return collection.upsertOne(mongoWhere, mongoUpdate);
204+
} else {
205+
return collection.findOneAndUpdate(mongoWhere, mongoUpdate);
206+
}
207+
});
196208
})
197209
.then(result => {
198210
if (!result) {
@@ -322,7 +334,22 @@ DatabaseController.prototype.destroy = function(className, query, { acl } = {})
322334
if (acl) {
323335
query = addWriteACL(query, acl);
324336
}
325-
return this.adapter.deleteObjectsByQuery(className, query, schemaController, !this.skipValidation)
337+
return schemaController.getOneSchema(className)
338+
.catch(error => {
339+
// If the schema doesn't exist, pretend it exists with no fields. This behaviour
340+
// will likely need revisiting.
341+
if (error === undefined) {
342+
return { fields: {} };
343+
}
344+
throw error;
345+
})
346+
.then(parseFormatSchema => this.adapter.deleteObjectsByQuery(
347+
className,
348+
query,
349+
schemaController,
350+
!this.skipValidation,
351+
parseFormatSchema
352+
))
326353
.catch(error => {
327354
// When deleting sessions while changing passwords, don't throw an error if they don't have any sessions.
328355
if (className === "_Session" && error.code === Parse.Error.OBJECT_NOT_FOUND) {
@@ -627,18 +654,29 @@ DatabaseController.prototype.find = function(className, query, {
627654
if (!isMaster) {
628655
query = addReadACL(query, aclGroup);
629656
}
630-
let mongoWhere = this.transform.transformWhere(schemaController, className, query);
631-
if (count) {
632-
delete mongoOptions.limit;
633-
return collection.count(mongoWhere, mongoOptions);
634-
} else {
635-
return collection.find(mongoWhere, mongoOptions)
636-
.then((mongoResults) => {
637-
return mongoResults.map((r) => {
638-
return this.untransformObject(schemaController, isMaster, aclGroup, className, r);
657+
return schemaController.getOneSchema(className)
658+
.catch(error => {
659+
// If the schema doesn't exist, pretend it exists with no fields. This behaviour
660+
// will likely need revisiting.
661+
if (error === undefined) {
662+
return { fields: {} };
663+
}
664+
throw error;
665+
})
666+
.then(parseFormatSchema => {
667+
let mongoWhere = this.transform.transformWhere(schemaController, className, query, parseFormatSchema);
668+
if (count) {
669+
delete mongoOptions.limit;
670+
return collection.count(mongoWhere, mongoOptions);
671+
} else {
672+
return collection.find(mongoWhere, mongoOptions)
673+
.then((mongoResults) => {
674+
return mongoResults.map((r) => {
675+
return this.untransformObject(schemaController, isMaster, aclGroup, className, r);
676+
});
639677
});
640-
});
641-
}
678+
}
679+
});
642680
});
643681
});
644682
};

0 commit comments

Comments
 (0)