mirror of
https://github.com/seejohnrun/haste-server
synced 2025-08-21 01:23:09 -07:00
fix mongo document_handler
This commit is contained in:
parent
7875147706
commit
85e51ca195
1 changed files with 81 additions and 94 deletions
|
@ -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;
|
Loading…
Add table
Add a link
Reference in a new issue