From e39286d88b3a901a284edde1bca91ba29fe6a089 Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Tue, 1 Mar 2016 23:39:19 -0800 Subject: [PATCH] Implement findAndDelete in MongoCollection and move SchemasRouter to it. --- src/Adapters/Storage/Mongo/MongoCollection.js | 11 +++++++++++ src/Routers/SchemasRouter.js | 10 +++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/Adapters/Storage/Mongo/MongoCollection.js b/src/Adapters/Storage/Mongo/MongoCollection.js index cb721db0..cabd6038 100644 --- a/src/Adapters/Storage/Mongo/MongoCollection.js +++ b/src/Adapters/Storage/Mongo/MongoCollection.js @@ -46,6 +46,17 @@ export default class MongoCollection { count(query, { skip, limit, sort } = {}) { return this._mongoCollection.count(query, { skip, limit, sort }); } + + // Atomically find and delete an object based on query. + // The result is the promise with an object that was in the database before deleting. + // Postgres Note: Translates directly to `DELETE * FROM ... RETURNING *`, which will return data after delete is done. + findOneAndDelete(query) { + // arguments: query, sort + return this._mongoCollection.findAndRemove(query, []).then(document => { + // Value is the object where mongo returns multiple fields. + return document.value; + }); + } drop() { return this._mongoCollection.drop(); diff --git a/src/Routers/SchemasRouter.js b/src/Routers/SchemasRouter.js index 007625f3..70b3157e 100644 --- a/src/Routers/SchemasRouter.js +++ b/src/Routers/SchemasRouter.js @@ -164,14 +164,14 @@ function deleteSchema(req) { .then(() => { // We've dropped the collection now, so delete the item from _SCHEMA // and clear the _Join collections - return req.config.database.collection('_SCHEMA') - .then(coll => coll.findAndRemove({_id: req.params.className}, [])) - .then(doc => { - if (doc.value === null) { + return req.config.database.adaptiveCollection('_SCHEMA') + .then(coll => coll.findOneAndDelete({_id: req.params.className})) + .then(document => { + if (document === null) { //tried to delete non-existent class return Promise.resolve(); } - return removeJoinTables(req.config.database, doc.value); + return removeJoinTables(req.config.database, document); }); }) .then(() => {