Skip to content

Commit

Permalink
fix(NODE-3726): add optional option overloads of Db's createCollectio…
Browse files Browse the repository at this point in the history
…n function (#3019)
  • Loading branch information
skrtheboss authored and dariakp committed Nov 2, 2021
1 parent 2a78d5a commit c3149e1
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/db.ts
Expand Up @@ -226,18 +226,17 @@ export class Db {
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
createCollection<TSchema extends Document = Document>(name: string): Promise<Collection<TSchema>>;
createCollection<TSchema extends Document = Document>(
name: string,
callback: Callback<Collection<TSchema>>
): void;
options?: CreateCollectionOptions
): Promise<Collection<TSchema>>;
createCollection<TSchema extends Document = Document>(
name: string,
options: CreateCollectionOptions
): Promise<Collection<TSchema>>;
callback: Callback<Collection<TSchema>>
): void;
createCollection<TSchema extends Document = Document>(
name: string,
options: CreateCollectionOptions,
options: CreateCollectionOptions | undefined,
callback: Callback<Collection<TSchema>>
): void;
createCollection<TSchema extends Document = Document>(
Expand Down
82 changes: 82 additions & 0 deletions test/types/community/db/createCollection.test-d.ts
@@ -0,0 +1,82 @@
import { expectType } from 'tsd';

import {
MongoClient,
ObjectId,
Collection,
CreateCollectionOptions,
AnyError,
Callback
} from '../../../../src/index';

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

interface SubTestSchema {
field1: string;
field2: string;
}

type FruitTypes = 'apple' | 'pear';

// test with collection type
interface TestSchema {
_id: ObjectId;
stringField: string;
numberField: number;
optionalNumberField?: number;
dateField: Date;
fruitTags: string[];
maybeFruitTags?: FruitTypes[];
readonlyFruitTags: ReadonlyArray<string>;
subInterfaceField: SubTestSchema;
subInterfaceArray: SubTestSchema[];
}

const options: CreateCollectionOptions = {};

// createCollection

expectType<Promise<Collection<TestSchema>>>(db.createCollection<TestSchema>('test'));

expectType<Promise<Collection<TestSchema>>>(db.createCollection<TestSchema>('test', options));

// ensure we can use the create collection in a promise based wrapper function
function extendedPromiseBasedCreateCollection(
name: string,
optionalOptions?: CreateCollectionOptions
): Promise<Collection<TestSchema>> {
return db.createCollection<TestSchema>(name, optionalOptions);
}

expectType<Promise<Collection<TestSchema>>>(extendedPromiseBasedCreateCollection('test'));

expectType<void>(
db.createCollection<TestSchema>('test', (err, collection) => {
expectType<AnyError | undefined>(err);
expectType<Collection<TestSchema> | undefined>(collection);
})
);

expectType<void>(
db.createCollection<TestSchema>('test', options, (err, collection) => {
expectType<AnyError | undefined>(err);
expectType<Collection<TestSchema> | undefined>(collection);
})
);

// ensure we can use the create collection in a callback based wrapper function
function extendedCallbackBasedCreateCollection(
name: string,
callback: Callback<Collection<TestSchema>>,
optionalOptions?: CreateCollectionOptions
): void {
db.createCollection<TestSchema>(name, optionalOptions, callback);
}

expectType<void>(
extendedCallbackBasedCreateCollection('test', (err, collection) => {
expectType<AnyError | undefined>(err);
expectType<Collection<TestSchema> | undefined>(collection);
})
);

0 comments on commit c3149e1

Please sign in to comment.