mirror of
https://github.com/seejohnrun/haste-server
synced 2025-08-21 11:33:09 -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),
|
var now = Math.floor(new Date().getTime() / 1000),
|
||||||
that = this;
|
that = this;
|
||||||
|
|
||||||
this.safeConnect(function (err, db) {
|
this.safeConnect(function (err, db, close) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(false);
|
return callback(false);
|
||||||
|
|
||||||
db.collection('entries').update({
|
db.collection('entries').updateOne({
|
||||||
'entry_id': key,
|
'entry_id': key,
|
||||||
$or: [
|
$or: [
|
||||||
{ expiration: -1 },
|
{ expiration: -1 },
|
||||||
{ expiration: { $gt: now } }
|
{ expiration: { $gt: now } }
|
||||||
]
|
]
|
||||||
}, {
|
}, {$set :{
|
||||||
'entry_id': key,
|
'entry_id': key,
|
||||||
'value': data,
|
'value': data,
|
||||||
'expiration': that.expire && !skipExpire ? that.expire + now : -1
|
'expiration': that.expire && !skipExpire ? that.expire + now : -1
|
||||||
}, {
|
}}, {
|
||||||
upsert: true
|
upsert: true
|
||||||
}, function (err, existing) {
|
}).then(function (existing, err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
winston.error('error persisting value to mongodb', { error: err });
|
winston.error('error persisting value to mongodb', { error: err });
|
||||||
|
close();
|
||||||
return callback(false);
|
return callback(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(true);
|
callback(true);
|
||||||
|
close();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -43,7 +45,7 @@ MongoDocumentStore.prototype.get = function (key, callback, skipExpire) {
|
||||||
var now = Math.floor(new Date().getTime() / 1000),
|
var now = Math.floor(new Date().getTime() / 1000),
|
||||||
that = this;
|
that = this;
|
||||||
|
|
||||||
this.safeConnect(function (err, db) {
|
this.safeConnect(function (err, db, close) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(false);
|
return callback(false);
|
||||||
|
|
||||||
|
@ -53,7 +55,7 @@ MongoDocumentStore.prototype.get = function (key, callback, skipExpire) {
|
||||||
{ expiration: -1 },
|
{ expiration: -1 },
|
||||||
{ expiration: { $gt: now } }
|
{ expiration: { $gt: now } }
|
||||||
]
|
]
|
||||||
}, function (err, entry) {
|
}).then(function (entry, err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
winston.error('error persisting value to mongodb', { error: err });
|
winston.error('error persisting value to mongodb', { error: err });
|
||||||
return callback(false);
|
return callback(false);
|
||||||
|
@ -70,18 +72,18 @@ MongoDocumentStore.prototype.get = function (key, callback, skipExpire) {
|
||||||
}
|
}
|
||||||
}, function (err, result) { });
|
}, function (err, result) { });
|
||||||
}
|
}
|
||||||
|
close();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
MongoDocumentStore.prototype.safeConnect = function (callback) {
|
MongoDocumentStore.prototype.safeConnect = function (callback) {
|
||||||
MongoClient.connect(this.connectionUrl, function (err, db) {
|
const client = new MongoClient(this.connectionUrl);
|
||||||
if (err) {
|
let db_name = this.connectionUrl.split("/");
|
||||||
winston.error('error connecting to mongodb', { error: err });
|
db_name = db_name[db_name.length - 1];
|
||||||
callback(err);
|
const db = client.db(db_name);
|
||||||
} else {
|
callback(undefined, db, function() {
|
||||||
callback(undefined, db);
|
client.close();
|
||||||
}
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue