Skip to content

Commit

Permalink
feat(listDatabases): add support for nameOnly option to listDatabases
Browse files Browse the repository at this point in the history
  • Loading branch information
kmahar authored and mbroadst committed Sep 7, 2017
1 parent fc9bea4 commit eb79b5a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
24 changes: 19 additions & 5 deletions lib/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

var toError = require('./utils').toError,
Define = require('./metadata'),
shallowClone = require('./utils').shallowClone;
shallowClone = require('./utils').shallowClone,
assign = require('./utils').assign;

/**
* @fileOverview The **Admin** class is an internal class that allows convenient access to
Expand Down Expand Up @@ -358,18 +359,31 @@ define.classMethod('validateCollection', { callback: true, promise: true });
/**
* List the available databases
*
* @param {object} [options=null] Optional settings.
* @param {boolean} [options.nameOnly=false] Whether the command should return only db names, or names and size info.
* @param {Admin~resultCallback} [callback] The command result callback.
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.listDatabases = function(callback) {
Admin.prototype.listDatabases = function(options, callback) {
var self = this;

var cmd = { listDatabases: 1 };

// no options were passed in, only a callback
if (typeof options == 'function') return self.s.db.executeDbAdminCommand(cmd, {}, options);

// if we did get options, use them
cmd = assign(cmd, options);

// cast boolean to 1 or 0
if (cmd.nameOnly) cmd.nameOnly = Number(cmd.nameOnly);

// Execute using callback
if (typeof callback == 'function')
return self.s.db.executeDbAdminCommand({ listDatabases: 1 }, {}, callback);
if (typeof callback == 'function') return self.s.db.executeDbAdminCommand(cmd, {}, callback);

// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
self.s.db.executeDbAdminCommand({ listDatabases: 1 }, {}, function(err, r) {
self.s.db.executeDbAdminCommand(cmd, {}, function(err, r) {
if (err) return reject(err);
resolve(r);
});
Expand Down
51 changes: 49 additions & 2 deletions test/functional/operation_example_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var test = require('./shared').assert;
var setupDatabase = require('./shared').setupDatabase;
var f = require('util').format;
var expect = require('chai').expect;

describe('Operation Examples', function() {
before(function() {
Expand Down Expand Up @@ -4822,7 +4823,7 @@ describe('Operation Examples', function() {
* @example-method dropDatabase
* @ignore
*/
it('shouldCorrectlyDropTheDatabase', {
it('should correctly drop the database', {
metadata: { requires: { topology: ['single'] } },

// The actual test we wish to run
Expand Down Expand Up @@ -5436,7 +5437,7 @@ describe('Operation Examples', function() {
* @example-method listDatabases
* @ignore
*/
it('shouldCorrectlyListAllAvailableDatabases', {
it('should correctly list all available databases', {
metadata: {
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
},
Expand Down Expand Up @@ -5472,6 +5473,52 @@ describe('Operation Examples', function() {
}
});

it('should correctly list all available databases names and no database sizes', {
metadata: {
requires: {
topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'],
mongodb: '>=3.2.13'
}
},

// The actual test we wish to run
test: function(done) {
var configuration = this.configuration;
var client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });
client.connect(function(err, client) {
// LINE var MongoClient = require('mongodb').MongoClient,
// LINE test = require('assert');
// LINE MongoClient.connect('mongodb://localhost:27017/test', function(err, client) {
// LINE var db = client.db('test);
// REPLACE configuration.writeConcernMax() WITH {w:1}
// REMOVE-LINE restartAndDone
// REMOVE-LINE done();
// REMOVE-LINE var db = client.db(configuration.db);
// BEGIN
var db = client.db(configuration.db);
// Use the admin database for the operation
var adminDb = db.admin();

// List all the available databases
adminDb.listDatabases({ nameOnly: 1 }, function(err, dbs) {
expect(err).to.not.exist;
expect(dbs.databases).to.include.deep.members([
{
name: 'admin'
},
{
name: 'local'
}
]);

client.close();
done();
});
});
// END
}
});

/**
* Retrieve the current server Info
*
Expand Down

0 comments on commit eb79b5a

Please sign in to comment.