Tidy up db controller
This commit is contained in:
@@ -140,7 +140,6 @@ const valueAsDate = value => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function transformQueryKeyValue(schema, className, key, value, { validate } = {}) {
|
function transformQueryKeyValue(schema, className, key, value, { validate } = {}) {
|
||||||
// Check if the schema is known since it's a built-in field.
|
|
||||||
switch(key) {
|
switch(key) {
|
||||||
case 'createdAt':
|
case 'createdAt':
|
||||||
if (valueAsDate(value)) {
|
if (valueAsDate(value)) {
|
||||||
@@ -188,9 +187,6 @@ function transformQueryKeyValue(schema, className, key, value, { validate } = {}
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle special schema key changes
|
|
||||||
// TODO: it seems like this is likely to have edge cases where
|
|
||||||
// pointer types are missed
|
|
||||||
let expected = undefined;
|
let expected = undefined;
|
||||||
if (schema && schema.getExpectedType) {
|
if (schema && schema.getExpectedType) {
|
||||||
expected = schema.getExpectedType(className, key);
|
expected = schema.getExpectedType(className, key);
|
||||||
|
|||||||
@@ -158,20 +158,15 @@ DatabaseController.prototype.update = function(className, query, update, {
|
|||||||
|
|
||||||
var isMaster = acl === undefined;
|
var isMaster = acl === undefined;
|
||||||
var aclGroup = acl || [];
|
var aclGroup = acl || [];
|
||||||
var mongoUpdate, schema;
|
var mongoUpdate;
|
||||||
return this.loadSchema()
|
return this.loadSchema()
|
||||||
.then(s => {
|
.then(schemaController => {
|
||||||
schema = s;
|
return (isMaster ? Promise.resolve() : schemaController.validatePermission(className, aclGroup, 'update'))
|
||||||
if (!isMaster) {
|
|
||||||
return schema.validatePermission(className, aclGroup, 'update');
|
|
||||||
}
|
|
||||||
return Promise.resolve();
|
|
||||||
})
|
|
||||||
.then(() => this.handleRelationUpdates(className, query.objectId, update))
|
.then(() => this.handleRelationUpdates(className, query.objectId, update))
|
||||||
.then(() => this.adapter.adaptiveCollection(className))
|
.then(() => this.adapter.adaptiveCollection(className))
|
||||||
.then(collection => {
|
.then(collection => {
|
||||||
if (!isMaster) {
|
if (!isMaster) {
|
||||||
query = this.addPointerPermissions(schema, className, 'update', query, aclGroup);
|
query = this.addPointerPermissions(schemaController, className, 'update', query, aclGroup);
|
||||||
}
|
}
|
||||||
if (!query) {
|
if (!query) {
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
@@ -179,8 +174,18 @@ DatabaseController.prototype.update = function(className, query, update, {
|
|||||||
if (acl) {
|
if (acl) {
|
||||||
query = addWriteACL(query, acl);
|
query = addWriteACL(query, acl);
|
||||||
}
|
}
|
||||||
var mongoWhere = this.transform.transformWhere(schema, className, query, {validate: !this.skipValidation});
|
var mongoWhere = this.transform.transformWhere(
|
||||||
mongoUpdate = this.transform.transformUpdate(schema, className, update, {validate: !this.skipValidation});
|
schemaController,
|
||||||
|
className,
|
||||||
|
query,
|
||||||
|
{validate: !this.skipValidation}
|
||||||
|
);
|
||||||
|
mongoUpdate = this.transform.transformUpdate(
|
||||||
|
schemaController,
|
||||||
|
className,
|
||||||
|
update,
|
||||||
|
{validate: !this.skipValidation}
|
||||||
|
);
|
||||||
if (many) {
|
if (many) {
|
||||||
return collection.updateMany(mongoWhere, mongoUpdate);
|
return collection.updateMany(mongoWhere, mongoUpdate);
|
||||||
} else if (upsert) {
|
} else if (upsert) {
|
||||||
@@ -191,14 +196,14 @@ DatabaseController.prototype.update = function(className, query, update, {
|
|||||||
})
|
})
|
||||||
.then(result => {
|
.then(result => {
|
||||||
if (!result) {
|
if (!result) {
|
||||||
return Promise.reject(new Parse.Error(Parse.Error.OBJECT_NOT_FOUND,
|
return Promise.reject(new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Object not found.'));
|
||||||
'Object not found.'));
|
|
||||||
}
|
}
|
||||||
if (this.skipValidation) {
|
if (this.skipValidation) {
|
||||||
return Promise.resolve(result);
|
return Promise.resolve(result);
|
||||||
}
|
}
|
||||||
return sanitizeDatabaseResult(originalUpdate, result);
|
return sanitizeDatabaseResult(originalUpdate, result);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function sanitizeDatabaseResult(originalObject, result) {
|
function sanitizeDatabaseResult(originalObject, result) {
|
||||||
|
|||||||
Reference in New Issue
Block a user