Fix: MongoDB not working

This commit is contained in:
Bharath S 2021-12-04 14:46:17 +05:30
commit fa68daea12
5 changed files with 85 additions and 29 deletions

View file

@ -33,7 +33,7 @@
}, },
"storage": { "storage": {
"type": "file" "type": "file",
}, },
"documents": { "documents": {

View file

@ -11,30 +11,30 @@ var MongoDocumentStore = function (options) {
MongoDocumentStore.prototype.set = function (key, data, callback, skipExpire) { 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) {
if (err) if (err)
return callback(false); return callback(false);
db.collection('entries').update({ db.collection('entries').updateOne({
'entry_id': key,
$or: [ $or: [
{ expiration: -1 }, { expiration: -1 },
{ expiration: { $gt: now } } { expiration: { $gt: now } }
],
$and: [
{
'entry_id': key,
}
] ]
}, { },
'entry_id': key, {
'value': data, $set: {
'expiration': that.expire && !skipExpire ? that.expire + now : -1 'entry_id': key,
}, { 'value': data,
upsert: true 'expiration': that.expire && !skipExpire ? that.expire + now : -1
}, function (err, existing) {
if (err) {
winston.error('error persisting value to mongodb', { error: err });
return callback(false);
} }
},
callback(true); {
upsert: true
}); });
}); });
}; };
@ -48,7 +48,11 @@ MongoDocumentStore.prototype.get = function (key, callback, skipExpire) {
return callback(false); return callback(false);
db.collection('entries').findOne({ db.collection('entries').findOne({
'entry_id': key, $and: [
{
'entry_id': key,
}
],
$or: [ $or: [
{ expiration: -1 }, { expiration: -1 },
{ expiration: { $gt: now } } { expiration: { $gt: now } }
@ -62,8 +66,12 @@ MongoDocumentStore.prototype.get = function (key, callback, skipExpire) {
callback(entry === null ? false : entry.value); callback(entry === null ? false : entry.value);
if (entry !== null && entry.expiration !== -1 && that.expire && !skipExpire) { if (entry !== null && entry.expiration !== -1 && that.expire && !skipExpire) {
db.collection('entries').update({ db.collection('entries').updateOne({
'entry_id': key $and: [
{
'entry_id': key,
}
],
}, { }, {
$set: { $set: {
'expiration': that.expire + now 'expiration': that.expire + now
@ -74,8 +82,8 @@ MongoDocumentStore.prototype.get = function (key, callback, skipExpire) {
}); });
}; };
MongoDocumentStore.prototype.safeConnect = function (callback) { MongoDocumentStore.prototype.safeConnect = async function (callback) {
MongoClient.connect(this.connectionUrl, function (err, db) { await MongoClient.connect(this.connectionUrl, function (err, db) {
if (err) { if (err) {
winston.error('error connecting to mongodb', { error: err }); winston.error('error connecting to mongodb', { error: err });
callback(err); callback(err);
@ -85,4 +93,5 @@ MongoDocumentStore.prototype.safeConnect = function (callback) {
}); });
}; };
module.exports = MongoDocumentStore; module.exports = MongoDocumentStore;

View file

@ -26,7 +26,10 @@
"winston": "^2.0.0" "winston": "^2.0.0"
}, },
"devDependencies": { "devDependencies": {
"mocha": "^8.1.3" "mocha": "^8.1.3",
"mongodb": "^4.2.1",
"mongoose": "^6.0.14",
"nodeman": "^1.1.2"
}, },
"bundledDependencies": [], "bundledDependencies": [],
"bin": { "bin": {

View file

@ -20,14 +20,17 @@ describe('RandomKeyGenerator', function() {
}, Error); }, Error);
}); });
it('should return a key of the proper number of words from the given dictionary', () => { it('should return a key of the proper number of words from the given dictionary', (done) => {
const path = '/tmp/haste-server-test-dictionary'; const path = 'data/haste-server-test-dictionary';
const words = ['cat']; const charList = 'cat';
fs.writeFileSync(path, words.join('\n')); fs.writeFile(path, charList, function (err) {
if (err) throw err;
const gen = new Generator({path}, () => {
assert.equal('catcatcat', gen.createKey(3));
}); });
done();
const gen = new Generator({path});
const key = gen.createKey(3);
assert.equal(6, key.length);
fs.unlinkSync(path);
}); });
}); });
}); });

View file

@ -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)
// })
// })
})