Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
language: node_js
node_js:
- "0.11"
- "0.10"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we wont test against 0.11? I'm not sure if it matters, but I know other waterline repos test against 0.11.
Edit I saw the discussion around this, nevermind!

- "0.8"
- "0.12"
before_script:
- sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
- echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
- sudo apt-get update
- sudo apt-get install -y mongodb-org=2.6.5 mongodb-org-server=2.6.5 mongodb-org-shell=2.6.5 mongodb-org-mongos=2.6.5 mongodb-org-tools=2.6.5
- sleep 15 #mongo may not be responded directly. See http://docs.travis-ci.com/user/database-setup/#MongoDB
- mongo --version
services:
- mongodb
14 changes: 8 additions & 6 deletions lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var Errors = require('waterline-errors').adapter;
var ObjectId = require('mongodb').ObjectID;
var _runJoins = require('waterline-cursor');
var util = require('util');
var _ = require('lodash');


module.exports = (function() {
Expand Down Expand Up @@ -79,10 +80,12 @@ module.exports = (function() {
*/

registerConnection: function(connection, collections, cb) {

if(!connection.identity) return cb(Errors.IdentityMissing);
if(connections[connection.identity]) return cb(Errors.IdentityDuplicate);

// Merging default options
connection = _.defaults(connection, this.defaults);

// Store the connection
connections[connection.identity] = {
config: connection,
Expand All @@ -91,7 +94,7 @@ module.exports = (function() {

// Create a new active connection
new Connection(connection, function(_err, db) {

if(_err) {
return cb((function _createError(){
var msg = util.format('Failed to connect to MongoDB. Are you sure your configured Mongo instance is running?\n Error details:\n%s', util.inspect(_err, false, null));
Expand Down Expand Up @@ -151,11 +154,10 @@ module.exports = (function() {
var connectionObject = connections[connectionName];
var collection = connectionObject.collections[collectionName];
var schema = collection.schema;
var names = connectionObject.connection.db.listCollections(collectionName);
if(names.length > 0) return cb(null, schema);
cb();

connectionObject.connection.db.collectionNames(collectionName, function(err, names) {
if(names.length > 0) return cb(null, schema);
cb();
});
},

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ Collection.prototype.stream = function find(criteria, stream) {
*/

Collection.prototype.insert = function insert(values, cb) {

var self = this;

// Normalize values to an array
Expand All @@ -180,7 +181,7 @@ Collection.prototype.insert = function insert(values, cb) {

this.connection.db.collection(this.identity).insert(docs, function(err, results) {
if(err) return cb(err);
cb(null, utils.rewriteIds(results, self.schema));
cb(null, utils.rewriteIds(results.ops, self.schema));
});
};

Expand Down
2 changes: 1 addition & 1 deletion lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Connection.prototype._buildConnection = function _buildConnection(cb) {
connectionOptions.replSet = this.config.replSet || {};

// Build up options used for creating a Server instance
connectionOptions.server_options = {
connectionOptions.server = {
readPreference: this.config.readPreference,
ssl: this.config.ssl,
poolSize: this.config.poolSize,
Expand Down
10 changes: 7 additions & 3 deletions lib/query/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ Query.prototype.parseValue = function parseValue(field, modifier, val) {
"use strict";
var self = this;

// Omit adding regex value to these modifiers
var omitRegExModifiers = ['$ne', 'greaterThan', '>', 'gt', 'greaterThanOrEqual',
'>=', 'gte', '$gt', '$gte'
];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should '<', 'lessThan', '<=', 'lessThanOrEqual' also be here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmarcelino I didn't add the lessthan operator because waterline adapter test didn't complain. However, with my curiosity I just ran a few query for the lessthan operator with regex and it barf with error same as the above operators "BadValue Can't have RegEx as arg to predicate over field". I will add the lessthan to the list. Thanks.


// Look and see if the key is in the schema, id attribute and all association
// attributes are objectid type by default (@see { @link collection._parseDefinition }).
if (hop(self.schema, field) && self.schema[field].type === 'objectid') {
Expand Down Expand Up @@ -344,13 +349,12 @@ Query.prototype.parseValue = function parseValue(field, modifier, val) {
if (self.schema[field].type === 'date') {
return new Date(val);
}

}

if(modifier === '$ne') {
if (omitRegExModifiers.indexOf(modifier) > -1) {
return val;
}

// Replace Percent Signs, work in a case insensitive fashion by default
val = utils.caseInsensitive(val);
val = val.replace(/%/g, '.*');
Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
{
"name": "Ted Kulp",
"email": "github@wishy.org"
},
{
"name": "Andy Pham",
"email": "andyphamhung@gmail.com"
}
],
"license": "MIT",
Expand All @@ -39,9 +43,10 @@
"dependencies": {
"async": "~0.8.0",
"lodash": "~2.4.1",
"mongodb": "1.4.26",
"waterline-errors": "~0.10.0",
"waterline-cursor": "~0.0.5"
"mongodb": "^2.0.27",
"waterline-adapter-tests": "^0.10.10",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

waterline-adapter-tests should be on devDependencies.

"waterline-cursor": "~0.0.5",
"waterline-errors": "~0.10.0"
},
"devDependencies": {
"mocha": "*",
Expand Down
20 changes: 18 additions & 2 deletions test/unit/query/adapter.query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,22 @@ describe('Query', function () {
assert(_.isEqual(actual, expect));
});

it('should accept `$gt` selector without turning string value to a RegExp', function () {
var where = { name: { $gt: 'banana' } };
var expect = _.cloneDeep(where);
var Q = new Query({ where: where }, { name: 'string' });
var actual = Q.criteria.where;
assert(_.isEqual(actual, expect));
});

it('should accept `$gte` selector without turning string value to a RegExp', function () {
var where = { name: { $gte: 'apple' } };
var expect = _.cloneDeep(where);
var Q = new Query({ where: where }, { name: 'string' });
var actual = Q.criteria.where;
assert(_.isEqual(actual, expect));
});

});


Expand All @@ -233,12 +249,12 @@ describe('Query', function () {
var where = {
or: [{ name: { $exists: false } }, { name: { '!': 'clark' } }]
};
var expect = { $or: [ { name: { $exists: false } }, { name: { $ne: /^clark$/i } } ] };
var expect = { $or: [ { name: { $exists: false } }, { name: { $ne: 'clark' } } ] };
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious why this changed.
Edit I read better and figured it out :P

var Q = new Query({ where: where }, { name: 'string', age: 'integer' });
var actual = Q.criteria.where;
assert(_.isEqual(actual, expect));
});

});

});
});