Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
98 changes: 46 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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
47 changes: 9 additions & 38 deletions lib/data-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,9 @@

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');

/**
* Constants for generated events.
*/
const Events = {
Connecting: 'DataService:Connecting',
Readable: 'DataService:Readable',
Error: 'DataService:Error'
};

/**
* Instantiate a new DataService object.
*
Expand All @@ -31,21 +21,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);
});
});
}

/**
Expand All @@ -63,21 +40,16 @@ 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) => {
if (tunnelError) {
return callback(tunnelError, this);
connect(done) {
debug('Connecting...');
this.client.connect((err) => {
if (err) {
return done(err);
}
this.client.connect((error) => {
debug('Data Service is readable.');
process.nextTick(() => {
this.emit(Events.Readable);
});
return callback(error, this);
});
done(null, this);
this.emit('readable');
});
}

Expand Down Expand Up @@ -337,4 +309,3 @@ class DataService extends EventEmitter {
}

module.exports = DataService;
module.exports.Events = Events;
23 changes: 17 additions & 6 deletions lib/native-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

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');
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.
*/
class NativeClient {
class NativeClient extends EventEmitter {

/**
* Instantiate a new NativeClient object.
Expand All @@ -21,19 +23,28 @@ class NativeClient {
* @param {Connection} model - The Connection model.
*/
constructor(model) {
super();
this.model = model;
}

/**
* Connect to the server.
*
* @param {function} callback - The callback function.
* @param {function} done - The callback function.
* @return {NativeClient}
*/
connect(callback) {
createConnection(this.model, (error, database) => {
connect(done) {
debug('connecting...');
this.client = connect(this.model, (err, database) => {
if (err) {
return done(this._translateMessage(err));
}
debug('connected!');
this.database = database;
callback(this._translateMessage(error), this);
done(null, this);
});
this.client.on('status', (evt) => this.emit('status', evt));
return this;
}

/**
Expand Down
118 changes: 0 additions & 118 deletions lib/ssh-tunnel-connector.js

This file was deleted.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@
"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",
"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",
Expand Down
Loading