Skip to content

Commit e39286d

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

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
@@ -46,6 +46,17 @@ export default class MongoCollection {
4646
count(query, { skip, limit, sort } = {}) {
4747
return this._mongoCollection.count(query, { skip, limit, sort });
4848
}
49+
50+
// Atomically find and delete an object based on query.
51+
// The result is the promise with an object that was in the database before deleting.
52+
// Postgres Note: Translates directly to `DELETE * FROM ... RETURNING *`, which will return data after delete is done.
53+
findOneAndDelete(query) {
54+
// arguments: query, sort
55+
return this._mongoCollection.findAndRemove(query, []).then(document => {
56+
// Value is the object where mongo returns multiple fields.
57+
return document.value;
58+
});
59+
}
4960

5061
drop() {
5162
return this._mongoCollection.drop();

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)