From eb79b5a5850fb55c27a194e1dadc939c57596bd1 Mon Sep 17 00:00:00 2001 From: Kaitlin Mahar Date: Wed, 6 Sep 2017 14:02:18 -0400 Subject: [PATCH] feat(listDatabases): add support for nameOnly option to listDatabases NODE-1129 --- lib/admin.js | 24 +++++++--- test/functional/operation_example_tests.js | 51 +++++++++++++++++++++- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/lib/admin.js b/lib/admin.js index 497f867bff..8cf06c0783 100644 --- a/lib/admin.js +++ b/lib/admin.js @@ -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 @@ -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); }); diff --git a/test/functional/operation_example_tests.js b/test/functional/operation_example_tests.js index b277345098..d0e7eac768 100644 --- a/test/functional/operation_example_tests.js +++ b/test/functional/operation_example_tests.js @@ -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() { @@ -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 @@ -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'] } }, @@ -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 *