Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-3599): incorrect indexes return type #2980

Merged
merged 1 commit into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1190,14 +1190,14 @@ export class Collection<TSchema extends Document = Document> {
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
indexes(): Promise<Document>;
indexes(callback: Callback<Document>): void;
indexes(options: IndexInformationOptions): Promise<Document>;
indexes(options: IndexInformationOptions, callback: Callback<Document>): void;
indexes(): Promise<Document[]>;
indexes(callback: Callback<Document[]>): void;
indexes(options: IndexInformationOptions): Promise<Document[]>;
indexes(options: IndexInformationOptions, callback: Callback<Document[]>): void;
indexes(
options?: IndexInformationOptions | Callback<Document>,
callback?: Callback<Document>
): Promise<Document> | void {
options?: IndexInformationOptions | Callback<Document[]>,
callback?: Callback<Document[]>
): Promise<Document[]> | void {
if (typeof options === 'function') (callback = options), (options = {});

return executeOperation(
Expand Down
4 changes: 2 additions & 2 deletions src/operations/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ function makeIndexSpec(indexSpec: IndexSpecification, options: any): IndexDescri
}

/** @internal */
export class IndexesOperation extends AbstractOperation<Document> {
export class IndexesOperation extends AbstractOperation<Document[]> {
options: IndexInformationOptions;
collection: Collection;

Expand All @@ -169,7 +169,7 @@ export class IndexesOperation extends AbstractOperation<Document> {
this.collection = collection;
}

execute(server: Server, session: ClientSession, callback: Callback<Document>): void {
execute(server: Server, session: ClientSession, callback: Callback<Document[]>): void {
const coll = this.collection;
const options = this.options;

Expand Down
26 changes: 26 additions & 0 deletions test/types/indexes_test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expectType } from 'tsd';
import { MongoClient, Document, AnyError } from '../../src';

const client = new MongoClient('');
const db = client.db('test');
const collection = db.collection('test.find');

// Promise variant testing
expectType<Promise<Document[]>>(collection.indexes());
expectType<Promise<Document[]>>(collection.indexes({}));

// Explicit check for iterable result
for (const index of await collection.indexes()) {
expectType<Document>(index);
}

// Callback variant testing
collection.indexes((err, indexes) => {
expectType<AnyError | undefined>(err);
expectType<Document[] | undefined>(indexes);
});

collection.indexes({}, (err, indexes) => {
expectType<AnyError | undefined>(err);
expectType<Document[] | undefined>(indexes);
});