2020-11-26 23:23:47 +00:00
|
|
|
const MongoClient = require('mongodb').MongoClient,
|
2016-06-10 20:43:43 +00:00
|
|
|
winston = require('winston');
|
|
|
|
|
2020-11-26 23:23:47 +00:00
|
|
|
const MongoDocumentStore = function (options) {
|
2016-06-10 20:43:43 +00:00
|
|
|
this.expire = options.expire;
|
2020-11-26 23:23:47 +00:00
|
|
|
this.connectionUrl = process.env.DATABASE_URL || options.connectionUrl;
|
|
|
|
this.connectionName = process.env.DATABASE_NAME || options.connectionName;
|
2016-06-10 20:43:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
MongoDocumentStore.prototype.set = function (key, data, callback, skipExpire) {
|
2020-11-26 23:23:47 +00:00
|
|
|
const now = Math.floor(new Date().getTime() / 1000),
|
2016-06-10 20:43:43 +00:00
|
|
|
that = this;
|
|
|
|
|
|
|
|
this.safeConnect(function (err, db) {
|
|
|
|
if (err)
|
|
|
|
return callback(false);
|
2020-11-26 23:23:47 +00:00
|
|
|
|
2020-12-03 18:05:09 +00:00
|
|
|
db.collection('entries').updateOne({
|
2016-06-10 20:43:43 +00:00
|
|
|
'entry_id': key,
|
|
|
|
$or: [
|
|
|
|
{ expiration: -1 },
|
|
|
|
{ expiration: { $gt: now } }
|
|
|
|
]
|
|
|
|
}, {
|
2020-12-03 18:05:09 +00:00
|
|
|
$set: {
|
|
|
|
'entry_id': key,
|
|
|
|
'value': data,
|
|
|
|
'expiration': that.expire && !skipExpire ? that.expire + now : -1
|
|
|
|
}
|
2016-06-10 20:43:43 +00:00
|
|
|
}, {
|
|
|
|
upsert: true
|
|
|
|
}, function (err, existing) {
|
|
|
|
if (err) {
|
|
|
|
winston.error('error persisting value to mongodb', { error: err });
|
|
|
|
return callback(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
MongoDocumentStore.prototype.get = function (key, callback, skipExpire) {
|
2020-11-26 23:23:47 +00:00
|
|
|
const now = Math.floor(new Date().getTime() / 1000),
|
2016-06-10 20:43:43 +00:00
|
|
|
that = this;
|
|
|
|
|
|
|
|
this.safeConnect(function (err, db) {
|
|
|
|
if (err)
|
|
|
|
return callback(false);
|
2020-11-26 23:23:47 +00:00
|
|
|
|
2016-06-10 20:43:43 +00:00
|
|
|
db.collection('entries').findOne({
|
|
|
|
'entry_id': key,
|
|
|
|
$or: [
|
|
|
|
{ expiration: -1 },
|
|
|
|
{ expiration: { $gt: now } }
|
|
|
|
]
|
|
|
|
}, function (err, entry) {
|
|
|
|
if (err) {
|
|
|
|
winston.error('error persisting value to mongodb', { error: err });
|
|
|
|
return callback(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(entry === null ? false : entry.value);
|
|
|
|
|
|
|
|
if (entry !== null && entry.expiration !== -1 && that.expire && !skipExpire) {
|
2020-12-03 18:05:09 +00:00
|
|
|
db.collection('entries').updateOne({
|
2016-06-10 20:43:43 +00:00
|
|
|
'entry_id': key
|
|
|
|
}, {
|
|
|
|
$set: {
|
|
|
|
'expiration': that.expire + now
|
|
|
|
}
|
|
|
|
}, function (err, result) { });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
MongoDocumentStore.prototype.safeConnect = function (callback) {
|
2020-12-03 18:05:09 +00:00
|
|
|
MongoClient.connect(this.connectionUrl, { useUnifiedTopology: true }, function (err, client) {
|
2016-06-10 20:43:43 +00:00
|
|
|
if (err) {
|
|
|
|
winston.error('error connecting to mongodb', { error: err });
|
|
|
|
callback(err);
|
|
|
|
} else {
|
2020-11-26 23:23:47 +00:00
|
|
|
callback(undefined, client.db(this.connectionDBName));
|
2016-06-10 20:43:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = MongoDocumentStore;
|