diff --git a/.travis.yml b/.travis.yml index 76e2872ee..0200ae4d9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,13 @@ language: node_js node_js: - - "0.11" - "0.10" - - "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 diff --git a/lib/adapter.js b/lib/adapter.js index dbe8f96cb..563827de0 100644 --- a/lib/adapter.js +++ b/lib/adapter.js @@ -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() { @@ -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, @@ -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)); @@ -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(); - }); }, /** diff --git a/lib/collection.js b/lib/collection.js index 01df1b278..ec517c294 100644 --- a/lib/collection.js +++ b/lib/collection.js @@ -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 @@ -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)); }); }; diff --git a/lib/connection.js b/lib/connection.js index f3f14f8f8..88941647f 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -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, diff --git a/lib/query/index.js b/lib/query/index.js index 76ebd1ac0..210cb2eb9 100644 --- a/lib/query/index.js +++ b/lib/query/index.js @@ -291,6 +291,12 @@ 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', '<', 'lessThan', '<=', + 'lessThanOrEqual' + ]; + // 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') { @@ -344,13 +350,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, '.*'); diff --git a/package.json b/package.json index 87dd76b08..d8c3c7bbf 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,10 @@ { "name": "Ted Kulp", "email": "github@wishy.org" + }, + { + "name": "Andy Pham", + "email": "andyphamhung@gmail.com" } ], "license": "MIT", @@ -39,9 +43,9 @@ "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-cursor": "~0.0.5", + "waterline-errors": "~0.10.0" }, "devDependencies": { "mocha": "*", diff --git a/test/unit/query/adapter.query.test.js b/test/unit/query/adapter.query.test.js index b911f155a..ee0da4cfd 100644 --- a/test/unit/query/adapter.query.test.js +++ b/test/unit/query/adapter.query.test.js @@ -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)); + }); + }); @@ -233,7 +249,7 @@ 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' } } ] }; var Q = new Query({ where: where }, { name: 'string', age: 'integer' }); var actual = Q.criteria.where; assert(_.isEqual(actual, expect)); @@ -241,4 +257,4 @@ describe('Query', function () { }); -}); \ No newline at end of file +});