Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/operations/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export abstract class CommandOperation<T> extends AbstractOperation<T> {
this.ns = new MongoDBNamespace(dbNameOverride, '$cmd');
} else {
this.ns = parent
? parent.s.namespace.withCollection('$cmd')
? parent.s?.namespace.withCollection('$cmd')
: new MongoDBNamespace('admin', '$cmd');
}

Expand Down
6 changes: 2 additions & 4 deletions src/operations/list_databases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class ListDatabasesOperation extends CommandOperation<ListDatabasesResult
constructor(db: Db, options?: ListDatabasesOptions) {
super(db, options);
this.options = options ?? {};
this.options.nameOnly = !!this.options.nameOnly;
this.ns = new MongoDBNamespace('admin', '$cmd');
}

Expand All @@ -39,10 +40,7 @@ export class ListDatabasesOperation extends CommandOperation<ListDatabasesResult
session: ClientSession | undefined,
callback: Callback<ListDatabasesResult>
): void {
const cmd: Document = { listDatabases: 1 };
if (this.options.nameOnly) {
cmd.nameOnly = Number(cmd.nameOnly);
}
const cmd: Document = { listDatabases: 1, nameOnly: this.options.nameOnly };

if (this.options.filter) {
cmd.filter = this.options.filter;
Expand Down
36 changes: 36 additions & 0 deletions test/unit/operations/list_databases.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const { expect } = require('chai');

const { ListDatabasesOperation } = require('../../mongodb');

describe('ListDatabasesOperation', function () {
const db = 'test';

describe('#constructor', function () {
context('when nameOnly is provided', function () {
context('when nameOnly is true', function () {
const operation = new ListDatabasesOperation(db, { nameOnly: true });
it('sets nameOnly to true', function () {
expect(operation.options).to.have.property('nameOnly', true);
});
});

context('when nameOnly is false', function () {
const operation = new ListDatabasesOperation({}, { nameOnly: false });

it('sets nameOnly to false', function () {
expect(operation.options).to.have.property('nameOnly', false);
});
});
});

context('when no options are provided', function () {
const operation = new ListDatabasesOperation(db, {});

it('sets nameOnly to false', function () {
expect(operation.options).to.have.property('nameOnly', false);
});
});
});
});