Implement findAndDelete in MongoCollection and move SchemasRouter to it.

This commit is contained in:
Nikita Lutsenko
2016-03-01 23:39:19 -08:00
parent 4bd163b790
commit e39286d88b
2 changed files with 16 additions and 5 deletions

View File

@@ -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();

View File

@@ -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(() => {