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