From ddabb1187f8e350ec05684c0c2beb2830e7d70a3 Mon Sep 17 00:00:00 2001 From: Andrew Molchanov Date: Fri, 27 Nov 2020 02:23:47 +0300 Subject: [PATCH 1/5] Changes to support MongoDB Node.JS Driver v3 --- lib/document_stores/{mongo.js => mongodb.js} | 21 ++++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) rename lib/document_stores/{mongo.js => mongodb.js} (81%) diff --git a/lib/document_stores/mongo.js b/lib/document_stores/mongodb.js similarity index 81% rename from lib/document_stores/mongo.js rename to lib/document_stores/mongodb.js index 502a508..e700212 100644 --- a/lib/document_stores/mongo.js +++ b/lib/document_stores/mongodb.js @@ -1,21 +1,20 @@ - - -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({ 'entry_id': key, $or: [ @@ -40,13 +39,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: [ @@ -75,12 +74,12 @@ MongoDocumentStore.prototype.get = function (key, callback, skipExpire) { }; MongoDocumentStore.prototype.safeConnect = function (callback) { - MongoClient.connect(this.connectionUrl, function (err, db) { + MongoClient.connect(this.connectionUrl, function (err, client) { if (err) { winston.error('error connecting to mongodb', { error: err }); callback(err); } else { - callback(undefined, db); + callback(undefined, client.db(this.connectionDBName)); } }); }; From 06577a4e01b538f17a0494b422a90bcb01f64f5e Mon Sep 17 00:00:00 2001 From: Andrew Molchanov Date: Fri, 27 Nov 2020 02:31:29 +0300 Subject: [PATCH 2/5] Update readme for MongoDB --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b42c844..8737ee1 100644 --- a/README.md +++ b/README.md @@ -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. From cd10b3dc3edc06edc10a92705f36cd0affff38f8 Mon Sep 17 00:00:00 2001 From: Andrew Molchanov Date: Thu, 3 Dec 2020 21:05:09 +0300 Subject: [PATCH 3/5] fixed deprecation warnings --- lib/document_stores/mongodb.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/document_stores/mongodb.js b/lib/document_stores/mongodb.js index e700212..250dbe3 100644 --- a/lib/document_stores/mongodb.js +++ b/lib/document_stores/mongodb.js @@ -15,16 +15,18 @@ MongoDocumentStore.prototype.set = function (key, data, callback, skipExpire) { 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) { @@ -61,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: { @@ -74,7 +76,7 @@ MongoDocumentStore.prototype.get = function (key, callback, skipExpire) { }; MongoDocumentStore.prototype.safeConnect = function (callback) { - MongoClient.connect(this.connectionUrl, function (err, client) { + MongoClient.connect(this.connectionUrl, { useUnifiedTopology: true }, function (err, client) { if (err) { winston.error('error connecting to mongodb', { error: err }); callback(err); From c3c1f85ce15663d27fdc09382166a09cbdffd3b9 Mon Sep 17 00:00:00 2001 From: Andrew Molchanov Date: Wed, 21 Jul 2021 01:53:17 +0300 Subject: [PATCH 4/5] fix safeConnect --- lib/document_stores/mongodb.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/document_stores/mongodb.js b/lib/document_stores/mongodb.js index 250dbe3..456fc4c 100644 --- a/lib/document_stores/mongodb.js +++ b/lib/document_stores/mongodb.js @@ -81,7 +81,7 @@ MongoDocumentStore.prototype.safeConnect = function (callback) { winston.error('error connecting to mongodb', { error: err }); callback(err); } else { - callback(undefined, client.db(this.connectionDBName)); + callback(undefined, client.db(this.connectionName)); } }); }; From dc6fccc9fe98812a1605ba18dfdc28f196a91258 Mon Sep 17 00:00:00 2001 From: Andrew Molchanov Date: Wed, 22 Dec 2021 01:13:50 +0300 Subject: [PATCH 5/5] fixed bug with connection limit --- lib/document_stores/mongodb.js | 147 +++++++++++++++++++-------------- 1 file changed, 84 insertions(+), 63 deletions(-) diff --git a/lib/document_stores/mongodb.js b/lib/document_stores/mongodb.js index 456fc4c..0f4ac03 100644 --- a/lib/document_stores/mongodb.js +++ b/lib/document_stores/mongodb.js @@ -1,5 +1,5 @@ -const MongoClient = require('mongodb').MongoClient, - winston = require('winston'); +const { MongoClient } = require("mongodb"), + winston = require("winston"); const MongoDocumentStore = function (options) { this.expire = options.expire; @@ -8,82 +8,103 @@ const MongoDocumentStore = function (options) { }; MongoDocumentStore.prototype.set = function (key, data, callback, skipExpire) { - const now = Math.floor(new Date().getTime() / 1000), - that = this; + const now = Math.floor(new Date().getTime() / 1000); - this.safeConnect(function (err, db) { - if (err) - return callback(false); + this.safeConnect((err, db, client) => { + if (err) 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 + 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, + }, + (err /*, existing*/) => { + client.close(); + + if (err) { + winston.error("error persisting value to mongodb", { + error: err, + }); + return callback(false); + } + + callback(true); } - }, { - upsert: true - }, function (err, existing) { - if (err) { - winston.error('error persisting value to mongodb', { error: err }); - return callback(false); - } - - callback(true); - }); + ); }); }; MongoDocumentStore.prototype.get = function (key, callback, skipExpire) { - const now = Math.floor(new Date().getTime() / 1000), - that = this; + const now = Math.floor(new Date().getTime() / 1000); - this.safeConnect(function (err, db) { - if (err) - return callback(false); + this.safeConnect((err, db, client) => { + if (err) return callback(false); - db.collection('entries').findOne({ - 'entry_id': key, - $or: [ - { expiration: -1 }, - { expiration: { $gt: now } } - ] - }, function (err, entry) { - if (err) { - winston.error('error persisting value to mongodb', { error: err }); - return callback(false); + db.collection("entries").findOne( + { + entry_id: key, + $or: [{ expiration: -1 }, { expiration: { $gt: now } }], + }, + (err, entry) => { + if (err) { + winston.error("error persisting value to mongodb", { + error: err, + }); + client.close(); + return callback(false); + } + + callback(entry === null ? false : entry.value); + + if ( + entry !== null && + entry.expiration !== -1 && + this.expire && + !skipExpire + ) { + db.collection("entries").updateOne( + { + entry_id: key, + }, + { + $set: { + expiration: this.expire + now, + }, + }, + () => { + client.close(); + } + ); + } } - - callback(entry === null ? false : entry.value); - - if (entry !== null && entry.expiration !== -1 && that.expire && !skipExpire) { - db.collection('entries').updateOne({ - 'entry_id': key - }, { - $set: { - 'expiration': that.expire + now - } - }, function (err, result) { }); - } - }); + ); }); }; MongoDocumentStore.prototype.safeConnect = function (callback) { - MongoClient.connect(this.connectionUrl, { useUnifiedTopology: true }, function (err, client) { - if (err) { - winston.error('error connecting to mongodb', { error: err }); - callback(err); - } else { - callback(undefined, client.db(this.connectionName)); + MongoClient.connect( + this.connectionUrl, + { useUnifiedTopology: true }, + (err, client) => { + if (err) { + winston.error("error connecting to mongodb", { error: err }); + callback(err); + } else { + callback(undefined, client.db(this.connectionName), client); + } } - }); + ); }; module.exports = MongoDocumentStore;