Skip to content

Commit 5636ca1

Browse files
working
1 parent 2ef6c10 commit 5636ca1

File tree

7 files changed

+26
-155
lines changed

7 files changed

+26
-155
lines changed

src/collection.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
type ListSearchIndexesOptions
1212
} from './cursor/list_search_indexes_cursor';
1313
import type { Db } from './db';
14-
import { MongoInvalidArgumentError, MongoOperationTimeoutError } from './error';
14+
import { MongoAPIError, MongoInvalidArgumentError, MongoOperationTimeoutError } from './error';
1515
import type { MongoClient, PkFactory } from './mongo_client';
1616
import type {
1717
Abortable,
@@ -67,9 +67,7 @@ import {
6767
type InsertOneOptions,
6868
type InsertOneResult
6969
} from './operations/insert';
70-
import { IsCappedOperation } from './operations/is_capped';
7170
import type { Hint, OperationOptions } from './operations/operation';
72-
import { OptionsOperation } from './operations/options_operation';
7371
import { RenameOperation, type RenameOptions } from './operations/rename';
7472
import {
7573
CreateSearchIndexesOperation,
@@ -557,10 +555,16 @@ export class Collection<TSchema extends Document = Document> {
557555
* @param options - Optional settings for the command
558556
*/
559557
async options(options?: OperationOptions): Promise<Document> {
560-
return await executeOperation(
561-
this.client,
562-
new OptionsOperation(this as TODO_NODE_3286, resolveOptions(this, options))
563-
);
558+
options = resolveOptions(this, options);
559+
const [collection] = await this.s.db
560+
.listCollections({ name: this.collectionName }, { ...options, nameOnly: false })
561+
.toArray();
562+
563+
if (collection == null || collection.options == null) {
564+
throw new MongoAPIError(`collection ${this.namespace} not found`);
565+
}
566+
567+
return collection.options;
564568
}
565569

566570
/**
@@ -569,10 +573,8 @@ export class Collection<TSchema extends Document = Document> {
569573
* @param options - Optional settings for the command
570574
*/
571575
async isCapped(options?: OperationOptions): Promise<boolean> {
572-
return await executeOperation(
573-
this.client,
574-
new IsCappedOperation(this as TODO_NODE_3286, resolveOptions(this, options))
575-
);
576+
const collectionOptions = await this.options(options);
577+
return Boolean(collectionOptions?.isCapped);
576578
}
577579

578580
/**

src/db.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { MongoInvalidArgumentError } from './error';
1010
import type { MongoClient, PkFactory } from './mongo_client';
1111
import type { Abortable, TODO_NODE_3286 } from './mongo_types';
1212
import type { AggregateOptions } from './operations/aggregate';
13-
import { CollectionsOperation } from './operations/collections';
1413
import {
1514
CreateCollectionOperation,
1615
type CreateCollectionOptions
@@ -360,13 +359,13 @@ export class Db {
360359
): ListCollectionsCursor<CollectionInfo>;
361360
listCollections<
362361
T extends Pick<CollectionInfo, 'name' | 'type'> | CollectionInfo =
363-
| Pick<CollectionInfo, 'name' | 'type'>
364-
| CollectionInfo
362+
| Pick<CollectionInfo, 'name' | 'type'>
363+
| CollectionInfo
365364
>(filter?: Document, options?: ListCollectionsOptions & Abortable): ListCollectionsCursor<T>;
366365
listCollections<
367366
T extends Pick<CollectionInfo, 'name' | 'type'> | CollectionInfo =
368-
| Pick<CollectionInfo, 'name' | 'type'>
369-
| CollectionInfo
367+
| Pick<CollectionInfo, 'name' | 'type'>
368+
| CollectionInfo
370369
>(
371370
filter: Document = {},
372371
options: ListCollectionsOptions & Abortable = {}
@@ -435,10 +434,15 @@ export class Db {
435434
* @param options - Optional settings for the command
436435
*/
437436
async collections(options?: ListCollectionsOptions): Promise<Collection[]> {
438-
return await executeOperation(
439-
this.client,
440-
new CollectionsOperation(this, resolveOptions(this, options))
441-
);
437+
options = resolveOptions(this, options);
438+
const documents = await this.listCollections({}, { ...options, nameOnly: true }).toArray();
439+
440+
return documents
441+
.filter(
442+
// Filter collections removing any illegal ones
443+
({ name }) => !name.includes('$')
444+
)
445+
.map(({ name }) => new Collection(this, name, this.s.options));
442446
}
443447

444448
/**

src/operations/collections.ts

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/operations/is_capped.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/operations/options_operation.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

test/integration/crud/abstract_operation.test.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ describe('abstract operation', function () {
4343
subclassType: mongodb.BulkWriteOperation,
4444
correctCommandName: 'bulkWrite'
4545
},
46-
{
47-
subclassCreator: () => new mongodb.CollectionsOperation(db, {}),
48-
subclassType: mongodb.CollectionsOperation,
49-
correctCommandName: 'listCollections'
50-
},
5146
{
5247
subclassCreator: () => new mongodb.CountOperation(collection.fullNamespace, { a: 1 }, {}),
5348
subclassType: mongodb.CountOperation,
@@ -166,11 +161,6 @@ describe('abstract operation', function () {
166161
subclassType: mongodb.InsertManyOperation,
167162
correctCommandName: 'insert'
168163
},
169-
{
170-
subclassCreator: () => new mongodb.IsCappedOperation(collection, {}),
171-
subclassType: mongodb.IsCappedOperation,
172-
correctCommandName: 'listCollections'
173-
},
174164
{
175165
subclassCreator: () =>
176166
new mongodb.KillCursorsOperation(
@@ -192,11 +182,6 @@ describe('abstract operation', function () {
192182
subclassType: mongodb.ListDatabasesOperation,
193183
correctCommandName: 'listDatabases'
194184
},
195-
{
196-
subclassCreator: () => new mongodb.OptionsOperation(collection, {}),
197-
subclassType: mongodb.OptionsOperation,
198-
correctCommandName: 'listCollections'
199-
},
200185
{
201186
subclassCreator: () => new mongodb.ProfilingLevelOperation(db, {}),
202187
subclassType: mongodb.ProfilingLevelOperation,

test/mongodb.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ export * from '../src/operations/bulk_write';
167167
export * from '../src/operations/client_bulk_write/command_builder';
168168
export * from '../src/operations/client_bulk_write/common';
169169
export * from '../src/operations/client_bulk_write/results_merger';
170-
export * from '../src/operations/collections';
171170
export * from '../src/operations/command';
172171
export * from '../src/operations/count';
173172
export * from '../src/operations/create_collection';
@@ -181,12 +180,10 @@ export * from '../src/operations/find_and_modify';
181180
export * from '../src/operations/get_more';
182181
export * from '../src/operations/indexes';
183182
export * from '../src/operations/insert';
184-
export * from '../src/operations/is_capped';
185183
export * from '../src/operations/kill_cursors';
186184
export * from '../src/operations/list_collections';
187185
export * from '../src/operations/list_databases';
188186
export * from '../src/operations/operation';
189-
export * from '../src/operations/options_operation';
190187
export * from '../src/operations/profiling_level';
191188
export * from '../src/operations/remove_user';
192189
export * from '../src/operations/rename';

0 commit comments

Comments
 (0)