fix mongo document_handler

This commit is contained in:
PaauullI 2023-08-28 17:00:02 +02:00
commit 85e51ca195

View file

@ -1,104 +1,91 @@
var MongoClient = require("mongodb").MongoClient, const winston = require('winston');
winston = require("winston"); const mongodb = require('mongodb');
var MongoDocumentStore = function (options) { class MongoDocumentStore {
this.expire = options.expire; constructor(config){
this.connectionUrl = process.env.DATABASE_URl || options.connectionUrl; this.expire = config.expire;
}; this.connectionUrl = process.env.DATABASE_URl || config.connectionUrl;
this.MongoClient = new mongodb.MongoClient(this.connectionUrl);
}
MongoDocumentStore.prototype.set = function (key, data, callback, skipExpire) { async set(key, data, callback, skipExpire){
var now = Math.floor(new Date().getTime() / 1000), winston.silly(`mongo set ${key}`);
that = this; const now = Math.floor(Date.now() / 1000);
this.safeConnect(function (err, db, close) { if (await (this.safeConnect()).error) return callback(false);
if (err) return callback(false);
db.collection("entries") return await this.MongoClient.db().collection('entries').updateOne(
.updateOne( {
{ 'entry_id': key,
entry_id: key, $or: [
$or: [{ expiration: -1 }, { expiration: { $gt: now } }], { expiration: -1 },
}, { expiration: { $gt: now } }
{ ]
$set: { },
entry_id: key, {
value: data, $set: {
expiration: 'entry_id': key,
that.expire && !skipExpire ? that.expire + now : -1, value: data,
}, expiration: this.expire && !skipExpire ? this.expire + now : -1
}, }
{ },
upsert: true, {
} upsert: true
) }
.then(function (existing, err) { )
if (err) { .then((err, result) => {
winston.error("error persisting value to mongodb", { return callback(true);
error: err, })
}); .catch((err, result) => {
close(); winston.error('error updating mongodb document', { error: err });
return callback(false); return callback(false)
}
callback(true);
close();
}); });
}); }
}; async get(key, callback, skipExpire){
winston.silly(`mongo get ${key}`);
const now = Math.floor(Date.now() / 1000);
MongoDocumentStore.prototype.get = function (key, callback, skipExpire) { if ((await this.safeConnect()).error) return callback(false);
var now = Math.floor(new Date().getTime() / 1000),
that = this;
this.safeConnect(function (err, db, close) { let document = await this.MongoClient.db().collection('entries').findOne({
if (err) return callback(false); 'entry_id': key,
$or: [
{ expiration: -1 },
{ expiration: { $gt: now } }
]
}).catch(err => {
winston.error('error finding mongodb document', { error: err });
return callback(false);
});
db.collection("entries") if (document && document.expiration != -1 && this.expire && !skipExpire) {
.findOne({ await this.MongoClient.db().collection('entries').updateOne(
entry_id: key, { 'entry_id': key },
$or: [{ expiration: -1 }, { expiration: { $gt: now } }], { $set: { expiration: this.expire + now } }
)
.then(() => {
winston.silly('extended expiry of mongodb document', { key: key, timestamp: this.expire + now });
}) })
.then(function (entry, err) { .catch(err => {
if (err) { winston.warn('error extending expiry of mongodb document', { error: err });
winston.error("error persisting value to mongodb", { });
error: err, }
}); return callback(document ? document.value : false);
return callback(false); }
} async safeConnect() {
// check if we are already connected
if(!!this.MongoClient && !!this.MongoClient.topology && this.MongoClient?.topology?.isConnected()) return { error: null };
callback(entry === null ? false : entry.value); return await this.MongoClient.connect()
.then(() => {
if ( winston.info('connected to mongodb');
entry !== null && return { error: null };
entry.expiration !== -1 && })
that.expire && .catch(err => {
!skipExpire winston.error('error connecting to mongodb', { error: err });
) { return { error: err };
db.collection("entries").update( });
{ }
entry_id: key, }
},
{
$set: {
expiration: that.expire + now,
},
},
function (err, result) {}
);
}
close();
});
});
};
MongoDocumentStore.prototype.safeConnect = function (callback) {
const client = new MongoClient(this.connectionUrl);
let db_name = this.connectionUrl.split("/");
db_name = db_name[db_name.length - 1];
const db = client.db(db_name);
callback(undefined, db, function () {
client.close();
});
};
module.exports = MongoDocumentStore; module.exports = MongoDocumentStore;