diff --git a/packages/service-provider-core/src/cursors.ts b/packages/service-provider-core/src/cursors.ts new file mode 100644 index 000000000..a09b6ddd6 --- /dev/null +++ b/packages/service-provider-core/src/cursors.ts @@ -0,0 +1,69 @@ +import type { + CollationOptions, + CountOptions, + CursorFlag, + Document, + ExplainVerbosityLike, + ReadConcernLike, + ReadPreferenceLike, + ResumeToken, +} from './all-transport-types'; + +interface ServiceProviderBaseCursor { + close(): Promise; + hasNext(): Promise; + next(): Promise; + tryNext(): Promise; + readonly closed: boolean; + [Symbol.asyncIterator](): AsyncGenerator; +} + +export interface ServiceProviderAbstractCursor + extends ServiceProviderBaseCursor { + batchSize(number: number): void; + maxTimeMS(value: number): void; + bufferedCount(): number; + readBufferedDocuments(number?: number): TSchema[]; + toArray(): Promise; +} + +export interface ServiceProviderAggregationOrFindCursor + extends ServiceProviderAbstractCursor { + project($project: Document): void; + skip($skip: number): void; + sort($sort: Document): void; + explain(verbosity?: ExplainVerbosityLike): Promise; + addCursorFlag(flag: CursorFlag, value: boolean): void; + withReadPreference(readPreference: ReadPreferenceLike): this; + withReadConcern(readConcern: ReadConcernLike): this; +} + +// eslint-disable-next-line @typescript-eslint/no-empty-interface +export interface ServiceProviderRunCommandCursor + extends ServiceProviderAbstractCursor {} + +export interface ServiceProviderFindCursor + extends ServiceProviderAggregationOrFindCursor { + allowDiskUse(allow?: boolean): void; + collation(value: CollationOptions): void; + comment(value: string): void; + maxAwaitTimeMS(value: number): void; + count(options?: CountOptions): Promise; + hint(hint: string | Document): void; + max(max: Document): void; + min(min: Document): void; + limit(value: number): void; + skip(value: number): void; + returnKey(value: boolean): void; + showRecordId(value: boolean): void; +} + +// eslint-disable-next-line @typescript-eslint/no-empty-interface +export interface ServiceProviderAggregationCursor + extends ServiceProviderAggregationOrFindCursor {} + +export interface ServiceProviderChangeStream + extends ServiceProviderBaseCursor { + next(): Promise; + readonly resumeToken: ResumeToken; +} diff --git a/packages/service-provider-core/src/index.ts b/packages/service-provider-core/src/index.ts index ed965048f..222575a7a 100644 --- a/packages/service-provider-core/src/index.ts +++ b/packages/service-provider-core/src/index.ts @@ -18,6 +18,14 @@ export { export { bson } from './bson-export'; +export { + ServiceProviderAbstractCursor, + ServiceProviderAggregationCursor, + ServiceProviderFindCursor, + ServiceProviderRunCommandCursor, + ServiceProviderChangeStream, +} from './cursors'; + export { ServiceProvider, ShellAuthOptions, diff --git a/packages/service-provider-core/src/readable.ts b/packages/service-provider-core/src/readable.ts index ff7f341ea..a3450a8c3 100644 --- a/packages/service-provider-core/src/readable.ts +++ b/packages/service-provider-core/src/readable.ts @@ -8,13 +8,16 @@ import type { FindOptions, ListCollectionsOptions, ListIndexesOptions, - AggregationCursor, - FindCursor, DbOptions, ReadPreferenceFromOptions, ReadPreferenceLike, } from './all-transport-types'; -import type { ChangeStream, ChangeStreamOptions } from './all-transport-types'; +import type { ChangeStreamOptions } from './all-transport-types'; +import type { + ServiceProviderAggregationCursor, + ServiceProviderChangeStream, + ServiceProviderFindCursor, +} from './cursors'; /** * Interface for read operations in the CRUD specification. @@ -37,7 +40,7 @@ export default interface Readable { pipeline: Document[], options?: AggregateOptions, dbOptions?: DbOptions - ): AggregationCursor; + ): ServiceProviderAggregationCursor; /** * Run an aggregation pipeline on the DB. @@ -54,7 +57,7 @@ export default interface Readable { pipeline: Document[], options?: AggregateOptions, dbOptions?: DbOptions - ): AggregationCursor; + ): ServiceProviderAggregationCursor; /** * Returns the count of documents that would match a find() query for the @@ -152,7 +155,7 @@ export default interface Readable { filter?: Document, options?: FindOptions, dbOptions?: DbOptions - ): FindCursor; + ): ServiceProviderFindCursor; /** * Get currently known topology information. @@ -215,7 +218,7 @@ export default interface Readable { dbOptions?: DbOptions, db?: string, coll?: string - ): ChangeStream; + ): ServiceProviderChangeStream; /** * Returns an array of documents that identify and describe the existing diff --git a/packages/service-provider-core/src/writable.ts b/packages/service-provider-core/src/writable.ts index 3a82dceeb..cc1e312fc 100644 --- a/packages/service-provider-core/src/writable.ts +++ b/packages/service-provider-core/src/writable.ts @@ -1,4 +1,4 @@ -import type { RunCommandCursor, RunCursorCommandOptions } from 'mongodb'; +import type { RunCursorCommandOptions } from 'mongodb'; import type { Document, InsertOneOptions, @@ -25,6 +25,7 @@ import type { OrderedBulkOperation, UnorderedBulkOperation, } from './all-transport-types'; +import type { ServiceProviderRunCommandCursor } from './cursors'; /** * Interface for write operations in the CRUD specification. @@ -70,7 +71,7 @@ export default interface Writable { spec: Document, options: RunCursorCommandOptions, dbOptions?: DbOptions - ): RunCommandCursor; + ): ServiceProviderRunCommandCursor; /** * Drop a database diff --git a/packages/shell-api/src/abstract-cursor.ts b/packages/shell-api/src/abstract-cursor.ts index b3af0ec8a..f51f1dad9 100644 --- a/packages/shell-api/src/abstract-cursor.ts +++ b/packages/shell-api/src/abstract-cursor.ts @@ -8,9 +8,9 @@ import { import type Mongo from './mongo'; import type { Document, - FindCursor as ServiceProviderCursor, - AggregationCursor as ServiceProviderAggregationCursor, - RunCommandCursor as ServiceProviderRunCommandCursor, + ServiceProviderFindCursor, + ServiceProviderAggregationCursor, + ServiceProviderRunCommandCursor, } from '@mongosh/service-provider-core'; import { asPrintable } from './enums'; import { CursorIterationResult } from './result'; @@ -20,7 +20,7 @@ import { iterate } from './helpers'; export abstract class AbstractCursor< CursorType extends | ServiceProviderAggregationCursor - | ServiceProviderCursor + | ServiceProviderFindCursor | ServiceProviderRunCommandCursor > extends ShellApiWithMongoClass { _mongo: Mongo; @@ -78,7 +78,7 @@ export abstract class AbstractCursor< @returnsPromise async hasNext(): Promise { - return this._cursor.hasNext(); + return await this._cursor.hasNext(); } @returnsPromise diff --git a/packages/shell-api/src/aggregate-or-find-cursor.ts b/packages/shell-api/src/aggregate-or-find-cursor.ts index 716a7f6e3..46cb48e01 100644 --- a/packages/shell-api/src/aggregate-or-find-cursor.ts +++ b/packages/shell-api/src/aggregate-or-find-cursor.ts @@ -7,15 +7,17 @@ import { import type { Document, ExplainVerbosityLike, - FindCursor as ServiceProviderCursor, - AggregationCursor as ServiceProviderAggregationCursor, + ServiceProviderFindCursor, + ServiceProviderAggregationCursor, } from '@mongosh/service-provider-core'; import { validateExplainableVerbosity, markAsExplainOutput } from './helpers'; import { AbstractCursor } from './abstract-cursor'; @shellApiClassNoHelp export abstract class AggregateOrFindCursor< - CursorType extends ServiceProviderAggregationCursor | ServiceProviderCursor + CursorType extends + | ServiceProviderAggregationCursor + | ServiceProviderFindCursor > extends AbstractCursor { @returnType('this') projection(spec: Document): this { diff --git a/packages/shell-api/src/aggregation-cursor.ts b/packages/shell-api/src/aggregation-cursor.ts index 182097189..e142a64eb 100644 --- a/packages/shell-api/src/aggregation-cursor.ts +++ b/packages/shell-api/src/aggregation-cursor.ts @@ -1,6 +1,6 @@ import type Mongo from './mongo'; import { shellApiClassDefault } from './decorators'; -import type { AggregationCursor as ServiceProviderAggregationCursor } from '@mongosh/service-provider-core'; +import type { ServiceProviderAggregationCursor } from '@mongosh/service-provider-core'; import { AggregateOrFindCursor } from './aggregate-or-find-cursor'; @shellApiClassDefault diff --git a/packages/shell-api/src/change-stream-cursor.ts b/packages/shell-api/src/change-stream-cursor.ts index 89df34ada..cf3c41b26 100644 --- a/packages/shell-api/src/change-stream-cursor.ts +++ b/packages/shell-api/src/change-stream-cursor.ts @@ -6,7 +6,7 @@ import { ShellApiWithMongoClass, } from './decorators'; import type { - ChangeStream, + ServiceProviderChangeStream, Document, ResumeToken, } from '@mongosh/service-provider-core'; @@ -23,11 +23,15 @@ import type Mongo from './mongo'; @shellApiClassDefault export default class ChangeStreamCursor extends ShellApiWithMongoClass { _mongo: Mongo; - _cursor: ChangeStream; + _cursor: ServiceProviderChangeStream; _currentIterationResult: CursorIterationResult | null = null; _on: string; - constructor(cursor: ChangeStream, on: string, mongo: Mongo) { + constructor( + cursor: ServiceProviderChangeStream, + on: string, + mongo: Mongo + ) { super(); this._cursor = cursor; this._on = on; diff --git a/packages/shell-api/src/cursor.ts b/packages/shell-api/src/cursor.ts index 425be1607..2d1cd219f 100644 --- a/packages/shell-api/src/cursor.ts +++ b/packages/shell-api/src/cursor.ts @@ -14,7 +14,7 @@ import { } from './decorators'; import { ServerVersions, CURSOR_FLAGS } from './enums'; import type { - FindCursor as ServiceProviderCursor, + ServiceProviderFindCursor, CursorFlag, Document, CollationOptions, @@ -27,10 +27,10 @@ import type Mongo from './mongo'; import { AggregateOrFindCursor } from './aggregate-or-find-cursor'; @shellApiClassDefault -export default class Cursor extends AggregateOrFindCursor { +export default class Cursor extends AggregateOrFindCursor { _tailable = false; - constructor(mongo: Mongo, cursor: ServiceProviderCursor) { + constructor(mongo: Mongo, cursor: ServiceProviderFindCursor) { super(mongo, cursor); } @@ -100,7 +100,7 @@ export default class Cursor extends AggregateOrFindCursor @returnsPromise @deprecated async count(): Promise { - return this._cursor.count(); + return await this._cursor.count(); } @returnsPromise @@ -199,7 +199,7 @@ export default class Cursor extends AggregateOrFindCursor @returnsPromise async size(): Promise { - return this._cursor.count(); + return await this._cursor.count(); } @returnType('Cursor') diff --git a/packages/shell-api/src/run-command-cursor.ts b/packages/shell-api/src/run-command-cursor.ts index 065b8d813..20fbc992d 100644 --- a/packages/shell-api/src/run-command-cursor.ts +++ b/packages/shell-api/src/run-command-cursor.ts @@ -1,6 +1,6 @@ import type Mongo from './mongo'; import { shellApiClassDefault } from './decorators'; -import type { RunCommandCursor as ServiceProviderRunCommandCursor } from '@mongosh/service-provider-core'; +import type { ServiceProviderRunCommandCursor } from '@mongosh/service-provider-core'; import { AbstractCursor } from './abstract-cursor'; @shellApiClassDefault