mirror of
https://github.com/seejohnrun/haste-server
synced 2025-08-22 13:03:28 -07:00
Fix: MongoDB not working
This commit is contained in:
parent
7af15cc32d
commit
fa68daea12
5 changed files with 85 additions and 29 deletions
|
@ -33,7 +33,7 @@
|
||||||
},
|
},
|
||||||
|
|
||||||
"storage": {
|
"storage": {
|
||||||
"type": "file"
|
"type": "file",
|
||||||
},
|
},
|
||||||
|
|
||||||
"documents": {
|
"documents": {
|
||||||
|
|
|
@ -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,
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}, {
|
},
|
||||||
|
{
|
||||||
|
$set: {
|
||||||
'entry_id': key,
|
'entry_id': key,
|
||||||
'value': data,
|
'value': data,
|
||||||
'expiration': that.expire && !skipExpire ? that.expire + now : -1
|
'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);
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
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({
|
||||||
|
$and: [
|
||||||
|
{
|
||||||
'entry_id': key,
|
'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;
|
||||||
|
|
|
@ -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": {
|
||||||
|
|
|
@ -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);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
41
test/mongodb_document_store_spec.js
Normal file
41
test/mongodb_document_store_spec.js
Normal 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)
|
||||||
|
// })
|
||||||
|
// })
|
||||||
|
})
|
Loading…
Add table
Add a link
Reference in a new issue