From 85e51ca1955196b0bcd270f235c0cd1c5d78f242 Mon Sep 17 00:00:00 2001 From: PaauullI <76554205+PaauullI@users.noreply.github.com> Date: Mon, 28 Aug 2023 17:00:02 +0200 Subject: [PATCH] fix mongo document_handler --- lib/document_stores/mongo.js | 175 ++++++++++++++++------------------- 1 file changed, 81 insertions(+), 94 deletions(-) diff --git a/lib/document_stores/mongo.js b/lib/document_stores/mongo.js index d6f54e6..2709505 100644 --- a/lib/document_stores/mongo.js +++ b/lib/document_stores/mongo.js @@ -1,104 +1,91 @@ -var MongoClient = require("mongodb").MongoClient, - winston = require("winston"); +const winston = require('winston'); +const mongodb = require('mongodb'); -var MongoDocumentStore = function (options) { - this.expire = options.expire; - this.connectionUrl = process.env.DATABASE_URl || options.connectionUrl; -}; +class MongoDocumentStore { + constructor(config){ + 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) { - var now = Math.floor(new Date().getTime() / 1000), - that = this; + async set(key, data, callback, skipExpire){ + winston.silly(`mongo set ${key}`); + const now = Math.floor(Date.now() / 1000); - this.safeConnect(function (err, db, close) { - if (err) return callback(false); + if (await (this.safeConnect()).error) return callback(false); - db.collection("entries") - .updateOne( - { - entry_id: key, - $or: [{ expiration: -1 }, { expiration: { $gt: now } }], - }, - { - $set: { - entry_id: key, - value: data, - expiration: - that.expire && !skipExpire ? that.expire + now : -1, - }, - }, - { - upsert: true, - } - ) - .then(function (existing, err) { - if (err) { - winston.error("error persisting value to mongodb", { - error: err, - }); - close(); - return callback(false); - } - - callback(true); - close(); + return await this.MongoClient.db().collection('entries').updateOne( + { + 'entry_id': key, + $or: [ + { expiration: -1 }, + { expiration: { $gt: now } } + ] + }, + { + $set: { + 'entry_id': key, + value: data, + expiration: this.expire && !skipExpire ? this.expire + now : -1 + } + }, + { + upsert: true + } + ) + .then((err, result) => { + return callback(true); + }) + .catch((err, result) => { + winston.error('error updating mongodb document', { error: err }); + return callback(false) }); - }); -}; + } + async get(key, callback, skipExpire){ + winston.silly(`mongo get ${key}`); + const now = Math.floor(Date.now() / 1000); -MongoDocumentStore.prototype.get = function (key, callback, skipExpire) { - var now = Math.floor(new Date().getTime() / 1000), - that = this; + if ((await this.safeConnect()).error) return callback(false); - this.safeConnect(function (err, db, close) { - if (err) return callback(false); + let document = await this.MongoClient.db().collection('entries').findOne({ + '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") - .findOne({ - entry_id: key, - $or: [{ expiration: -1 }, { expiration: { $gt: now } }], + if (document && document.expiration != -1 && this.expire && !skipExpire) { + await this.MongoClient.db().collection('entries').updateOne( + { 'entry_id': key }, + { $set: { expiration: this.expire + now } } + ) + .then(() => { + winston.silly('extended expiry of mongodb document', { key: key, timestamp: this.expire + now }); }) - .then(function (entry, err) { - if (err) { - winston.error("error persisting value to mongodb", { - error: err, - }); - return callback(false); - } + .catch(err => { + winston.warn('error extending expiry of mongodb document', { error: err }); + }); + } + return callback(document ? document.value : false); + } + async safeConnect() { + // check if we are already connected + if(!!this.MongoClient && !!this.MongoClient.topology && this.MongoClient?.topology?.isConnected()) return { error: null }; + + return await this.MongoClient.connect() + .then(() => { + winston.info('connected to mongodb'); + return { error: null }; + }) + .catch(err => { + winston.error('error connecting to mongodb', { error: err }); + return { error: err }; + }); + } +} - callback(entry === null ? false : entry.value); - - if ( - entry !== null && - entry.expiration !== -1 && - that.expire && - !skipExpire - ) { - 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; \ No newline at end of file