Merge c3c1f85ce1
into 5d2965ffc5
commit
c819371d44
|
@ -165,11 +165,12 @@ Once you've done that, your config section should look like:
|
|||
``` json
|
||||
{
|
||||
"type": "mongodb",
|
||||
"connectionUrl": "mongodb://localhost:27017/database"
|
||||
"connectionUrl": "mongodb://localhost:27017",
|
||||
"connectionName": "database"
|
||||
}
|
||||
```
|
||||
|
||||
You can also just set the environment variable for `DATABASE_URL` to your database connection url.
|
||||
You can also just set the environment variable for `DATABASE_URL` to your server connection url and `DATABASE_NAME` for your database name.
|
||||
|
||||
Unlike with postgres you do NOT have to create the table in your mongo database prior to running.
|
||||
|
||||
|
|
|
@ -1,31 +1,32 @@
|
|||
|
||||
|
||||
var MongoClient = require('mongodb').MongoClient,
|
||||
const MongoClient = require('mongodb').MongoClient,
|
||||
winston = require('winston');
|
||||
|
||||
var MongoDocumentStore = function (options) {
|
||||
const MongoDocumentStore = function (options) {
|
||||
this.expire = options.expire;
|
||||
this.connectionUrl = process.env.DATABASE_URl || options.connectionUrl;
|
||||
this.connectionUrl = process.env.DATABASE_URL || options.connectionUrl;
|
||||
this.connectionName = process.env.DATABASE_NAME || options.connectionName;
|
||||
};
|
||||
|
||||
MongoDocumentStore.prototype.set = function (key, data, callback, skipExpire) {
|
||||
var now = Math.floor(new Date().getTime() / 1000),
|
||||
const now = Math.floor(new Date().getTime() / 1000),
|
||||
that = this;
|
||||
|
||||
this.safeConnect(function (err, db) {
|
||||
if (err)
|
||||
return callback(false);
|
||||
|
||||
db.collection('entries').update({
|
||||
|
||||
db.collection('entries').updateOne({
|
||||
'entry_id': key,
|
||||
$or: [
|
||||
{ expiration: -1 },
|
||||
{ expiration: { $gt: now } }
|
||||
]
|
||||
}, {
|
||||
'entry_id': key,
|
||||
'value': data,
|
||||
'expiration': that.expire && !skipExpire ? that.expire + now : -1
|
||||
$set: {
|
||||
'entry_id': key,
|
||||
'value': data,
|
||||
'expiration': that.expire && !skipExpire ? that.expire + now : -1
|
||||
}
|
||||
}, {
|
||||
upsert: true
|
||||
}, function (err, existing) {
|
||||
|
@ -40,13 +41,13 @@ MongoDocumentStore.prototype.set = function (key, data, callback, skipExpire) {
|
|||
};
|
||||
|
||||
MongoDocumentStore.prototype.get = function (key, callback, skipExpire) {
|
||||
var now = Math.floor(new Date().getTime() / 1000),
|
||||
const now = Math.floor(new Date().getTime() / 1000),
|
||||
that = this;
|
||||
|
||||
this.safeConnect(function (err, db) {
|
||||
if (err)
|
||||
return callback(false);
|
||||
|
||||
|
||||
db.collection('entries').findOne({
|
||||
'entry_id': key,
|
||||
$or: [
|
||||
|
@ -62,7 +63,7 @@ MongoDocumentStore.prototype.get = function (key, callback, skipExpire) {
|
|||
callback(entry === null ? false : entry.value);
|
||||
|
||||
if (entry !== null && entry.expiration !== -1 && that.expire && !skipExpire) {
|
||||
db.collection('entries').update({
|
||||
db.collection('entries').updateOne({
|
||||
'entry_id': key
|
||||
}, {
|
||||
$set: {
|
||||
|
@ -75,12 +76,12 @@ MongoDocumentStore.prototype.get = function (key, callback, skipExpire) {
|
|||
};
|
||||
|
||||
MongoDocumentStore.prototype.safeConnect = function (callback) {
|
||||
MongoClient.connect(this.connectionUrl, function (err, db) {
|
||||
MongoClient.connect(this.connectionUrl, { useUnifiedTopology: true }, function (err, client) {
|
||||
if (err) {
|
||||
winston.error('error connecting to mongodb', { error: err });
|
||||
callback(err);
|
||||
} else {
|
||||
callback(undefined, db);
|
||||
callback(undefined, client.db(this.connectionName));
|
||||
}
|
||||
});
|
||||
};
|
Loading…
Reference in New Issue