Skip to content

Commit 58485c9

Browse files
listCollections
1 parent 95289a6 commit 58485c9

File tree

2 files changed

+51
-31
lines changed

2 files changed

+51
-31
lines changed

src/operations/list_collections.ts

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1+
import { type Connection } from '..';
12
import type { Binary, Document } from '../bson';
2-
import { CursorResponse } from '../cmap/wire_protocol/responses';
3+
import { CursorResponse, ExplainedCursorResponse } from '../cmap/wire_protocol/responses';
34
import { type CursorTimeoutContext, type CursorTimeoutMode } from '../cursor/abstract_cursor';
45
import type { Db } from '../db';
56
import { type Abortable } from '../mongo_types';
6-
import type { Server } from '../sdam/server';
7-
import type { ClientSession } from '../sessions';
8-
import { type TimeoutContext } from '../timeout';
97
import { maxWireVersion } from '../utils';
10-
import { CommandOperation, type CommandOperationOptions } from './command';
8+
import { type CommandOperationOptions, ModernizedCommandOperation } from './command';
119
import { Aspect, defineAspects } from './operation';
1210

1311
/** @public */
@@ -28,7 +26,8 @@ export interface ListCollectionsOptions
2826
}
2927

3028
/** @internal */
31-
export class ListCollectionsOperation extends CommandOperation<CursorResponse> {
29+
export class ListCollectionsOperation extends ModernizedCommandOperation<CursorResponse> {
30+
override SERVER_COMMAND_RESPONSE_TYPE = CursorResponse;
3231
/**
3332
* @remarks WriteConcern can still be present on the options because
3433
* we inherit options from the client/db/collection. The
@@ -56,28 +55,15 @@ export class ListCollectionsOperation extends CommandOperation<CursorResponse> {
5655
if (typeof this.options.batchSize === 'number') {
5756
this.batchSize = this.options.batchSize;
5857
}
58+
59+
this.SERVER_COMMAND_RESPONSE_TYPE = this.explain ? ExplainedCursorResponse : CursorResponse;
5960
}
6061

6162
override get commandName() {
6263
return 'listCollections' as const;
6364
}
6465

65-
override async execute(
66-
server: Server,
67-
session: ClientSession | undefined,
68-
timeoutContext: TimeoutContext
69-
): Promise<CursorResponse> {
70-
return await super.executeCommand(
71-
server,
72-
session,
73-
this.generateCommand(maxWireVersion(server)),
74-
timeoutContext,
75-
CursorResponse
76-
);
77-
}
78-
79-
/* This is here for the purpose of unit testing the final command that gets sent. */
80-
generateCommand(wireVersion: number): Document {
66+
override buildCommandDocument(connection: Connection): Document {
8167
const command: Document = {
8268
listCollections: 1,
8369
filter: this.filter,
@@ -88,12 +74,18 @@ export class ListCollectionsOperation extends CommandOperation<CursorResponse> {
8874

8975
// we check for undefined specifically here to allow falsy values
9076
// eslint-disable-next-line no-restricted-syntax
91-
if (wireVersion >= 9 && this.options.comment !== undefined) {
77+
if (maxWireVersion(connection) >= 9 && this.options.comment !== undefined) {
9278
command.comment = this.options.comment;
9379
}
9480

9581
return command;
9682
}
83+
84+
override handleOk(
85+
response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
86+
): CursorResponse {
87+
return response;
88+
}
9789
}
9890

9991
/** @public */

test/unit/operations/list_collections.test.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const { expect } = require('chai');
4-
const { ListCollectionsOperation } = require('../../mongodb');
4+
const { ListCollectionsOperation, StreamDescription } = require('../../mongodb');
55

66
describe('ListCollectionsOperation', function () {
77
const db = 'test';
@@ -65,6 +65,8 @@ describe('ListCollectionsOperation', function () {
6565
});
6666

6767
describe('#generateCommand', function () {
68+
const description = new StreamDescription();
69+
6870
context('when comment is provided', function () {
6971
context('when the wireVersion < 9', function () {
7072
it('does not set a comment on the command', function () {
@@ -73,7 +75,10 @@ describe('ListCollectionsOperation', function () {
7375
{},
7476
{ dbName: db, comment: 'test comment' }
7577
);
76-
const command = operation.generateCommand(8);
78+
description.maxWireVersion = 8;
79+
const command = operation.buildCommandDocument({
80+
description
81+
});
7782
expect(command).not.to.haveOwnProperty('comment');
7883
});
7984
});
@@ -85,7 +90,10 @@ describe('ListCollectionsOperation', function () {
8590
{},
8691
{ dbName: db, comment: 'test comment' }
8792
);
88-
const command = operation.generateCommand(9);
93+
description.maxWireVersion = 9;
94+
const command = operation.buildCommandDocument({
95+
description
96+
});
8997
expect(command).to.have.property('comment').that.equals('test comment');
9098
});
9199
});
@@ -95,7 +103,11 @@ describe('ListCollectionsOperation', function () {
95103
const operation = new ListCollectionsOperation(db, {}, { nameOnly: true, dbName: db });
96104

97105
it('sets nameOnly to true', function () {
98-
expect(operation.generateCommand(8)).to.deep.equal({
106+
description.maxWireVersion = 8;
107+
const command = operation.buildCommandDocument({
108+
description
109+
});
110+
expect(command).to.deep.equal({
99111
listCollections: 1,
100112
cursor: {},
101113
filter: {},
@@ -109,7 +121,11 @@ describe('ListCollectionsOperation', function () {
109121
const operation = new ListCollectionsOperation(db, {}, { nameOnly: false, dbName: db });
110122

111123
it('sets nameOnly to false', function () {
112-
expect(operation.generateCommand(8)).to.deep.equal({
124+
description.maxWireVersion = 8;
125+
const command = operation.buildCommandDocument({
126+
description
127+
});
128+
expect(command).to.deep.equal({
113129
listCollections: 1,
114130
cursor: {},
115131
filter: {},
@@ -129,7 +145,11 @@ describe('ListCollectionsOperation', function () {
129145
);
130146

131147
it('sets authorizedCollections to true', function () {
132-
expect(operation.generateCommand(8)).to.deep.equal({
148+
description.maxWireVersion = 8;
149+
const command = operation.buildCommandDocument({
150+
description
151+
});
152+
expect(command).to.deep.equal({
133153
listCollections: 1,
134154
cursor: {},
135155
filter: {},
@@ -147,7 +167,11 @@ describe('ListCollectionsOperation', function () {
147167
);
148168

149169
it('sets authorizedCollections to false', function () {
150-
expect(operation.generateCommand(8)).to.deep.equal({
170+
description.maxWireVersion = 8;
171+
const command = operation.buildCommandDocument({
172+
description
173+
});
174+
expect(command).to.deep.equal({
151175
listCollections: 1,
152176
cursor: {},
153177
filter: {},
@@ -162,7 +186,11 @@ describe('ListCollectionsOperation', function () {
162186
const operation = new ListCollectionsOperation(db, {}, { dbName: db });
163187

164188
it('sets nameOnly and authorizedCollections properties to false', function () {
165-
expect(operation.generateCommand(8)).to.deep.equal({
189+
description.maxWireVersion = 8;
190+
const command = operation.buildCommandDocument({
191+
description
192+
});
193+
expect(command).to.deep.equal({
166194
listCollections: 1,
167195
cursor: {},
168196
filter: {},

0 commit comments

Comments
 (0)