diff --git a/src/collection.ts b/src/collection.ts index 5ef6ed8f2e5..ef3e79f8c2f 100644 --- a/src/collection.ts +++ b/src/collection.ts @@ -16,7 +16,7 @@ import { type ListSearchIndexesOptions } from './cursor/list_search_indexes_cursor'; import type { Db } from './db'; -import { MongoInvalidArgumentError, MongoOperationTimeoutError } from './error'; +import { MongoAPIError, MongoInvalidArgumentError, MongoOperationTimeoutError } from './error'; import type { MongoClient, PkFactory } from './mongo_client'; import type { Abortable, @@ -70,9 +70,7 @@ import { type InsertOneOptions, type InsertOneResult } from './operations/insert'; -import { IsCappedOperation } from './operations/is_capped'; import type { Hint, OperationOptions } from './operations/operation'; -import { OptionsOperation } from './operations/options_operation'; import { RenameOperation, type RenameOptions } from './operations/rename'; import { CreateSearchIndexesOperation, @@ -591,10 +589,16 @@ export class Collection { * @param options - Optional settings for the command */ async options(options?: OperationOptions): Promise { - return await executeOperation( - this.client, - new OptionsOperation(this as TODO_NODE_3286, resolveOptions(this, options)) - ); + options = resolveOptions(this, options); + const [collection] = await this.s.db + .listCollections({ name: this.collectionName }, { ...options, nameOnly: false }) + .toArray(); + + if (collection == null || collection.options == null) { + throw new MongoAPIError(`collection ${this.namespace} not found`); + } + + return collection.options; } /** @@ -603,10 +607,8 @@ export class Collection { * @param options - Optional settings for the command */ async isCapped(options?: OperationOptions): Promise { - return await executeOperation( - this.client, - new IsCappedOperation(this as TODO_NODE_3286, resolveOptions(this, options)) - ); + const { capped } = await this.options(options); + return Boolean(capped); } /** diff --git a/src/db.ts b/src/db.ts index bcadc4937e7..c75293f44af 100644 --- a/src/db.ts +++ b/src/db.ts @@ -10,7 +10,6 @@ import { MongoInvalidArgumentError } from './error'; import type { MongoClient, PkFactory } from './mongo_client'; import type { Abortable, TODO_NODE_3286 } from './mongo_types'; import type { AggregateOptions } from './operations/aggregate'; -import { CollectionsOperation } from './operations/collections'; import { CreateCollectionOperation, type CreateCollectionOptions @@ -435,10 +434,15 @@ export class Db { * @param options - Optional settings for the command */ async collections(options?: ListCollectionsOptions): Promise { - return await executeOperation( - this.client, - new CollectionsOperation(this, resolveOptions(this, options)) - ); + options = resolveOptions(this, options); + const collections = await this.listCollections({}, { ...options, nameOnly: true }).toArray(); + + return collections + .filter( + // Filter collections removing any illegal ones + ({ name }) => !name.includes('$') + ) + .map(({ name }) => new Collection(this, name, this.s.options)); } /** diff --git a/src/operations/collections.ts b/src/operations/collections.ts deleted file mode 100644 index 5ed96296803..00000000000 --- a/src/operations/collections.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { Collection } from '../collection'; -import type { Db } from '../db'; -import type { Server } from '../sdam/server'; -import type { ClientSession } from '../sessions'; -import { AbstractOperation, type OperationOptions } from './operation'; - -export interface CollectionsOptions extends OperationOptions { - nameOnly?: boolean; -} - -/** @internal */ -export class CollectionsOperation extends AbstractOperation { - override options: CollectionsOptions; - db: Db; - - constructor(db: Db, options: CollectionsOptions) { - super(options); - this.options = options; - this.db = db; - } - - override get commandName() { - return 'listCollections' as const; - } - - override async execute( - server: Server, - session: ClientSession | undefined - ): Promise { - // Let's get the collection names - const documents = await this.db - .listCollections( - {}, - { ...this.options, nameOnly: true, readPreference: this.readPreference, session } - ) - .toArray(); - const collections: Collection[] = []; - for (const { name } of documents) { - if (!name.includes('$')) { - // Filter collections removing any illegal ones - collections.push(new Collection(this.db, name, this.db.s.options)); - } - } - // Return the collection objects - return collections; - } -} diff --git a/src/operations/is_capped.ts b/src/operations/is_capped.ts deleted file mode 100644 index db52ec3bb31..00000000000 --- a/src/operations/is_capped.ts +++ /dev/null @@ -1,35 +0,0 @@ -import type { Collection } from '../collection'; -import { MongoAPIError } from '../error'; -import type { Server } from '../sdam/server'; -import type { ClientSession } from '../sessions'; -import { AbstractOperation, type OperationOptions } from './operation'; - -/** @internal */ -export class IsCappedOperation extends AbstractOperation { - override options: OperationOptions; - collection: Collection; - - constructor(collection: Collection, options: OperationOptions) { - super(options); - this.options = options; - this.collection = collection; - } - - override get commandName() { - return 'listCollections' as const; - } - - override async execute(server: Server, session: ClientSession | undefined): Promise { - const coll = this.collection; - const [collection] = await coll.s.db - .listCollections( - { name: coll.collectionName }, - { ...this.options, nameOnly: false, readPreference: this.readPreference, session } - ) - .toArray(); - if (collection == null || collection.options == null) { - throw new MongoAPIError(`collection ${coll.namespace} not found`); - } - return !!collection.options?.capped; - } -} diff --git a/src/operations/options_operation.ts b/src/operations/options_operation.ts deleted file mode 100644 index c480fe077d2..00000000000 --- a/src/operations/options_operation.ts +++ /dev/null @@ -1,35 +0,0 @@ -import type { Document } from '../bson'; -import type { Collection } from '../collection'; -import { MongoAPIError } from '../error'; -import type { Server } from '../sdam/server'; -import type { ClientSession } from '../sessions'; -import { AbstractOperation, type OperationOptions } from './operation'; - -/** @internal */ -export class OptionsOperation extends AbstractOperation { - override options: OperationOptions; - collection: Collection; - - constructor(collection: Collection, options: OperationOptions) { - super(options); - this.options = options; - this.collection = collection; - } - override get commandName() { - return 'listCollections' as const; - } - - override async execute(server: Server, session: ClientSession | undefined): Promise { - const coll = this.collection; - const [collection] = await coll.s.db - .listCollections( - { name: coll.collectionName }, - { ...this.options, nameOnly: false, readPreference: this.readPreference, session } - ) - .toArray(); - if (collection == null || collection.options == null) { - throw new MongoAPIError(`collection ${coll.namespace} not found`); - } - return collection.options; - } -} diff --git a/test/integration/crud/abstract_operation.test.ts b/test/integration/crud/abstract_operation.test.ts index 0c26dd01cc0..052286a3ea7 100644 --- a/test/integration/crud/abstract_operation.test.ts +++ b/test/integration/crud/abstract_operation.test.ts @@ -37,11 +37,6 @@ describe('abstract operation', function () { subclassType: mongodb.AggregateOperation, correctCommandName: 'aggregate' }, - { - subclassCreator: () => new mongodb.CollectionsOperation(db, {}), - subclassType: mongodb.CollectionsOperation, - correctCommandName: 'listCollections' - }, { subclassCreator: () => new mongodb.CountOperation(collection.fullNamespace, { a: 1 }, {}), subclassType: mongodb.CountOperation, @@ -155,11 +150,6 @@ describe('abstract operation', function () { subclassType: mongodb.InsertOneOperation, correctCommandName: 'insert' }, - { - subclassCreator: () => new mongodb.IsCappedOperation(collection, {}), - subclassType: mongodb.IsCappedOperation, - correctCommandName: 'listCollections' - }, { subclassCreator: () => new mongodb.KillCursorsOperation( @@ -181,11 +171,6 @@ describe('abstract operation', function () { subclassType: mongodb.ListDatabasesOperation, correctCommandName: 'listDatabases' }, - { - subclassCreator: () => new mongodb.OptionsOperation(collection, {}), - subclassType: mongodb.OptionsOperation, - correctCommandName: 'listCollections' - }, { subclassCreator: () => new mongodb.ProfilingLevelOperation(db, {}), subclassType: mongodb.ProfilingLevelOperation, diff --git a/test/mongodb.ts b/test/mongodb.ts index e9d019d0b12..9c385b21c2b 100644 --- a/test/mongodb.ts +++ b/test/mongodb.ts @@ -166,7 +166,6 @@ export * from '../src/operations/aggregate'; export * from '../src/operations/client_bulk_write/command_builder'; export * from '../src/operations/client_bulk_write/common'; export * from '../src/operations/client_bulk_write/results_merger'; -export * from '../src/operations/collections'; export * from '../src/operations/command'; export * from '../src/operations/count'; export * from '../src/operations/create_collection'; @@ -180,12 +179,10 @@ export * from '../src/operations/find_and_modify'; export * from '../src/operations/get_more'; export * from '../src/operations/indexes'; export * from '../src/operations/insert'; -export * from '../src/operations/is_capped'; export * from '../src/operations/kill_cursors'; export * from '../src/operations/list_collections'; export * from '../src/operations/list_databases'; export * from '../src/operations/operation'; -export * from '../src/operations/options_operation'; export * from '../src/operations/profiling_level'; export * from '../src/operations/remove_user'; export * from '../src/operations/rename';