Permalink
Browse files

Mongoose upgrade.

Testable and compilable again.
  • Loading branch information...
Orbifold committed Jan 5, 2019
1 parent 7ca9a09 commit d215924829063e034b276a862133f8bcf385f59a
Showing with 74 additions and 73 deletions.
  1. +57 −57 lib/Services/MongoStorage/index.js
  2. +2 −2 lib/Services/QTL/index.js
  3. +3 −3 package.json
  4. +2 −2 test/Configurator.js
  5. +5 −5 test/Datetime.js
  6. +1 −0 test/Questions.js
  7. +2 −2 test/Qwiery.js
  8. +2 −2 test/Utils.js
@@ -1,18 +1,18 @@
const StorageBase = require("../../Framework/StorageBase");
const StorageProxy = require("../../Framework/StorageProxy");
const loki = require("lokijs");
const path = require("path");
const _ = require("lodash");
const utils = require("../../utils");
const assert = require("assert");
const StorageBase = require('../../Framework/StorageBase');
const StorageProxy = require('../../Framework/StorageProxy');
const loki = require('lokijs');
const path = require('path');
const _ = require('lodash');
const utils = require('../../utils');
const assert = require('assert');
const mongoose = require('mongoose');

mongoose.set('useFindAndModify', false); //https://github.com/Automattic/mongoose/issues/7108
/**
* Mongo storage implementation.
*/
class MongoStorage extends StorageBase {
constructor() {
super("storage");
super('storage');
this.collections = {};
this._isConnected = false;

@@ -21,7 +21,7 @@ class MongoStorage extends StorageBase {

init(instantiator) {
super.init(instantiator);
if(this._isConnected) {
if (this._isConnected) {
return Promise.resolve();
}
return this._setupConnection(this.settings);
@@ -38,32 +38,32 @@ class MongoStorage extends StorageBase {
let dbURI, dbOptions;

let options = this.getPluginSettings();
if(this._isConnected) {
if (this._isConnected) {
return Promise.resolve();
}
mongoose.Promise = global.Promise;
return new Promise(function(resolve, reject) {
return new Promise(function (resolve, reject) {
// that.mongoose.connection.on('connected', function() {
// console.log('Mongoose connected to ' + dbURI);
// that._isConnected = true;
// resolve();
// });
mongoose.connection.on('error', function(err) {
mongoose.connection.on('error', function (err) {
console.log('Mongoose connection error: ' + err);
});
// that.mongoose.connection.on('disconnected', function() {
// console.log('Mongoose disconnected');
// that._isConnected = false;
// that.mongoose = null;
// });
process.on('SIGINT', function() {
mongoose.connection.close(function() {
process.on('SIGINT', function () {
mongoose.connection.close(function () {
//console.log('Mongoose disconnected through app termination');
process.exit(0);
});
that._isConnected = false;
});
if(_.isString(options)) {
if (_.isString(options)) {
dbURI = options;
} else {

@@ -85,8 +85,9 @@ class MongoStorage extends StorageBase {
// }
// );
that.db = mongoose.createConnection(dbURI, {
server: {socketOptions: {keepAlive: 300000, connectTimeoutMS: 30000}},
replset: {socketOptions: {keepAlive: 300000, connectTimeoutMS: 30000}}
keepAlive: 300000,
connectTimeoutMS: 30000,
useNewUrlParser: true
});
that._isConnected = true;
resolve()
@@ -103,25 +104,24 @@ class MongoStorage extends StorageBase {
}
* */
createCollection(specs) {
if(_.isString(specs)) {
if (_.isString(specs)) {
specs = {
collectionName: specs,
storageName: specs
};
}
else {
if(_.isNil(specs.collectionName)) {
throw new Error("No collectionName specified.");
} else {
if (_.isNil(specs.collectionName)) {
throw new Error('No collectionName specified.');
}
if(_.isNil(specs.storageName)) {
if (_.isNil(specs.storageName)) {
specs.storageName = specs.collectionName;
}
}

if(utils.isUndefined(specs.schema)) {
throw new Error("Cannot create a Mongo collection without a schema.");
if (utils.isUndefined(specs.schema)) {
throw new Error('Cannot create a Mongo collection without a schema.');
}
if(utils.isDefined(this.db.models[specs.storageName])) {
if (utils.isDefined(this.db.models[specs.storageName])) {
return Promise.resolve();
}
const schema = new mongoose.Schema(specs.schema, {collection: specs.storageName});
@@ -132,16 +132,16 @@ class MongoStorage extends StorageBase {

collectionExists(storageName) {
const that = this;
return new Promise(function(resolve, reject) {
return new Promise(function (resolve, reject) {
resolve(that.db.base.modelSchemas.hasOwnProperty(storageName));
});
}

getCollection(storageName) {
if(utils.isUndefined(storageName)) {
throw new Error("Cannot create or fetch an empty storage collection.");
if (utils.isUndefined(storageName)) {
throw new Error('Cannot create or fetch an empty storage collection.');
}
if(_.isNil(this.collections[storageName])) {
if (_.isNil(this.collections[storageName])) {
throw new Error(`The collection '${storageName}' has not been created.`);
}
return this.collections[storageName];
@@ -150,9 +150,9 @@ class MongoStorage extends StorageBase {
insert(blob, storageName) {
let collection = this.getCollection(storageName);
let item = new collection(blob);
return new Promise(function(resolve, reject) {
item.save(function(err, topi, affected) {
if(err) reject(err);
return new Promise(function (resolve, reject) {
item.save(function (err, topi, affected) {
if (err) reject(err);
else resolve(topi);
})
});
@@ -162,9 +162,9 @@ class MongoStorage extends StorageBase {
update(blob, storageName, condition = {}) {
let collection = this.getCollection(storageName);
const that = this;
return new Promise(function(resolve, reject) {
collection.findOneAndUpdate(condition, blob, {upsert: true}, function(err, doc) {
if(err) reject(err);
return new Promise(function (resolve, reject) {
collection.findOneAndUpdate(condition, blob, {upsert: true}, function (err, doc) {
if (err) reject(err);
else resolve(doc);
});

@@ -177,10 +177,10 @@ class MongoStorage extends StorageBase {

find(specs = {}, storageName, sortField, limit = 10000) {
let collection = this.getCollection(storageName);
return new Promise(function(resolve, reject) {
return new Promise(function (resolve, reject) {
let sorter = {};
if(utils.isDefined(sortField)) {
if(_.isString(sortField)) {
if (utils.isDefined(sortField)) {
if (_.isString(sortField)) {
sorter[sortField] = 1;
} else {
let field = _.keys(sortField)[0];
@@ -189,8 +189,8 @@ class MongoStorage extends StorageBase {
sorter[field] = desc ? -1 : 1;
}
}
collection.find(specs).sort(sorter).limit(limit).exec(function(err, items) {
if(err) {
collection.find(specs).sort(sorter).limit(limit).exec(function (err, items) {
if (err) {
reject(err);
} else {
resolve(items);
@@ -202,31 +202,30 @@ class MongoStorage extends StorageBase {

count(specs = {}, storageName) {
let collection = this.getCollection(storageName);
return new Promise(function(resolve, reject) {
collection.count(specs, function(err, count) {
return new Promise(function (resolve, reject) {
collection.countDocuments(specs, function (err, count) {
resolve(count);
});
});
}

findOne(specs = {}, storageName) {
const collection = this.getCollection(storageName);
return new Promise(function(resolve, reject) {
return new Promise(function (resolve, reject) {
assert(utils.isDefined(collection), `Could not fetch ${storageName} collection.`);
collection.findOne(specs).then(function(item) {
collection.findOne(specs).then(function (item) {
resolve(item);
});
});
}

remove(specs = {}, storageName) {
const collection = this.getCollection(storageName);
return new Promise(function(resolve, reject) {
collection.remove(specs, function(err) {
if(err) {
return new Promise(function (resolve, reject) {
collection.deleteMany(specs, function (err) {
if (err) {
reject(err);
}
else {
} else {
resolve();
}
});
@@ -236,9 +235,9 @@ class MongoStorage extends StorageBase {
distinct(specs = {}, field, storageName) {
const collection = this.getCollection(storageName);

return new Promise(function(resolve, reject) {
collection.find(specs).distinct(field, function(err, items) {
if(err) {
return new Promise(function (resolve, reject) {
collection.find(specs).distinct(field, function (err, items) {
if (err) {
reject(err);
} else {
resolve(items);
@@ -254,8 +253,8 @@ class MongoStorage extends StorageBase {
this._isConnected = false;
this._busyConnecting = false;
const that = this;
return new Promise(function(resolve, reject) {
that.db.disconnect().then(function() {
return new Promise(function (resolve, reject) {
that.db.disconnect().then(function () {
resolve();
});
});
@@ -266,4 +265,5 @@ class MongoStorage extends StorageBase {
}

}
module.exports = MongoStorage;

module.exports = MongoStorage;
@@ -103,8 +103,8 @@ class QTL extends ServiceBase {
setPersonalization(key, value){
return that.services.personalization.addPersonalization(key, value, ctx);
},
setPersonality( value){
return that.services.personality.add(key, value, ctx);
setPersonality(name){
return that.services.personality.addPersonality(name, ctx);
},
capture(key, value){
value = Language.capture(key, value);
@@ -1,7 +1,7 @@
{
"name": "qwiery",
"version": "1.6.2",
"_versionDate": "July 1, 2017",
"version": "1.6.3",
"_versionDate": "January 1, 2019",
"_serviceUrl": "http://api.qwiery.com",
"main": "lib/index.js",
"author": {
@@ -53,7 +53,7 @@
"markdown-it": "^8.3.1",
"mathjs": "^3.11.5",
"moment": "^2.18.1",
"mongoose": "^4.9.4",
"mongoose": "^5.4.2",
"node-tldr": "^0.4.14",
"path": "^0.12.7",
"pos": "^0.4.2",
@@ -174,7 +174,7 @@ exports.validatePlugins = function(test) {

const set14 = {
plugins: [{
path: "/anaconda"
path: "/Users/Swa/conda"
}]
};
try {
@@ -247,4 +247,4 @@ exports.validateDefaultApp = function(test) {
test.fail(e.message)
}
test.done();
};
};
@@ -23,8 +23,8 @@ exports.between = function(test) {
let d2 = d[1];
test.equal(d1.text, "January 21st");
test.equal(d2.text, "March 2nd");
test.equal(d1.date.toDateString(), new Date(2017, 0, 21).toDateString());
test.equal(d2.date.toDateString(), new Date(2017, 2, 2).toDateString());
test.equal(d1.date.toDateString(), new Date(2019, 0, 21).toDateString());
test.equal(d2.date.toDateString(), new Date(2019, 2, 2).toDateString());
test.done();
};

@@ -48,8 +48,8 @@ exports.pm = function(test) {

exports.hour = function(test) {
let d = Datetime.parse("It's now 15:45");
test.equal(d.length, 1);
test.equal(d[0].date.toDateString(), new Date().toDateString());
console.log(d[0].date.toLocaleString());
test.equal(d.length, 2);
test.equal(d[1].date.toDateString(), new Date().toDateString());
console.log(d[1].date.toLocaleString());
test.done();
};
@@ -107,6 +107,7 @@ exports.date = function(test) {
test.expect(1);
helper.qa("what is the current date", ctx, qwiery).then(function(answer) {
test.ok(answer.indexOf("It's") > -1);
console.log(answer);
test.done();
});
};
@@ -521,7 +521,7 @@ exports.dateParsing = function(test) {
const qwiery = new Qwiery();
qwiery.ask("I leave for Paris on the 22nd of March.", {userId: "Sharon"}).then(function(s) {
test.ok(s.Dates.length > 0);
test.equal(s.Dates[0].date.toDateString(), new Date(2017, 2, 22).toDateString());
test.equal(s.Dates[0].date.toDateString(), new Date(2019, 2, 22).toDateString());
test.done();
});
};
@@ -570,4 +570,4 @@ exports.parallization = function(test) {
test.equal(ss.Output.Answer[1], "Second");
test.done();
});
};
};
@@ -49,8 +49,8 @@ exports.getJsonPath = function(test) {
};

exports.getSiteTitle = function(test) {
utils.getSiteTitle("http://www.qwiery.com").then(function(s) {
test.equal(s, "Qwiery – Think. Listen. Understand.");
utils.getSiteTitle("http://www.orbifold.net").then(function(s) {
test.equal(s, "Orbifold Consulting – Deep Understanding at Your Service");
utils.getSiteTitle("stuff").then(function(s) {
test.equal(s, "stuff");
test.done();

0 comments on commit d215924

Please sign in to comment.