Skip to content

Commit 9b40c61

Browse files
skrthebossljhaywar
authored andcommitted
fix(NODE-3726): add optional option overloads of Db's createCollection function (#3019)
1 parent 6643136 commit 9b40c61

File tree

2 files changed

+87
-6
lines changed

2 files changed

+87
-6
lines changed

src/db.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -226,18 +226,17 @@ export class Db {
226226
* @param options - Optional settings for the command
227227
* @param callback - An optional callback, a Promise will be returned if none is provided
228228
*/
229-
createCollection<TSchema extends Document = Document>(name: string): Promise<Collection<TSchema>>;
230229
createCollection<TSchema extends Document = Document>(
231230
name: string,
232-
callback: Callback<Collection<TSchema>>
233-
): void;
231+
options?: CreateCollectionOptions
232+
): Promise<Collection<TSchema>>;
234233
createCollection<TSchema extends Document = Document>(
235234
name: string,
236-
options: CreateCollectionOptions
237-
): Promise<Collection<TSchema>>;
235+
callback: Callback<Collection<TSchema>>
236+
): void;
238237
createCollection<TSchema extends Document = Document>(
239238
name: string,
240-
options: CreateCollectionOptions,
239+
options: CreateCollectionOptions | undefined,
241240
callback: Callback<Collection<TSchema>>
242241
): void;
243242
createCollection<TSchema extends Document = Document>(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { expectType } from 'tsd';
2+
3+
import {
4+
MongoClient,
5+
ObjectId,
6+
Collection,
7+
CreateCollectionOptions,
8+
AnyError,
9+
Callback
10+
} from '../../../../src/index';
11+
12+
const client = new MongoClient('');
13+
const db = client.db('test');
14+
15+
interface SubTestSchema {
16+
field1: string;
17+
field2: string;
18+
}
19+
20+
type FruitTypes = 'apple' | 'pear';
21+
22+
// test with collection type
23+
interface TestSchema {
24+
_id: ObjectId;
25+
stringField: string;
26+
numberField: number;
27+
optionalNumberField?: number;
28+
dateField: Date;
29+
fruitTags: string[];
30+
maybeFruitTags?: FruitTypes[];
31+
readonlyFruitTags: ReadonlyArray<string>;
32+
subInterfaceField: SubTestSchema;
33+
subInterfaceArray: SubTestSchema[];
34+
}
35+
36+
const options: CreateCollectionOptions = {};
37+
38+
// createCollection
39+
40+
expectType<Promise<Collection<TestSchema>>>(db.createCollection<TestSchema>('test'));
41+
42+
expectType<Promise<Collection<TestSchema>>>(db.createCollection<TestSchema>('test', options));
43+
44+
// ensure we can use the create collection in a promise based wrapper function
45+
function extendedPromiseBasedCreateCollection(
46+
name: string,
47+
optionalOptions?: CreateCollectionOptions
48+
): Promise<Collection<TestSchema>> {
49+
return db.createCollection<TestSchema>(name, optionalOptions);
50+
}
51+
52+
expectType<Promise<Collection<TestSchema>>>(extendedPromiseBasedCreateCollection('test'));
53+
54+
expectType<void>(
55+
db.createCollection<TestSchema>('test', (err, collection) => {
56+
expectType<AnyError | undefined>(err);
57+
expectType<Collection<TestSchema> | undefined>(collection);
58+
})
59+
);
60+
61+
expectType<void>(
62+
db.createCollection<TestSchema>('test', options, (err, collection) => {
63+
expectType<AnyError | undefined>(err);
64+
expectType<Collection<TestSchema> | undefined>(collection);
65+
})
66+
);
67+
68+
// ensure we can use the create collection in a callback based wrapper function
69+
function extendedCallbackBasedCreateCollection(
70+
name: string,
71+
callback: Callback<Collection<TestSchema>>,
72+
optionalOptions?: CreateCollectionOptions
73+
): void {
74+
db.createCollection<TestSchema>(name, optionalOptions, callback);
75+
}
76+
77+
expectType<void>(
78+
extendedCallbackBasedCreateCollection('test', (err, collection) => {
79+
expectType<AnyError | undefined>(err);
80+
expectType<Collection<TestSchema> | undefined>(collection);
81+
})
82+
);

0 commit comments

Comments
 (0)