mirror of
https://github.com/seejohnrun/haste-server
synced 2025-08-21 11:53:10 -07:00
MongoDB 5.7.0
This commit is contained in:
parent
b52b394bad
commit
062d900563
1 changed files with 16 additions and 14 deletions
|
@ -12,29 +12,31 @@ MongoDocumentStore.prototype.set = function (key, data, callback, skipExpire) {
|
|||
var now = Math.floor(new Date().getTime() / 1000),
|
||||
that = this;
|
||||
|
||||
this.safeConnect(function (err, db) {
|
||||
this.safeConnect(function (err, db, close) {
|
||||
if (err)
|
||||
return callback(false);
|
||||
|
||||
db.collection('entries').update({
|
||||
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
|
||||
}, function (err, existing) {
|
||||
}).then(function (existing, err) {
|
||||
if (err) {
|
||||
winston.error('error persisting value to mongodb', { error: err });
|
||||
close();
|
||||
return callback(false);
|
||||
}
|
||||
|
||||
callback(true);
|
||||
close();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
@ -43,7 +45,7 @@ MongoDocumentStore.prototype.get = function (key, callback, skipExpire) {
|
|||
var now = Math.floor(new Date().getTime() / 1000),
|
||||
that = this;
|
||||
|
||||
this.safeConnect(function (err, db) {
|
||||
this.safeConnect(function (err, db, close) {
|
||||
if (err)
|
||||
return callback(false);
|
||||
|
||||
|
@ -53,7 +55,7 @@ MongoDocumentStore.prototype.get = function (key, callback, skipExpire) {
|
|||
{ expiration: -1 },
|
||||
{ expiration: { $gt: now } }
|
||||
]
|
||||
}, function (err, entry) {
|
||||
}).then(function (entry, err) {
|
||||
if (err) {
|
||||
winston.error('error persisting value to mongodb', { error: err });
|
||||
return callback(false);
|
||||
|
@ -70,18 +72,18 @@ MongoDocumentStore.prototype.get = function (key, callback, skipExpire) {
|
|||
}
|
||||
}, function (err, result) { });
|
||||
}
|
||||
close();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
MongoDocumentStore.prototype.safeConnect = function (callback) {
|
||||
MongoClient.connect(this.connectionUrl, function (err, db) {
|
||||
if (err) {
|
||||
winston.error('error connecting to mongodb', { error: err });
|
||||
callback(err);
|
||||
} else {
|
||||
callback(undefined, db);
|
||||
}
|
||||
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();
|
||||
});
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue