|
| 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