var MongoClient = require("mongodb").MongoClient var Winston = require("winston") class MongoDocumentStore { constructor(options) { this.connectionURL = `mongodb+srv://${options.username}:${options.password}@cluster0-vycgu.mongodb.net/${options.dbName}?retryWrites=true&w=majority`; this.dbName = options.dbName; this.collectionName = options.collectionName; } set(key, data, callback) { var _this = this; const client = new MongoClient(this.connectionURL, { useUnifiedTopology: true, useNewUrlParser: true }); client.connect(err => { if (err) { callback(false); Winston.error("Error connecting to MongoDB database.", { error: err }); return; } const db = client.db(_this.dbName); const collection = db.collection(_this.collectionName); collection.insertOne({key: key, data: data}, (err, result) => { if (err) { callback(false); Winston.error("Error inserting data into MongoDB database.", { error: error }); return; } Winston.info("Inserted data into MongoDB database."); callback(true); }); }); } get(key, callback) { var _this = this; MongoClient.connect(this.connectionURL, { useUnifiedTopology: true }, function(err, client) { if (err) { callback(false); Winston.error("Error connecting to MongoDB database.", { error: err }); return; } const db = client.db(_this.dbName); const collection = db.collection(_this.collectionName); collection.findOne({key: key}, (err, result) => { if (err || !result) { callback(false); if (err) { Winston.error("Error getting data from MongoDB database.", { error: error }); } return; } Winston.info("Retrieved data from MongoDB database."); callback(result.data); }); }); } } module.exports = MongoDocumentStore;