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": {
"type": "file"
"type": "file",
},
"documents": {

View file

@ -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;

View file

@ -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": {

View file

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

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