Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(NODE-3726): add optional option overloads of Db's createCollectio…
…n function (#3019)
- Loading branch information
1 parent
2a78d5a
commit c3149e1
Showing
2 changed files
with
87 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}) | ||
); |