2016-12-07 15:17:05 -08:00
|
|
|
const mongodb = require('mongodb');
|
|
|
|
|
const Collection = mongodb.Collection;
|
2016-03-01 20:04:15 -08:00
|
|
|
|
|
|
|
|
export default class MongoCollection {
|
2018-09-01 13:58:06 -04:00
|
|
|
_mongoCollection: Collection;
|
2016-03-01 20:04:15 -08:00
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
constructor(mongoCollection: Collection) {
|
2016-03-01 20:04:15 -08:00
|
|
|
this._mongoCollection = mongoCollection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Does a find with "smart indexing".
|
|
|
|
|
// Currently this just means, if it needs a geoindex and there is
|
|
|
|
|
// none, then build the geoindex.
|
|
|
|
|
// This could be improved a lot but it's not clear if that's a good
|
|
|
|
|
// idea. Or even if this behavior is a good idea.
|
2017-06-21 17:18:10 -03:00
|
|
|
find(query, { skip, limit, sort, keys, maxTimeMS, readPreference } = {}) {
|
2017-06-13 20:42:59 -05:00
|
|
|
// Support for Full Text Search - $text
|
2018-09-01 13:58:06 -04:00
|
|
|
if (keys && keys.$score) {
|
2017-06-13 20:42:59 -05:00
|
|
|
delete keys.$score;
|
2018-09-01 13:58:06 -04:00
|
|
|
keys.score = { $meta: 'textScore' };
|
2017-06-13 20:42:59 -05:00
|
|
|
}
|
2018-09-01 13:58:06 -04:00
|
|
|
return this._rawFind(query, {
|
|
|
|
|
skip,
|
|
|
|
|
limit,
|
|
|
|
|
sort,
|
|
|
|
|
keys,
|
|
|
|
|
maxTimeMS,
|
|
|
|
|
readPreference,
|
|
|
|
|
}).catch(error => {
|
|
|
|
|
// Check for "no geoindex" error
|
|
|
|
|
if (
|
|
|
|
|
error.code != 17007 &&
|
|
|
|
|
!error.message.match(/unable to find index for .geoNear/)
|
|
|
|
|
) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
// Figure out what key needs an index
|
|
|
|
|
const key = error.message.match(/field=([A-Za-z_0-9]+) /)[1];
|
|
|
|
|
if (!key) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var index = {};
|
|
|
|
|
index[key] = '2d';
|
|
|
|
|
return (
|
|
|
|
|
this._mongoCollection
|
|
|
|
|
.createIndex(index)
|
2016-03-01 20:04:15 -08:00
|
|
|
// Retry, but just once.
|
2018-09-01 13:58:06 -04:00
|
|
|
.then(() =>
|
|
|
|
|
this._rawFind(query, {
|
|
|
|
|
skip,
|
|
|
|
|
limit,
|
|
|
|
|
sort,
|
|
|
|
|
keys,
|
|
|
|
|
maxTimeMS,
|
|
|
|
|
readPreference,
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
});
|
2016-03-01 20:04:15 -08:00
|
|
|
}
|
|
|
|
|
|
2017-06-21 17:18:10 -03:00
|
|
|
_rawFind(query, { skip, limit, sort, keys, maxTimeMS, readPreference } = {}) {
|
2018-09-01 13:58:06 -04:00
|
|
|
let findOperation = this._mongoCollection.find(query, {
|
|
|
|
|
skip,
|
|
|
|
|
limit,
|
|
|
|
|
sort,
|
|
|
|
|
readPreference,
|
|
|
|
|
});
|
2016-09-24 13:43:49 -04:00
|
|
|
|
|
|
|
|
if (keys) {
|
|
|
|
|
findOperation = findOperation.project(keys);
|
|
|
|
|
}
|
2016-11-11 08:03:35 -08:00
|
|
|
|
|
|
|
|
if (maxTimeMS) {
|
|
|
|
|
findOperation = findOperation.maxTimeMS(maxTimeMS);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-24 13:43:49 -04:00
|
|
|
return findOperation.toArray();
|
2016-03-01 20:04:15 -08:00
|
|
|
}
|
|
|
|
|
|
2017-06-21 17:18:10 -03:00
|
|
|
count(query, { skip, limit, sort, maxTimeMS, readPreference } = {}) {
|
2018-09-01 13:58:06 -04:00
|
|
|
const countOperation = this._mongoCollection.count(query, {
|
|
|
|
|
skip,
|
|
|
|
|
limit,
|
|
|
|
|
sort,
|
|
|
|
|
maxTimeMS,
|
|
|
|
|
readPreference,
|
|
|
|
|
});
|
2016-11-11 08:03:35 -08:00
|
|
|
|
|
|
|
|
return countOperation;
|
2016-03-01 20:04:15 -08:00
|
|
|
}
|
2016-03-02 00:04:14 -08:00
|
|
|
|
2017-11-12 13:00:22 -06:00
|
|
|
distinct(field, query) {
|
|
|
|
|
return this._mongoCollection.distinct(field, query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
aggregate(pipeline, { maxTimeMS, readPreference } = {}) {
|
2018-09-01 13:58:06 -04:00
|
|
|
return this._mongoCollection
|
|
|
|
|
.aggregate(pipeline, { maxTimeMS, readPreference })
|
|
|
|
|
.toArray();
|
2017-11-12 13:00:22 -06:00
|
|
|
}
|
|
|
|
|
|
2016-03-07 19:26:40 -08:00
|
|
|
insertOne(object) {
|
|
|
|
|
return this._mongoCollection.insertOne(object);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-04 18:43:19 -08:00
|
|
|
// 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) {
|
2018-09-01 13:58:06 -04:00
|
|
|
return this._mongoCollection.update(query, update, { upsert: true });
|
2016-03-04 18:43:19 -08:00
|
|
|
}
|
|
|
|
|
|
2016-03-07 20:56:26 -08:00
|
|
|
updateOne(query, update) {
|
|
|
|
|
return this._mongoCollection.updateOne(query, update);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 14:16:26 -08:00
|
|
|
updateMany(query, update) {
|
|
|
|
|
return this._mongoCollection.updateMany(query, update);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 19:26:40 -08:00
|
|
|
deleteMany(query) {
|
|
|
|
|
return this._mongoCollection.deleteMany(query);
|
2016-03-04 18:43:19 -08:00
|
|
|
}
|
|
|
|
|
|
2016-06-10 20:27:21 -07:00
|
|
|
_ensureSparseUniqueIndexInBackground(indexRequest) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-09-01 13:58:06 -04:00
|
|
|
this._mongoCollection.ensureIndex(
|
|
|
|
|
indexRequest,
|
|
|
|
|
{ unique: true, background: true, sparse: true },
|
|
|
|
|
error => {
|
|
|
|
|
if (error) {
|
|
|
|
|
reject(error);
|
|
|
|
|
} else {
|
|
|
|
|
resolve();
|
|
|
|
|
}
|
2016-06-10 20:27:21 -07:00
|
|
|
}
|
2018-09-01 13:58:06 -04:00
|
|
|
);
|
2016-06-10 20:27:21 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-01 20:24:36 -08:00
|
|
|
drop() {
|
|
|
|
|
return this._mongoCollection.drop();
|
|
|
|
|
}
|
2016-03-01 20:04:15 -08:00
|
|
|
}
|