Skip to content

Commit b41010e

Browse files
committed
Implement findAndDelete in MongoCollection and move SchemasRouter to it.
1 parent 4bd163b commit b41010e

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/Adapters/Storage/Mongo/MongoCollection.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ export default class MongoCollection {
4343
.toArray();
4444
}
4545

46+
// Atomically find and delete an object based on query.
47+
// The result is the promise with an object that was in the database before deleting.
48+
// Postgres Note: Translates directly to `DELETE * FROM ... RETURNING *`, which will return data after delete is done.
49+
findOneAndDelete(query) {
50+
// arguments: query, sort
51+
return this._mongoCollection.findAndRemove(query, []).then(document => {
52+
// Value is the object where mongo returns multiple fields.
53+
return document.value;
54+
});
55+
}
56+
4657
count(query, { skip, limit, sort } = {}) {
4758
return this._mongoCollection.count(query, { skip, limit, sort });
4859
}

src/Routers/SchemasRouter.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,14 @@ function deleteSchema(req) {
164164
.then(() => {
165165
// We've dropped the collection now, so delete the item from _SCHEMA
166166
// and clear the _Join collections
167-
return req.config.database.collection('_SCHEMA')
168-
.then(coll => coll.findAndRemove({_id: req.params.className}, []))
169-
.then(doc => {
170-
if (doc.value === null) {
167+
return req.config.database.adaptiveCollection('_SCHEMA')
168+
.then(coll => coll.findOneAndDelete({_id: req.params.className}))
169+
.then(document => {
170+
if (document === null) {
171171
//tried to delete non-existent class
172172
return Promise.resolve();
173173
}
174-
return removeJoinTables(req.config.database, doc.value);
174+
return removeJoinTables(req.config.database, document);
175175
});
176176
})
177177
.then(() => {

0 commit comments

Comments
 (0)