Remove usages of non-adaptive collection inside DatabaseController.

This commit is contained in:
Nikita Lutsenko
2016-03-07 19:26:40 -08:00
parent 8d10447c9f
commit a163327ac9
3 changed files with 74 additions and 68 deletions

View File

@@ -57,6 +57,10 @@ export default class MongoCollection {
}) })
} }
insertOne(object) {
return this._mongoCollection.insertOne(object);
}
// Atomically updates data in the database for a single (first) object that matched the query // Atomically updates data in the database for a single (first) object that matched the query
// If there is nothing that matches the query - does insert // If there is nothing that matches the query - does insert
// Postgres Note: `INSERT ... ON CONFLICT UPDATE` that is available since 9.5. // Postgres Note: `INSERT ... ON CONFLICT UPDATE` that is available since 9.5.
@@ -83,8 +87,8 @@ export default class MongoCollection {
return this._mongoCollection.deleteOne(query); return this._mongoCollection.deleteOne(query);
} }
remove(query) { deleteMany(query) {
return this._mongoCollection.remove(query); return this._mongoCollection.deleteMany(query);
} }
drop() { drop() {

View File

@@ -54,6 +54,14 @@ function returnsTrue() {
return true; return true;
} }
DatabaseController.prototype.validateClassName = function(className) {
if (!Schema.classNameIsValid(className)) {
const error = new Parse.Error(Parse.Error.INVALID_CLASS_NAME, 'invalid className: ' + className);
return Promise.reject(error);
}
return Promise.resolve();
};
// Returns a promise for a schema object. // Returns a promise for a schema object.
// If we are provided a acceptor, then we run it on the schema. // If we are provided a acceptor, then we run it on the schema.
// If the schema isn't accepted, we reload it at most once. // If the schema isn't accepted, we reload it at most once.
@@ -230,30 +238,28 @@ DatabaseController.prototype.handleRelationUpdates = function(className,
// Adds a relation. // Adds a relation.
// Returns a promise that resolves successfully iff the add was successful. // Returns a promise that resolves successfully iff the add was successful.
DatabaseController.prototype.addRelation = function(key, fromClassName, DatabaseController.prototype.addRelation = function(key, fromClassName, fromId, toId) {
fromId, toId) { let doc = {
var doc = {
relatedId: toId, relatedId: toId,
owningId: fromId owningId : fromId
}; };
var className = '_Join:' + key + ':' + fromClassName; let className = `_Join:${key}:${fromClassName}`;
return this.collection(className).then((coll) => { return this.adaptiveCollection(className).then((coll) => {
return coll.update(doc, doc, {upsert: true}); return coll.upsertOne(doc, doc);
}); });
}; };
// Removes a relation. // Removes a relation.
// Returns a promise that resolves successfully iff the remove was // Returns a promise that resolves successfully iff the remove was
// successful. // successful.
DatabaseController.prototype.removeRelation = function(key, fromClassName, DatabaseController.prototype.removeRelation = function(key, fromClassName, fromId, toId) {
fromId, toId) {
var doc = { var doc = {
relatedId: toId, relatedId: toId,
owningId: fromId owningId: fromId
}; };
var className = '_Join:' + key + ':' + fromClassName; let className = `_Join:${key}:${fromClassName}`;
return this.collection(className).then((coll) => { return this.adaptiveCollection(className).then(coll => {
return coll.remove(doc); return coll.deleteOne(doc);
}); });
}; };
@@ -269,40 +275,36 @@ DatabaseController.prototype.destroy = function(className, query, options = {})
var aclGroup = options.acl || []; var aclGroup = options.acl || [];
var schema; var schema;
return this.loadSchema().then((s) => { return this.loadSchema()
schema = s; .then(s => {
if (!isMaster) { schema = s;
return schema.validatePermission(className, aclGroup, 'delete'); if (!isMaster) {
} return schema.validatePermission(className, aclGroup, 'delete');
return Promise.resolve();
}).then(() => {
return this.collection(className);
}).then((coll) => {
var mongoWhere = transform.transformWhere(schema, className, query);
if (options.acl) {
var writePerms = [
{_wperm: {'$exists': false}}
];
for (var entry of options.acl) {
writePerms.push({_wperm: {'$in': [entry]}});
} }
mongoWhere = {'$and': [mongoWhere, {'$or': writePerms}]}; return Promise.resolve();
} })
.then(() => this.adaptiveCollection(className))
.then(collection => {
let mongoWhere = transform.transformWhere(schema, className, query);
return coll.remove(mongoWhere); if (options.acl) {
}).then((resp) => { var writePerms = [
//Check _Session to avoid changing password failed without any session. { _wperm: { '$exists': false } }
if (resp.result.n === 0 && className !== "_Session") { ];
return Promise.reject( for (var entry of options.acl) {
new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, writePerms.push({ _wperm: { '$in': [entry] } });
'Object not found.')); }
mongoWhere = { '$and': [mongoWhere, { '$or': writePerms }] };
} }
}, (error) => { return collection.deleteMany(mongoWhere);
throw error; })
}); .then(resp => {
//Check _Session to avoid changing password failed without any session.
// TODO: @nlutsenko Stop relying on `result.n`
if (resp.result.n === 0 && className !== "_Session") {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Object not found.');
}
});
}; };
// Inserts an object into the database. // Inserts an object into the database.
@@ -312,21 +314,21 @@ DatabaseController.prototype.create = function(className, object, options) {
var isMaster = !('acl' in options); var isMaster = !('acl' in options);
var aclGroup = options.acl || []; var aclGroup = options.acl || [];
return this.loadSchema().then((s) => { return this.validateClassName(className)
schema = s; .then(() => this.loadSchema())
if (!isMaster) { .then(s => {
return schema.validatePermission(className, aclGroup, 'create'); schema = s;
} if (!isMaster) {
return Promise.resolve(); return schema.validatePermission(className, aclGroup, 'create');
}).then(() => { }
return Promise.resolve();
return this.handleRelationUpdates(className, null, object); })
}).then(() => { .then(() => this.handleRelationUpdates(className, null, object))
return this.collection(className); .then(() => this.adaptiveCollection(className))
}).then((coll) => { .then(coll => {
var mongoObject = transform.transformCreate(schema, className, object); var mongoObject = transform.transformCreate(schema, className, object);
return coll.insert([mongoObject]); return coll.insertOne(mongoObject);
}); });
}; };
// Runs a mongo query on the database. // Runs a mongo query on the database.

View File

@@ -71,7 +71,7 @@ export class HooksController {
_removeHooks(query) { _removeHooks(query) {
return this.getCollection().then(collection => { return this.getCollection().then(collection => {
return collection.remove(query); return collection.deleteMany(query);
}).then(() => { }).then(() => {
return {}; return {};
}); });