Move HooksController to use MongoCollection instead of direct Mongo access.

This commit is contained in:
Nikita Lutsenko
2016-03-04 18:43:19 -08:00
parent c6ae64bdbe
commit 172da3aaa3
2 changed files with 86 additions and 99 deletions

View File

@@ -1,4 +1,3 @@
let mongodb = require('mongodb');
let Collection = mongodb.Collection;
@@ -18,8 +17,7 @@ export default class MongoCollection {
return this._rawFind(query, { skip, limit, sort })
.catch(error => {
// Check for "no geoindex" error
if (error.code != 17007 ||
!error.message.match(/unable to find index for .geoNear/)) {
if (error.code != 17007 || !error.message.match(/unable to find index for .geoNear/)) {
throw error;
}
// Figure out what key needs an index
@@ -59,6 +57,13 @@ export default class MongoCollection {
})
}
// 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
// Postgres Note: `INSERT ... ON CONFLICT UPDATE` that is available since 9.5.
upsertOne(query, update) {
return this._mongoCollection.update(query, update, { upsert: true });
}
// 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.
@@ -70,6 +75,10 @@ export default class MongoCollection {
});
}
remove(query) {
return this._mongoCollection.remove(query);
}
drop() {
return this._mongoCollection.drop();
}