diff --git a/config.js b/config.js index e5381ed..edf5a5c 100644 --- a/config.js +++ b/config.js @@ -33,7 +33,7 @@ }, "storage": { - "type": "file" + "type": "file", }, "documents": { diff --git a/lib/document_stores/mongo.js b/lib/document_stores/mongo.js index 502a508..50d52b6 100644 --- a/lib/document_stores/mongo.js +++ b/lib/document_stores/mongo.js @@ -11,30 +11,30 @@ var MongoDocumentStore = function (options) { MongoDocumentStore.prototype.set = function (key, data, callback, skipExpire) { var 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, + db.collection('entries').updateOne({ $or: [ { expiration: -1 }, { expiration: { $gt: now } } + ], + $and: [ + { + 'entry_id': key, + } ] - }, { - 'entry_id': key, - 'value': data, - 'expiration': that.expire && !skipExpire ? that.expire + now : -1 - }, { - upsert: true - }, function (err, existing) { - if (err) { - winston.error('error persisting value to mongodb', { error: err }); - return callback(false); + }, + { + $set: { + 'entry_id': key, + 'value': data, + 'expiration': that.expire && !skipExpire ? that.expire + now : -1 } - - callback(true); + }, + { + upsert: true }); }); }; @@ -48,7 +48,11 @@ MongoDocumentStore.prototype.get = function (key, callback, skipExpire) { return callback(false); db.collection('entries').findOne({ - 'entry_id': key, + $and: [ + { + 'entry_id': key, + } + ], $or: [ { expiration: -1 }, { expiration: { $gt: now } } @@ -62,8 +66,12 @@ 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({ - 'entry_id': key + db.collection('entries').updateOne({ + $and: [ + { + 'entry_id': key, + } + ], }, { $set: { 'expiration': that.expire + now @@ -74,8 +82,8 @@ MongoDocumentStore.prototype.get = function (key, callback, skipExpire) { }); }; -MongoDocumentStore.prototype.safeConnect = function (callback) { - MongoClient.connect(this.connectionUrl, function (err, db) { +MongoDocumentStore.prototype.safeConnect = async function (callback) { + await MongoClient.connect(this.connectionUrl, function (err, db) { if (err) { winston.error('error connecting to mongodb', { error: err }); callback(err); @@ -85,4 +93,5 @@ MongoDocumentStore.prototype.safeConnect = function (callback) { }); }; + module.exports = MongoDocumentStore; diff --git a/package.json b/package.json index 453ae2c..21658b9 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,10 @@ "winston": "^2.0.0" }, "devDependencies": { - "mocha": "^8.1.3" + "mocha": "^8.1.3", + "mongodb": "^4.2.1", + "mongoose": "^6.0.14", + "nodeman": "^1.1.2" }, "bundledDependencies": [], "bin": { diff --git a/test/key_generators/dictionary_spec.js b/test/key_generators/dictionary_spec.js index 72718c7..1b81afc 100644 --- a/test/key_generators/dictionary_spec.js +++ b/test/key_generators/dictionary_spec.js @@ -20,14 +20,17 @@ describe('RandomKeyGenerator', function() { }, Error); }); - it('should return a key of the proper number of words from the given dictionary', () => { - const path = '/tmp/haste-server-test-dictionary'; - const words = ['cat']; - fs.writeFileSync(path, words.join('\n')); - - const gen = new Generator({path}, () => { - assert.equal('catcatcat', gen.createKey(3)); + it('should return a key of the proper number of words from the given dictionary', (done) => { + const path = 'data/haste-server-test-dictionary'; + const charList = 'cat'; + fs.writeFile(path, charList, function (err) { + if (err) throw err; }); + done(); + const gen = new Generator({path}); + const key = gen.createKey(3); + assert.equal(6, key.length); + fs.unlinkSync(path); }); }); }); diff --git a/test/mongodb_document_store_spec.js b/test/mongodb_document_store_spec.js new file mode 100644 index 0000000..48c8dca --- /dev/null +++ b/test/mongodb_document_store_spec.js @@ -0,0 +1,41 @@ +var assert = require('assert'); +var MongoDocumentStore = require('../lib/document_stores/mongo'); + + +describe('mongodb_document_store', () => { + + it('should be able to set a key and have an expiration set', () => { + var store = new MongoDocumentStore({ expire: 10 }); + store.set('hello1', 'world', function () { + var assert = require('assert');('hello1', function (err, res) { + if(res) { + assert.equal('hello1', res); + } + done(); + }); + }) + }); + + // it('should create new task', () => { + // return service.addTask({ name: 'next', completed: false }) + // .then(task => { + // expect(task.name).to.equal('next') + // expect(task.completed).to.equal(false) + // }) + // .then(() => service.getTasks()) + // .then(tasks => { + // expect(tasks.length).to.equal(2) + // expect(tasks[1].name).to.equal('next') + // }) + // }) + + // it('should remove task', () => { + // return service.getTasks() + // .then(tasks => tasks[0]._id) + // .then(taskId => service.deleteTask(taskId)) + // .then(() => service.getTasks()) + // .then(tasks => { + // expect(tasks.length).to.equal(0) + // }) + // }) +}) \ No newline at end of file