Skip to content

Commit

Permalink
chore(service-provider-core): explicitly define cursor types in servi…
Browse files Browse the repository at this point in the history
…ce-provider-core (#2008)

Instead of directly using driver types, only define the types required
for the Shell API, and define them separately from the driver.

This addresses part of MONGOSH-915 and is likely to help pave the way for
things like MONGOSH-1285.
  • Loading branch information
addaleax authored Jun 3, 2024
1 parent ea4490b commit cc2d431
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 27 deletions.
69 changes: 69 additions & 0 deletions packages/service-provider-core/src/cursors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import type {
CollationOptions,
CountOptions,
CursorFlag,
Document,
ExplainVerbosityLike,
ReadConcernLike,
ReadPreferenceLike,
ResumeToken,
} from './all-transport-types';

interface ServiceProviderBaseCursor<TSchema = Document> {
close(): Promise<void>;
hasNext(): Promise<boolean>;
next(): Promise<TSchema | null>;
tryNext(): Promise<TSchema | null>;
readonly closed: boolean;
[Symbol.asyncIterator](): AsyncGenerator<TSchema, void, void>;
}

export interface ServiceProviderAbstractCursor<TSchema = Document>
extends ServiceProviderBaseCursor<TSchema> {
batchSize(number: number): void;
maxTimeMS(value: number): void;
bufferedCount(): number;
readBufferedDocuments(number?: number): TSchema[];
toArray(): Promise<TSchema[]>;
}

export interface ServiceProviderAggregationOrFindCursor<TSchema = Document>
extends ServiceProviderAbstractCursor<TSchema> {
project($project: Document): void;
skip($skip: number): void;
sort($sort: Document): void;
explain(verbosity?: ExplainVerbosityLike): Promise<Document>;
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<TSchema = Document>
extends ServiceProviderAbstractCursor<TSchema> {}

export interface ServiceProviderFindCursor<TSchema = Document>
extends ServiceProviderAggregationOrFindCursor<TSchema> {
allowDiskUse(allow?: boolean): void;
collation(value: CollationOptions): void;
comment(value: string): void;
maxAwaitTimeMS(value: number): void;
count(options?: CountOptions): Promise<number>;
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<TSchema = Document>
extends ServiceProviderAggregationOrFindCursor<TSchema> {}

export interface ServiceProviderChangeStream<TSchema = Document>
extends ServiceProviderBaseCursor<TSchema> {
next(): Promise<TSchema>;
readonly resumeToken: ResumeToken;
}
8 changes: 8 additions & 0 deletions packages/service-provider-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ export {

export { bson } from './bson-export';

export {
ServiceProviderAbstractCursor,
ServiceProviderAggregationCursor,
ServiceProviderFindCursor,
ServiceProviderRunCommandCursor,
ServiceProviderChangeStream,
} from './cursors';

export {
ServiceProvider,
ShellAuthOptions,
Expand Down
17 changes: 10 additions & 7 deletions packages/service-provider-core/src/readable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -37,7 +40,7 @@ export default interface Readable {
pipeline: Document[],
options?: AggregateOptions,
dbOptions?: DbOptions
): AggregationCursor;
): ServiceProviderAggregationCursor;

/**
* Run an aggregation pipeline on the DB.
Expand All @@ -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
Expand Down Expand Up @@ -152,7 +155,7 @@ export default interface Readable {
filter?: Document,
options?: FindOptions,
dbOptions?: DbOptions
): FindCursor;
): ServiceProviderFindCursor;

/**
* Get currently known topology information.
Expand Down Expand Up @@ -215,7 +218,7 @@ export default interface Readable {
dbOptions?: DbOptions,
db?: string,
coll?: string
): ChangeStream<Document>;
): ServiceProviderChangeStream;

/**
* Returns an array of documents that identify and describe the existing
Expand Down
5 changes: 3 additions & 2 deletions packages/service-provider-core/src/writable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RunCommandCursor, RunCursorCommandOptions } from 'mongodb';
import type { RunCursorCommandOptions } from 'mongodb';
import type {
Document,
InsertOneOptions,
Expand All @@ -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.
Expand Down Expand Up @@ -70,7 +71,7 @@ export default interface Writable {
spec: Document,
options: RunCursorCommandOptions,
dbOptions?: DbOptions
): RunCommandCursor;
): ServiceProviderRunCommandCursor;

/**
* Drop a database
Expand Down
10 changes: 5 additions & 5 deletions packages/shell-api/src/abstract-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -20,7 +20,7 @@ import { iterate } from './helpers';
export abstract class AbstractCursor<
CursorType extends
| ServiceProviderAggregationCursor
| ServiceProviderCursor
| ServiceProviderFindCursor
| ServiceProviderRunCommandCursor
> extends ShellApiWithMongoClass {
_mongo: Mongo;
Expand Down Expand Up @@ -78,7 +78,7 @@ export abstract class AbstractCursor<

@returnsPromise
async hasNext(): Promise<boolean> {
return this._cursor.hasNext();
return await this._cursor.hasNext();
}

@returnsPromise
Expand Down
8 changes: 5 additions & 3 deletions packages/shell-api/src/aggregate-or-find-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CursorType> {
@returnType('this')
projection(spec: Document): this {
Expand Down
2 changes: 1 addition & 1 deletion packages/shell-api/src/aggregation-cursor.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 7 additions & 3 deletions packages/shell-api/src/change-stream-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ShellApiWithMongoClass,
} from './decorators';
import type {
ChangeStream,
ServiceProviderChangeStream,
Document,
ResumeToken,
} from '@mongosh/service-provider-core';
Expand All @@ -23,11 +23,15 @@ import type Mongo from './mongo';
@shellApiClassDefault
export default class ChangeStreamCursor extends ShellApiWithMongoClass {
_mongo: Mongo;
_cursor: ChangeStream<Document>;
_cursor: ServiceProviderChangeStream<Document>;
_currentIterationResult: CursorIterationResult | null = null;
_on: string;

constructor(cursor: ChangeStream<Document>, on: string, mongo: Mongo) {
constructor(
cursor: ServiceProviderChangeStream<Document>,
on: string,
mongo: Mongo
) {
super();
this._cursor = cursor;
this._on = on;
Expand Down
10 changes: 5 additions & 5 deletions packages/shell-api/src/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from './decorators';
import { ServerVersions, CURSOR_FLAGS } from './enums';
import type {
FindCursor as ServiceProviderCursor,
ServiceProviderFindCursor,
CursorFlag,
Document,
CollationOptions,
Expand All @@ -27,10 +27,10 @@ import type Mongo from './mongo';
import { AggregateOrFindCursor } from './aggregate-or-find-cursor';

@shellApiClassDefault
export default class Cursor extends AggregateOrFindCursor<ServiceProviderCursor> {
export default class Cursor extends AggregateOrFindCursor<ServiceProviderFindCursor> {
_tailable = false;

constructor(mongo: Mongo, cursor: ServiceProviderCursor) {
constructor(mongo: Mongo, cursor: ServiceProviderFindCursor) {
super(mongo, cursor);
}

Expand Down Expand Up @@ -100,7 +100,7 @@ export default class Cursor extends AggregateOrFindCursor<ServiceProviderCursor>
@returnsPromise
@deprecated
async count(): Promise<number> {
return this._cursor.count();
return await this._cursor.count();
}

@returnsPromise
Expand Down Expand Up @@ -199,7 +199,7 @@ export default class Cursor extends AggregateOrFindCursor<ServiceProviderCursor>

@returnsPromise
async size(): Promise<number> {
return this._cursor.count();
return await this._cursor.count();
}

@returnType('Cursor')
Expand Down
2 changes: 1 addition & 1 deletion packages/shell-api/src/run-command-cursor.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit cc2d431

Please sign in to comment.