Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.

WIP [do not review]: update connection model for async #299

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"node": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"rules": {
"strict": "off",
"no-undef": "error",
Expand Down
19 changes: 9 additions & 10 deletions lib/data-service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const EventEmitter = require('events');
const debug = require('debug')('mongodb-data-service:data-service');

/**
* Instantiate a new DataService object.
Expand Down Expand Up @@ -31,6 +32,7 @@ class DataService extends EventEmitter {
.on('topologyOpening', (evt) => this.emit('topologyOpening', evt))
.on('topologyClosed', (evt) => this.emit('topologyClosed', evt))
.on('topologyDescriptionChanged', (evt) => {
debug('got new topologyDescriptionChanged', evt.newDescription);
this.lastSeenTopology = evt.newDescription;
this.emit('topologyDescriptionChanged', evt);
});
Expand Down Expand Up @@ -151,17 +153,13 @@ class DataService extends EventEmitter {

/**
* Connect to the server.
*
* @param {function} done - The callback function.
*/
connect(done) {
this.client.connect((err) => {
if (err) {
return done(err);
}
done(null, this);
this.emit('readable');
});
async connect() {
await this.client.connect();

this.emit('readable');

return this;
}

/**
Expand All @@ -180,6 +178,7 @@ class DataService extends EventEmitter {
* and options.
*
* @param {string} ns - The namespace to search on.
* @param {object} filter - The filter for the count.
* @param {object} options - The query options.
* @param {function} callback - The callback function.
*/
Expand Down
42 changes: 21 additions & 21 deletions lib/native-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,39 +98,37 @@ class NativeClient extends EventEmitter {
/**
* Connect to the server.
*
* @param {function} done - The callback function.
* @return {NativeClient}
*/
connect(done) {
async connect() {
debug('connecting...');

this.connectionOptions = null;
this.isWritable = false;
this.isMongos = false;

connect(
const [
// eslint-disable-next-line no-unused-vars
_, // The client is set in the `setupListeners` method.
connectionOptions
] = await connect(
this.model,
this.setupListeners.bind(this),
(err, _client, connectionOptions) => {
if (err) {
return done(this._translateMessage(err));
}
this.setupListeners.bind(this)
);

this.connectionOptions = connectionOptions;
this.connectionOptions = connectionOptions;

this.isWritable = this.client.isWritable;
this.isMongos = this.client.isMongos;
this.isWritable = this.client.isWritable;
this.isMongos = this.client.isMongos;

debug('connected!', {
isWritable: this.isWritable,
isMongos: this.isMongos
});
debug('connected!', {
isWritable: this.isWritable,
isMongos: this.isMongos
});

this.client.on('status', (evt) => this.emit('status', evt));
this.database = this.client.db(this.model.ns || ADMIN);

this.client.on('status', (evt) => this.emit('status', evt));
this.database = this.client.db(this.model.ns || ADMIN);
done(null, this);
}
);
return this;
}

Expand Down Expand Up @@ -301,7 +299,8 @@ class NativeClient extends EventEmitter {
*/
collections(databaseName, callback) {
if (databaseName === SYSTEM) {
return callback(null, []);
callback(null, []);
return;
}
this.collectionNames(databaseName, (error, names) => {
if (error) {
Expand Down Expand Up @@ -521,6 +520,7 @@ class NativeClient extends EventEmitter {

/**
* Disconnect the client.
* @param {Function} callback - Callback called after connection closed.
*/
disconnect(callback) {
this.client.close(true, callback);
Expand Down
4 changes: 2 additions & 2 deletions test/data-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ describe('DataService', function() {
this.timeout(20000);
const service = new DataService(helper.connection);

before(function(done) {
service.connect(done);
before(async() => {
await service.connect();
});
after(function(done) {
service.disconnect(done);
Expand Down
94 changes: 53 additions & 41 deletions test/instance-detail-helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,72 @@ const connect = Connection.connect;
const { getInstance } = require('../lib/instance-detail-helper');
const helper = require('./helper');
const DataService = require('../lib/data-service');
const { promisify } = require('util');
const _ = require('lodash');

describe('mongodb-data-service#instance', function() {
describe('local', function() {
let client;
let db;
after(function(done) {
client.close(true, done);
});
it('should connect to `localhost:27018`', function(done) {
Connection.from(
'mongodb://localhost:27018/data-service',
function(error, model) {
assert.equal(error, null);
connect(
model,
null,
function(err, _client) {
if (err) {
return done(err);
}
client = _client;
db = client.db('data-service');
done();
}
);
}
let model;

before(async() => {
model = await Connection.from(
'mongodb://localhost:27018/data-service'
);
});
it('should not close the db after getting instance details', function(done) {
assert(db);
getInstance(client, db, function(err) {
if (err) {
return done(err);
}
db.admin().ping(function(_err, pingResult) {
if (_err) {
done(_err);

describe('connecting', () => {
after(function(done) {
client.close(true, done);
});
it('should connect to `localhost:27018`', async() => {
const [ _client ] = await connect(
model,
null
);
client = _client;
});
});

describe('after connecting', () => {
before(async() => {
const [ _client ] = await connect(
model,
null
);
client = _client;
});
after(function(done) {
client.close(true, done);
});

it('should not close the db after getting instance details', (done) => {
const db = client.db('data-service');

assert(db);
getInstance(client, db, function(err) {
if (err) {
return done(err);
}
done(null, pingResult);
db.admin().ping(function(_err, pingResult) {
if (_err) {
done(_err);
}
done(null, pingResult);
});
});
});
});

if (process.env.MONGODB_TOPOLOGY !== 'cluster') {
describe('views', function() {
var service = new DataService(helper.connection);
var instanceDetails = null;
before(function(done) {
service.connect(function(err) {
if (err) return done(err);
helper.insertTestDocuments(service.client, function() {
done();
});
});
const service = new DataService(helper.connection);
let instanceDetails;
before(async() => {
await service.connect();

const runInsertDocuments = promisify(helper.insertTestDocuments);
await runInsertDocuments(service.client);
});

after(function(done) {
Expand Down
Loading