From 48a3a7c47dff1efeca376ba34d253a97feed6fd6 Mon Sep 17 00:00:00 2001 From: Lucas Hrabovsky Date: Mon, 25 Jul 2016 10:50:30 -0400 Subject: [PATCH 1/6] INT-1650: Make ssh tunnels easier to debug --- lib/data-service.js | 23 +++++++++++++++-------- lib/native-client.js | 14 ++++++++++---- lib/ssh-tunnel-connector.js | 24 +++++++++++++++--------- 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/lib/data-service.js b/lib/data-service.js index d16aab53..506575c5 100644 --- a/lib/data-service.js +++ b/lib/data-service.js @@ -63,20 +63,27 @@ class DataService extends EventEmitter { /** * Connect to the server. * - * @param {function} callback - The callback function. + * @param {function} done - The callback function. */ - connect(callback) { - debug('Connecting to MongoDB.'); - this.connector.connect((tunnelError) => { + connect(done) { + debug('Connecting...'); + debug('Creating SSH tunnel if requested...'); + this.connector.connect((tunnelError, tunnelCreated) => { if (tunnelError) { - return callback(tunnelError, this); + return done(tunnelError, this); + } + if (tunnelCreated) { + debug('SSH tunnel created'); } - this.client.connect((error) => { - debug('Data Service is readable.'); + + this.client.connect((err) => { + if (err) { + return done(err); + } process.nextTick(() => { this.emit(Events.Readable); }); - return callback(error, this); + done(null, this); }); }); } diff --git a/lib/native-client.js b/lib/native-client.js index 06f2e7c9..5835cf47 100644 --- a/lib/native-client.js +++ b/lib/native-client.js @@ -8,6 +8,7 @@ const getIndexes = require('mongodb-index-model').fetch; const createSampleStream = require('mongodb-collection-sample'); const parseNamespace = require('mongodb-ns'); const translate = require('mongodb-js-errors').translate; +const debug = require('debug')('mongodb-data-service:native-client'); /** * The native client class. @@ -27,12 +28,17 @@ class NativeClient { /** * Connect to the server. * - * @param {function} callback - The callback function. + * @param {function} done - The callback function. */ - connect(callback) { - createConnection(this.model, (error, database) => { + connect(done) { + debug('connecting...'); + createConnection(this.model, (err, database) => { + if (err) { + return done(this._translateMessage(err)); + } + debug('connected!'); this.database = database; - callback(this._translateMessage(error), this); + done(null, this); }); } diff --git a/lib/ssh-tunnel-connector.js b/lib/ssh-tunnel-connector.js index 38d46e81..b3c61f28 100644 --- a/lib/ssh-tunnel-connector.js +++ b/lib/ssh-tunnel-connector.js @@ -41,14 +41,14 @@ class SshTunnelConnector extends EventEmitter { connect(done) { if (!this.options.host) { debug('No SSH tunnel host option found - using direct connection.'); - return done(); + return done(null, false); } const connectMessage = this._connectMessage(); debug(connectMessage); this.emit(Events.Connecting, connectMessage); - tunnel(this.options, (err) => { + const onTunnelConnected = (err) => { if (err) { this.emit(Events.Error, this._errorMessage()); debug('error setting up tunnel', err); @@ -56,10 +56,11 @@ class SshTunnelConnector extends EventEmitter { } debug('tunnel opened - testing'); this.test(done); - }).on('error', (err) => { - this.emit(Events.Error, this._errorMessage()); - debug('tunnel write failed', err); - done(err); + }; + + debug('creating', this.options); + tunnel(this.options, onTunnelConnected).on('error', (err) => { + debug('tunnel error', err); }); } /** @@ -86,11 +87,16 @@ class SshTunnelConnector extends EventEmitter { debug('write to test client failed with error', err); return done(err); } - setTimeout(() => { - client.end(); - debug('test successful - emitting %s', Events.Ready); + + client.on('end', () => { + debug('disconnecting test socket'); + debug('emitting %s', Events.Ready); this.emit(Events.Ready); done(null, true); + }); + + setTimeout(() => { + client.end(); }, 300); }); } From d827d808ea8c110b47698d90cc38d916cc05ee0b Mon Sep 17 00:00:00 2001 From: Lucas Hrabovsky Date: Mon, 25 Jul 2016 18:02:21 -0400 Subject: [PATCH 2/6] Move SSH Tunnel to mongodb-connection-model Depends on https://github.com/mongodb-js/connection-model/pull/87 being merged and published. Then updating mongodb-connection-model in package.json. --- lib/data-service.js | 38 ++------- lib/native-client.js | 11 ++- lib/ssh-tunnel-connector.js | 124 ------------------------------ package.json | 3 +- test/ssh-tunnel-connector.test.js | 60 --------------- 5 files changed, 15 insertions(+), 221 deletions(-) delete mode 100644 lib/ssh-tunnel-connector.js delete mode 100644 test/ssh-tunnel-connector.test.js diff --git a/lib/data-service.js b/lib/data-service.js index 506575c5..6be7051d 100644 --- a/lib/data-service.js +++ b/lib/data-service.js @@ -2,7 +2,6 @@ const debug = require('debug')('mongodb-data-service:data-service'); const NativeClient = require('./native-client'); -const SshTunnelConnector = require('./ssh-tunnel-connector'); const Router = require('./router'); const EventEmitter = require('events'); @@ -31,21 +30,8 @@ class DataService extends EventEmitter { */ constructor(model) { super(); - this.connector = new SshTunnelConnector(model.ssh_tunnel_options); - this.client = new NativeClient(model); + this.client = new NativeClient(model).on('status', (evt) => this.emit('status', evt)); this.router = new Router(); - - this.connector.on(SshTunnelConnector.Events.Connecting, (message) => { - process.nextTick(() => { - this.emit(Events.Connecting, message); - }); - }); - - this.connector.on(SshTunnelConnector.Events.Error, (message) => { - process.nextTick(() => { - this.emit(Events.Error, message); - }); - }); } /** @@ -67,24 +53,12 @@ class DataService extends EventEmitter { */ connect(done) { debug('Connecting...'); - debug('Creating SSH tunnel if requested...'); - this.connector.connect((tunnelError, tunnelCreated) => { - if (tunnelError) { - return done(tunnelError, this); - } - if (tunnelCreated) { - debug('SSH tunnel created'); + this.client.connect((err) => { + if (err) { + return done(err); } - - this.client.connect((err) => { - if (err) { - return done(err); - } - process.nextTick(() => { - this.emit(Events.Readable); - }); - done(null, this); - }); + done(null, this); + this.emit(Events.Readable); }); } diff --git a/lib/native-client.js b/lib/native-client.js index 5835cf47..39ff6d5c 100644 --- a/lib/native-client.js +++ b/lib/native-client.js @@ -2,7 +2,8 @@ const _ = require('lodash'); const async = require('async'); -const createConnection = require('mongodb-connection-model').connect; +const EventEmitter = require('events'); +const connect = require('mongodb-connection-model').connect; const getInstance = require('mongodb-instance-model').get; const getIndexes = require('mongodb-index-model').fetch; const createSampleStream = require('mongodb-collection-sample'); @@ -13,7 +14,7 @@ const debug = require('debug')('mongodb-data-service:native-client'); /** * The native client class. */ -class NativeClient { +class NativeClient extends EventEmitter { /** * Instantiate a new NativeClient object. @@ -22,6 +23,7 @@ class NativeClient { * @param {Connection} model - The Connection model. */ constructor(model) { + super(); this.model = model; } @@ -29,10 +31,11 @@ class NativeClient { * Connect to the server. * * @param {function} done - The callback function. + * @return {NativeClient} */ connect(done) { debug('connecting...'); - createConnection(this.model, (err, database) => { + this.client = connect(this.model, (err, database) => { if (err) { return done(this._translateMessage(err)); } @@ -40,6 +43,8 @@ class NativeClient { this.database = database; done(null, this); }); + this.client.on('status', (evt) => this.emit('status', evt)); + return this; } /** diff --git a/lib/ssh-tunnel-connector.js b/lib/ssh-tunnel-connector.js deleted file mode 100644 index b3c61f28..00000000 --- a/lib/ssh-tunnel-connector.js +++ /dev/null @@ -1,124 +0,0 @@ -'use strict'; - -const debug = require('debug')('mongodb-data-service:ssh-tunnel-connector'); -const tunnel = require('tunnel-ssh'); -const EventEmitter = require('events'); -const net = require('net'); - -const Events = { - Connecting: 'SshTunnelConnector:Connecting', - Testing: 'SshTunnelConnector:Testing', - Ready: 'SshTunnelConnector:Ready', - Error: 'SshTunnelConnector:Error' -}; - -/** - * Instantiate a new SshTunnelConnector object. - * - * @constructor - * @param {Object} options - The ssh tunnel options. - */ -class SshTunnelConnector extends EventEmitter { - - /** - * Instantiate a new SshTunnelConnector object. - * - * @constructor - * @param {Object} options - The ssh tunnel options. - */ - constructor(options) { - super(); - this.options = options; - } - - /** - * Connect to the SSH tunnel and execute the callback. - * - * @param {Function} done - The callback. - * - * @returns {Object} The executed callback. - */ - connect(done) { - if (!this.options.host) { - debug('No SSH tunnel host option found - using direct connection.'); - return done(null, false); - } - - const connectMessage = this._connectMessage(); - debug(connectMessage); - this.emit(Events.Connecting, connectMessage); - - const onTunnelConnected = (err) => { - if (err) { - this.emit(Events.Error, this._errorMessage()); - debug('error setting up tunnel', err); - return done(err); - } - debug('tunnel opened - testing'); - this.test(done); - }; - - debug('creating', this.options); - tunnel(this.options, onTunnelConnected).on('error', (err) => { - debug('tunnel error', err); - }); - } - /** - * Test that a tunnel can actually be created by opening a socket - * to it and writing some data. - * - * @param {Function} done - The callback. - */ - test(done) { - this.emit(Events.Testing); - var client = new net.Socket(); - client.on('error', function(err) { - debug('test client got an error', err); - client.end(); - done(err); - }); - - debug('test client connecting to %s:%s', this.options.dstHost, this.options.dstPort); - client.connect(this.options.dstPort, this.options.dstHost, () => { - debug('writing test message'); - try { - client.write('mongodb-data-service:ssh-tunnel-connector: ping'); - } catch (err) { - debug('write to test client failed with error', err); - return done(err); - } - - client.on('end', () => { - debug('disconnecting test socket'); - debug('emitting %s', Events.Ready); - this.emit(Events.Ready); - done(null, true); - }); - - setTimeout(() => { - client.end(); - }, 300); - }); - } - - /** - * Get the connecting message. - * - * @returns {String} The connecting message. - */ - _connectMessage() { - return `Attempting SSH connection to server at ${this.options.host}`; - } - - /** - * Get the error message. - * - * @returns {String} The error message. - */ - _errorMessage() { - return `Failed to connect to ${this.options.host} via SSH tunnel.`; - } -} - -module.exports = SshTunnelConnector; -module.exports.Events = Events; diff --git a/package.json b/package.json index 6a6c604b..a3c60be4 100644 --- a/package.json +++ b/package.json @@ -34,8 +34,7 @@ "mongodb-instance-model": "^3.1.0", "mongodb-js-errors": "^0.2.1", "mongodb-ns": "^1.0.3", - "mongodb-url": "^1.0.2", - "tunnel-ssh": "^2.1.1" + "mongodb-url": "^1.0.2" }, "devDependencies": { "chai": "^3.4.1", diff --git a/test/ssh-tunnel-connector.test.js b/test/ssh-tunnel-connector.test.js deleted file mode 100644 index 32024a6c..00000000 --- a/test/ssh-tunnel-connector.test.js +++ /dev/null @@ -1,60 +0,0 @@ -'use strict'; - -const helper = require('./helper'); -const assert = helper.assert; -const expect = helper.expect; - -const NativeClient = require('../lib/native-client'); -const fixture = require('mongodb-connection-fixture'); -const Connection = require('mongodb-connection-model'); -const SshTunnelConnector = require('../lib/ssh-tunnel-connector'); -const _ = require('lodash'); - -describe('SshTunnelConnector', function() { - this.timeout(15000); - - if (fixture.SSH_TUNNEL_MATRIX.length > 0) { - _.map(fixture.SSH_TUNNEL_MATRIX, function(model) { - var connection = new Connection(model); - var client = new NativeClient(connection); - - describe('#connect', function() { - var connector = new SshTunnelConnector(connection.ssh_tunnel_options); - - it('connects to the ssh tunnel', function(done) { - connector.connect(function() { - client.connect(function(err) { - expect(err).to.equal(null); - client.find('mongodb.fanclub', {}, { limit: 10 }, function(error, docs) { - assert.equal(null, error); - expect(docs.length).to.equal(10); - done(); - }); - }); - }); - }); - }); - }); - } - describe('#regression', function() { - /** - * @see https://jira.mongodb.org/browse/INT-1510 - */ - it('should error when ssh fails', function(done) { - var connector = new SshTunnelConnector({ - dstHost: 'localhost', - dstPort: 27107, - username: 'foo', - password: 'bar', - host: 'remotehost', - sshPort: 22 - }); - - connector.connect(function(err) { - expect(err).not.to.equal(null, 'should have an error'); - expect(err).not.to.equal(undefined, 'should have an error'); - done(); - }); - }); - }); -}); From d7e4f6c2e3d2ef439933c219976fdb78ba8ae35a Mon Sep 17 00:00:00 2001 From: Lucas Hrabovsky Date: Tue, 26 Jul 2016 13:18:32 -0400 Subject: [PATCH 3/6] :arrow_up: mongodb-connection-model@5.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a3c60be4..83c55f77 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "lodash": "^4.6.1", "mongodb": "^2.1.10", "mongodb-collection-sample": "^1.2.0", - "mongodb-connection-model": "^4.3.0", + "mongodb-connection-model": "^5.0.0", "mongodb-index-model": "^0.5.3", "mongodb-instance-model": "^3.1.0", "mongodb-js-errors": "^0.2.1", From 7f097c7e4cf50bff0443d942b2c5deacfc79aafd Mon Sep 17 00:00:00 2001 From: Lucas Hrabovsky Date: Tue, 26 Jul 2016 13:30:01 -0400 Subject: [PATCH 4/6] :memo: :art: Remove `Events` and give README some love --- README.md | 98 +++++++++++++++++++++------------------------ lib/data-service.js | 12 +----- 2 files changed, 47 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index 51941cf3..a66aa0ed 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,6 @@ # mongodb-data-service [![][travis_img]][travis_url] [![][npm_img]][npm_url] -> MongoDB Data Service - -The data service provides an API on top of [currently] the MongoDB Node Driver and -[in the future] mongodb-scout-client. +> MongoDB Data Service: an API on top of (currently) the [MongoDB Node Driver][driver] and (some day) [mongodb-scope-client][scope-client]. ## Installation @@ -16,65 +13,60 @@ npm install --save mongodb-data-service ### Instantiating the service. ```javascript - const Connection = require('mongodb-connection-model'); - const DataService = require('mongodb-data-service'); - - var model = new Connection({ hostname: '127.0.0.1', port: 27018, ns: 'data-service' }); - var service = new DataService(model); +const Connection = require('mongodb-connection-model'); +const DataService = require('mongodb-data-service'); + +var service = new DataService(new Connection({ + hostname: '127.0.0.1', + port: 27018, + ns: 'data-service' +})); ``` ### Connecting to the server. -Once the service is ready, it will also emit a `DataService.Events.Readble` event. +Once the service is ready, it will also emit a `readable` event. ```javascript - service.connect((error) => { - assert.equal(null, error); - }); - - function handleReadable() { - console.log('Connected!'); - } - - service.on(DataService.Events.Readable, handleReadable); +service.connect((err) => assert.equal(null, err)}) + .on('readable', () => console.log('Connected!')); ``` ### API ```javascript - - // Get information for a collection. - service.collection('database.collection', {}, (error, result) => { - assert.equal(null, error); - }); - - // Get a document count. - service.count('database.collection', { a: 1 }, {}, (error, count) => { - assert.equal(null, error); - }); - - // Get information for a database. - service.database('database', {}, (error, result) => { - assert.equal(null, error); - }); - - // Find documents in a collection. - service.find('database.collection', { a: 1 }, {}, (error, documents) => { - assert.equal(null, error); - }); - - // Get a result for a RESTful endpoint. - service.get('/collection/database.test', {}, (error, result) => { - assert.equal(null, error); - }); - - // Get instance details. - service.instance({}, (error, result) => { - assert.equal(null, error); - }); - - // Get a sample stream of documents from a collection. - service.sample('database.collection', {}); +// Get information for a collection. +service.collection('database.collection', {}, (error, result) => { + assert.equal(null, error); +}); + +// Get a document count. +service.count('database.collection', { a: 1 }, {}, (error, count) => { + assert.equal(null, error); +}); + +// Get information for a database. +service.database('database', {}, (error, result) => { + assert.equal(null, error); +}); + +// Find documents in a collection. +service.find('database.collection', { a: 1 }, {}, (error, documents) => { + assert.equal(null, error); +}); + +// Get a result for a RESTful endpoint. +service.get('/collection/database.test', {}, (error, result) => { + assert.equal(null, error); +}); + +// Get instance details. +service.instance({}, (error, result) => { + assert.equal(null, error); +}); + +// Get a sample stream of documents from a collection. +service.sample('database.collection', {}); ``` ## License @@ -85,3 +77,5 @@ Apache 2.0 [travis_url]: https://travis-ci.org/mongodb-js/data-service [npm_img]: https://img.shields.io/npm/v/mongodb-data-service.svg?style=flat-square [npm_url]: https://www.npmjs.org/package/mongodb-data-service +[scope-client]: https://github.com/mongodb-js/scope-client +[driver]: https://github.com/mongodb/node-mongodb-native diff --git a/lib/data-service.js b/lib/data-service.js index 6be7051d..67517e5b 100644 --- a/lib/data-service.js +++ b/lib/data-service.js @@ -5,15 +5,6 @@ const NativeClient = require('./native-client'); const Router = require('./router'); const EventEmitter = require('events'); -/** - * Constants for generated events. - */ -const Events = { - Connecting: 'DataService:Connecting', - Readable: 'DataService:Readable', - Error: 'DataService:Error' -}; - /** * Instantiate a new DataService object. * @@ -58,7 +49,7 @@ class DataService extends EventEmitter { return done(err); } done(null, this); - this.emit(Events.Readable); + this.emit('readable'); }); } @@ -318,4 +309,3 @@ class DataService extends EventEmitter { } module.exports = DataService; -module.exports.Events = Events; From c9223e04a5e8ce2a6ecf478d549ccfb2298ad2d3 Mon Sep 17 00:00:00 2001 From: Lucas Hrabovsky Date: Tue, 26 Jul 2016 17:44:02 -0400 Subject: [PATCH 5/6] :green_heart: --- test/data-service.test.js | 2 +- test/native-client.test.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/test/data-service.test.js b/test/data-service.test.js index 15516bcc..f9b87771 100644 --- a/test/data-service.test.js +++ b/test/data-service.test.js @@ -90,7 +90,7 @@ describe('DataService', function() { { returnOriginal: false }, function(error, result) { expect(error).to.equal(null); - expect(result._id).to.deep.equal(id); + expect(result._id.toString()).to.deep.equal(id.toString()); expect(result.b).to.equal(5); expect(result.hasOwnProperty('a')).to.equal(false); done(); diff --git a/test/native-client.test.js b/test/native-client.test.js index 975f173e..14d4f743 100644 --- a/test/native-client.test.js +++ b/test/native-client.test.js @@ -28,7 +28,8 @@ describe('NativeClient', function() { var badConnection = new Connection({ hostname: '127.0.0.1', port: 27050, ns: 'data-service' }); var badClient = new NativeClient(badConnection); - var message = 'MongoDB not running on the provided host and port'; + // var message = 'MongoDB not running on the provided host and port'; + var message = 'Could not connect to MongoDB on the provided host and port'; it('maps the error message', function(done) { badClient.connect(function(error) { expect(error.message).to.equal(message); @@ -262,7 +263,7 @@ describe('NativeClient', function() { { returnOriginal: false }, function(error, result) { expect(error).to.equal(null); - expect(result._id).to.deep.equal(id); + expect(result._id.toString()).to.deep.equal(id.toString()); expect(result.b).to.equal(5); expect(result.hasOwnProperty('a')).to.equal(false); done(); From 0a1788d6bd83df06427c473b84b0102df7c35f61 Mon Sep 17 00:00:00 2001 From: Lucas Hrabovsky Date: Tue, 26 Jul 2016 17:44:19 -0400 Subject: [PATCH 6/6] :necktie: --- test/data-service.test.js | 106 +++++++++++++++++++++------- test/helper.js | 13 +++- test/native-client.test.js | 137 ++++++++++++++++++++++++++++--------- 3 files changed, 197 insertions(+), 59 deletions(-) diff --git a/test/data-service.test.js b/test/data-service.test.js index f9b87771..a2705069 100644 --- a/test/data-service.test.js +++ b/test/data-service.test.js @@ -10,7 +10,9 @@ var DataService = require('../lib/data-service'); describe('DataService', function() { var service = new DataService(helper.connection); - before(require('mongodb-runner/mocha/before')({ port: 27018 })); + before(require('mongodb-runner/mocha/before')({ + port: 27018 + })); after(require('mongodb-runner/mocha/after')()); before(function(done) { @@ -19,11 +21,17 @@ describe('DataService', function() { describe('#deleteOne', function() { it('deletes the document from the collection', function(done) { - service.insertOne('data-service.test', { a: 500 }, {}, function(err) { + service.insertOne('data-service.test', { + a: 500 + }, {}, function(err) { assert.equal(null, err); - service.deleteOne('data-service.test', { a: 500 }, {}, function(er) { + service.deleteOne('data-service.test', { + a: 500 + }, {}, function(er) { assert.equal(null, er); - service.find('data-service.test', { a: 500 }, {}, function(error, docs) { + service.find('data-service.test', { + a: 500 + }, {}, function(error, docs) { assert.equal(null, error); expect(docs.length).to.equal(0); done(); @@ -35,11 +43,17 @@ describe('DataService', function() { describe('#deleteMany', function() { it('deletes the document from the collection', function(done) { - service.insertOne('data-service.test', { a: 500 }, {}, function(err) { + service.insertOne('data-service.test', { + a: 500 + }, {}, function(err) { assert.equal(null, err); - service.deleteMany('data-service.test', { a: 500 }, {}, function(er) { + service.deleteMany('data-service.test', { + a: 500 + }, {}, function(er) { assert.equal(null, er); - service.find('data-service.test', { a: 500 }, {}, function(error, docs) { + service.find('data-service.test', { + a: 500 + }, {}, function(error, docs) { assert.equal(null, error); expect(docs.length).to.equal(0); done(); @@ -63,7 +77,9 @@ describe('DataService', function() { }); it('returns a cursor for the documents', function(done) { - service.find('data-service.test', {}, { skip: 1 }, function(error, docs) { + service.find('data-service.test', {}, { + skip: 1 + }, function(error, docs) { assert.equal(null, error); expect(docs.length).to.equal(1); done(); @@ -81,14 +97,22 @@ describe('DataService', function() { var id = new ObjectId(); it('returns the updated document', function(done) { - service.insertOne('data-service.test', { _id: id, a: 500 }, {}, function(err) { + service.insertOne('data-service.test', { + _id: id, + a: 500 + }, {}, function(err) { assert.equal(null, err); service.findOneAndReplace( 'data-service.test', - { _id: id }, - { b: 5 }, - { returnOriginal: false }, - function(error, result) { + { + _id: id + }, + { + b: 5 + }, + { + returnOriginal: false + }, function(error, result) { expect(error).to.equal(null); expect(result._id.toString()).to.deep.equal(id.toString()); expect(result.b).to.equal(5); @@ -114,7 +138,9 @@ describe('DataService', function() { describe('#count', function() { context('when a filter is provided', function() { it('returns a count for the matching documents', function(done) { - service.count('data-service.test', { a: 1 }, {}, function(error, count) { + service.count('data-service.test', { + a: 1 + }, {}, function(error, count) { assert.equal(null, error); expect(count).to.equal(0); done(); @@ -228,9 +254,13 @@ describe('DataService', function() { }); it('inserts the document into the collection', function(done) { - service.insertOne('data-service.test', { a: 500 }, {}, function(err) { + service.insertOne('data-service.test', { + a: 500 + }, {}, function(err) { assert.equal(null, err); - service.find('data-service.test', { a: 500 }, {}, function(error, docs) { + service.find('data-service.test', { + a: 500 + }, {}, function(error, docs) { assert.equal(null, error); expect(docs.length).to.equal(1); done(); @@ -247,9 +277,15 @@ describe('DataService', function() { }); it('inserts the documents into the collection', function(done) { - service.insertMany('data-service.test', [{ a: 500 }, { a: 500 }], {}, function(err) { + service.insertMany('data-service.test', [{ + a: 500 + }, { + a: 500 + }], {}, function(err) { assert.equal(null, err); - service.find('data-service.test', { a: 500 }, {}, function(error, docs) { + service.find('data-service.test', { + a: 500 + }, {}, function(error, docs) { assert.equal(null, error); expect(docs.length).to.equal(2); done(); @@ -295,11 +331,21 @@ describe('DataService', function() { }); it('updates the document', function(done) { - service.insertOne('data-service.test', { a: 500 }, {}, function(err) { + service.insertOne('data-service.test', { + a: 500 + }, {}, function(err) { assert.equal(null, err); - service.updateOne('data-service.test', { a: 500 }, { '$set': { a: 600 }}, {}, function(er) { + service.updateOne('data-service.test', { + a: 500 + }, { + $set: { + a: 600 + } + }, {}, function(er) { assert.equal(null, er); - service.find('data-service.test', { a: 600 }, {}, function(error, docs) { + service.find('data-service.test', { + a: 600 + }, {}, function(error, docs) { assert.equal(null, error); expect(docs.length).to.equal(1); done(); @@ -317,11 +363,23 @@ describe('DataService', function() { }); it('updates the documents', function(done) { - service.insertMany('data-service.test', [{ a: 500 }, { a: 500 }], {}, function(err) { + service.insertMany('data-service.test', [{ + a: 500 + }, { + a: 500 + }], {}, function(err) { assert.equal(null, err); - service.updateMany('data-service.test', { a: 500 }, { '$set': { a: 600 }}, {}, function(er) { + service.updateMany('data-service.test', { + a: 500 + }, { + $set: { + a: 600 + } + }, {}, function(er) { assert.equal(null, er); - service.find('data-service.test', { a: 600 }, {}, function(error, docs) { + service.find('data-service.test', { + a: 600 + }, {}, function(error, docs) { assert.equal(null, error); expect(docs.length).to.equal(2); done(); diff --git a/test/helper.js b/test/helper.js index 7e17cc51..1b7dafaa 100644 --- a/test/helper.js +++ b/test/helper.js @@ -9,12 +9,19 @@ module.exports.assert = assert; module.exports.expect = expect; module.exports.eventStream = es; -module.exports.connection = - new Connection({ hostname: '127.0.0.1', port: 27018, ns: 'data-service' }); +module.exports.connection = new Connection({ + hostname: '127.0.0.1', + port: 27018, + ns: 'data-service' +}); module.exports.insertTestDocuments = function(client, callback) { var collection = client.database.collection('test'); - collection.insertMany([{ a: 1 }, { a: 2 }], callback); + collection.insertMany([{ + a: 1 + }, { + a: 2 + }], callback); }; module.exports.deleteTestDocuments = function(client, callback) { diff --git a/test/native-client.test.js b/test/native-client.test.js index 14d4f743..296b5e90 100644 --- a/test/native-client.test.js +++ b/test/native-client.test.js @@ -25,8 +25,11 @@ describe('NativeClient', function() { describe('#connect', function() { context('when an invalid connection was provided', function() { - var badConnection = - new Connection({ hostname: '127.0.0.1', port: 27050, ns: 'data-service' }); + var badConnection = new Connection({ + hostname: '127.0.0.1', + port: 27050, + ns: 'data-service' + }); var badClient = new NativeClient(badConnection); // var message = 'MongoDB not running on the provided host and port'; var message = 'Could not connect to MongoDB on the provided host and port'; @@ -60,7 +63,9 @@ describe('NativeClient', function() { context('when a filter is provided', function() { it('returns a cursor for the matching documents', function(done) { - client.find('data-service.test', { a: 1 }, {}, function(error, docs) { + client.find('data-service.test', { + a: 1 + }, {}, function(error, docs) { assert.equal(null, error); expect(docs.length).to.equal(1); done(); @@ -80,7 +85,9 @@ describe('NativeClient', function() { context('when options are provided', function() { it('returns a cursor for the documents', function(done) { - client.find('data-service.test', {}, { skip: 1 }, function(error, docs) { + client.find('data-service.test', {}, { + skip: 1 + }, function(error, docs) { assert.equal(null, error); expect(docs.length).to.equal(1); done(); @@ -190,7 +197,9 @@ describe('NativeClient', function() { describe('#count', function() { context('when a filter is provided', function() { it('returns a count for the matching documents', function(done) { - client.count('data-service.test', { a: 1 }, {}, function(error, count) { + client.count('data-service.test', { + a: 1 + }, {}, function(error, count) { assert.equal(null, error); expect(count).to.equal(0); done(); @@ -202,7 +211,9 @@ describe('NativeClient', function() { describe('#explain', function() { context('when a filter is provided', function() { it('returns an explain object for the provided filter', function(done) { - client.explain('data-service.test', { a: 1 }, {}, function(error, explanation) { + client.explain('data-service.test', { + a: 1 + }, {}, function(error, explanation) { assert.equal(null, error); expect(explanation).to.be.an('object'); done(); @@ -213,11 +224,17 @@ describe('NativeClient', function() { describe('#deleteOne', function() { it('deletes the document from the collection', function(done) { - client.insertOne('data-service.test', { a: 500 }, {}, function(err) { + client.insertOne('data-service.test', { + a: 500 + }, {}, function(err) { assert.equal(null, err); - client.deleteOne('data-service.test', { a: 500 }, {}, function(er) { + client.deleteOne('data-service.test', { + a: 500 + }, {}, function(er) { assert.equal(null, er); - client.find('data-service.test', { a: 500 }, {}, function(error, docs) { + client.find('data-service.test', { + a: 500 + }, {}, function(error, docs) { assert.equal(null, error); expect(docs.length).to.equal(0); done(); @@ -229,11 +246,19 @@ describe('NativeClient', function() { describe('#deleteMany', function() { it('deletes the documents from the collection', function(done) { - client.insertMany('data-service.test', [{ a: 500 }, { a: 500 }], {}, function(err) { + client.insertMany('data-service.test', [{ + a: 500 + }, { + a: 500 + }], {}, function(err) { assert.equal(null, err); - client.deleteMany('data-service.test', { a: 500 }, {}, function(er) { + client.deleteMany('data-service.test', { + a: 500 + }, {}, function(er) { assert.equal(null, er); - client.find('data-service.test', { a: 500 }, {}, function(error, docs) { + client.find('data-service.test', { + a: 500 + }, {}, function(error, docs) { assert.equal(null, error); expect(docs.length).to.equal(0); done(); @@ -254,14 +279,22 @@ describe('NativeClient', function() { var id = new ObjectId(); it('returns the updated document', function(done) { - client.insertOne('data-service.test', { _id: id, a: 500 }, {}, function(err) { + client.insertOne('data-service.test', { + _id: id, + a: 500 + }, {}, function(err) { assert.equal(null, err); client.findOneAndReplace( 'data-service.test', - { _id: id }, - { b: 5 }, - { returnOriginal: false }, - function(error, result) { + { + _id: id + }, + { + b: 5 + }, + { + returnOriginal: false + }, function(error, result) { expect(error).to.equal(null); expect(result._id.toString()).to.deep.equal(id.toString()); expect(result.b).to.equal(5); @@ -277,14 +310,22 @@ describe('NativeClient', function() { var id = new ObjectId(); it('returns the updated document', function(done) { - client.insertOne('data-service.test', { _id: id, a: 500 }, {}, function(err) { + client.insertOne('data-service.test', { + _id: id, + a: 500 + }, {}, function(err) { assert.equal(null, err); client.findOneAndReplace( 'data-service.test', - { _id: id }, - { '$b': 5 }, - { returnOriginal: false }, - function(error) { + { + _id: id + }, + { + $b: 5 + }, + { + returnOriginal: false + }, function(error) { expect(error.message).to.not.equal(null); done(); } @@ -338,9 +379,13 @@ describe('NativeClient', function() { }); it('inserts the document into the collection', function(done) { - client.insertOne('data-service.test', { a: 500 }, {}, function(err) { + client.insertOne('data-service.test', { + a: 500 + }, {}, function(err) { assert.equal(null, err); - client.find('data-service.test', { a: 500 }, {}, function(error, docs) { + client.find('data-service.test', { + a: 500 + }, {}, function(error, docs) { assert.equal(null, error); expect(docs.length).to.equal(1); done(); @@ -357,9 +402,15 @@ describe('NativeClient', function() { }); it('inserts the documents into the collection', function(done) { - client.insertMany('data-service.test', [{ a: 500 }, { a: 500 }], {}, function(err) { + client.insertMany('data-service.test', [{ + a: 500 + }, { + a: 500 + }], {}, function(err) { assert.equal(null, err); - client.find('data-service.test', { a: 500 }, {}, function(error, docs) { + client.find('data-service.test', { + a: 500 + }, {}, function(error, docs) { assert.equal(null, error); expect(docs.length).to.equal(2); done(); @@ -419,11 +470,21 @@ describe('NativeClient', function() { }); it('updates the document', function(done) { - client.insertOne('data-service.test', { a: 500 }, {}, function(err) { + client.insertOne('data-service.test', { + a: 500 + }, {}, function(err) { assert.equal(null, err); - client.updateOne('data-service.test', { a: 500 }, { '$set': { a: 600 }}, {}, function(er) { + client.updateOne('data-service.test', { + a: 500 + }, { + $set: { + a: 600 + } + }, {}, function(er) { assert.equal(null, er); - client.find('data-service.test', { a: 600 }, {}, function(error, docs) { + client.find('data-service.test', { + a: 600 + }, {}, function(error, docs) { assert.equal(null, error); expect(docs.length).to.equal(1); done(); @@ -441,11 +502,23 @@ describe('NativeClient', function() { }); it('updates the documents', function(done) { - client.insertMany('data-service.test', [{ a: 500 }, { a: 500 }], {}, function(err) { + client.insertMany('data-service.test', [{ + a: 500 + }, { + a: 500 + }], {}, function(err) { assert.equal(null, err); - client.updateMany('data-service.test', { a: 500 }, { '$set': { a: 600 }}, {}, function(er) { + client.updateMany('data-service.test', { + a: 500 + }, { + $set: { + a: 600 + } + }, {}, function(er) { assert.equal(null, er); - client.find('data-service.test', { a: 600 }, {}, function(error, docs) { + client.find('data-service.test', { + a: 600 + }, {}, function(error, docs) { assert.equal(null, error); expect(docs.length).to.equal(2); done();