|
1 |
| -import { expectType } from 'tsd'; |
| 1 | +import { expectAssignable, expectType } from 'tsd'; |
2 | 2 |
|
3 |
| -import { type Document, MongoClient } from '../../src'; |
| 3 | +import { type IndexInformationOptions, MongoClient } from '../../src'; |
| 4 | +import { type IndexDescriptionCompact, type IndexDescriptionInfo } from '../mongodb'; |
4 | 5 |
|
5 | 6 | const client = new MongoClient('');
|
6 | 7 | const db = client.db('test');
|
7 | 8 | const collection = db.collection('test.find');
|
8 | 9 |
|
9 |
| -// Promise variant testing |
10 |
| -expectType<Promise<Document[]>>(collection.indexes()); |
11 |
| -expectType<Promise<Document[]>>(collection.indexes({})); |
| 10 | +const exampleFullIndexes: IndexDescriptionInfo[] = [ |
| 11 | + { v: 2, key: { _id: 1 }, name: '_id_' }, |
| 12 | + { v: 2, key: { listingName: 'hashed' }, name: 'listingName_hashed' }, |
| 13 | + { |
| 14 | + v: 2, |
| 15 | + key: { 'auctionDetails.listingId': 1 }, |
| 16 | + name: 'auctionDetails_listingId_1', |
| 17 | + unique: true |
| 18 | + } |
| 19 | +]; |
| 20 | +const exampleCompactIndexes: IndexDescriptionCompact = { |
| 21 | + _id_: [['_id', 1]], |
| 22 | + listingName_hashed: [['listingName', 'hashed']], |
| 23 | + auctionDetails_listingId_1: [['auctionDetails.listingId', 1]] |
| 24 | +}; |
| 25 | + |
| 26 | +const ambiguousFullness = Math.random() > 0.5; |
| 27 | + |
| 28 | +// test Collection.prototype.indexes |
| 29 | + |
| 30 | +const defaultIndexes = await collection.indexes(); |
| 31 | +const emptyOptionsIndexes = await collection.indexes({}); |
| 32 | +const fullIndexes = await collection.indexes({ full: true }); |
| 33 | +const notFullIndexes = await collection.indexes({ full: false }); |
| 34 | +const ambiguousIndexes = await collection.indexes({ full: ambiguousFullness }); |
| 35 | + |
| 36 | +expectAssignable<typeof fullIndexes>(exampleFullIndexes); |
| 37 | +expectAssignable<typeof ambiguousIndexes>(exampleFullIndexes); |
| 38 | +expectAssignable<typeof ambiguousIndexes>(exampleCompactIndexes); |
| 39 | +expectAssignable<typeof notFullIndexes>(exampleCompactIndexes); |
| 40 | + |
| 41 | +expectType<IndexDescriptionInfo[]>(defaultIndexes); |
| 42 | +expectType<IndexDescriptionInfo[]>(emptyOptionsIndexes); |
| 43 | +expectType<IndexDescriptionInfo[]>(fullIndexes); |
| 44 | +expectType<IndexDescriptionCompact>(notFullIndexes); |
| 45 | +expectType<IndexDescriptionInfo[] | IndexDescriptionCompact>(ambiguousIndexes); |
| 46 | + |
| 47 | +// test Collection.prototype.indexInformation |
| 48 | + |
| 49 | +const defaultIndexInfo = await collection.indexInformation(); |
| 50 | +const emptyOptionsIndexInfo = await collection.indexInformation({}); |
| 51 | +const fullIndexInfo = await collection.indexInformation({ full: true }); |
| 52 | +const notFullIndexInfo = await collection.indexInformation({ full: false }); |
| 53 | +const ambiguousIndexInfo = await collection.indexInformation({ full: ambiguousFullness }); |
| 54 | + |
| 55 | +expectAssignable<typeof fullIndexInfo>(exampleFullIndexes); |
| 56 | +expectAssignable<typeof ambiguousIndexInfo>(exampleFullIndexes); |
| 57 | +expectAssignable<typeof ambiguousIndexInfo>(exampleCompactIndexes); |
| 58 | +expectAssignable<typeof notFullIndexInfo>(exampleCompactIndexes); |
| 59 | + |
| 60 | +expectType<IndexDescriptionCompact>(defaultIndexInfo); |
| 61 | +expectType<IndexDescriptionCompact>(emptyOptionsIndexInfo); |
| 62 | +expectType<IndexDescriptionInfo[]>(fullIndexInfo); |
| 63 | +expectType<IndexDescriptionCompact>(notFullIndexInfo); |
| 64 | +expectType<IndexDescriptionInfo[] | IndexDescriptionCompact>(ambiguousIndexInfo); |
12 | 65 |
|
13 | 66 | // Explicit check for iterable result
|
14 | 67 | for (const index of await collection.indexes()) {
|
15 |
| - expectType<Document>(index); |
| 68 | + expectType<IndexDescriptionInfo>(index); |
16 | 69 | }
|
| 70 | + |
| 71 | +// test Db.prototype.indexInformation |
| 72 | + |
| 73 | +const dbDefaultIndexInfo = await db.indexInformation('some-collection'); |
| 74 | +const dbEmptyOptionsIndexInfo = await db.indexInformation('some-collection', {}); |
| 75 | +const dbFullIndexInfo = await db.indexInformation('some-collection', { full: true }); |
| 76 | +const dbNotFullIndexInfo = await db.indexInformation('some-collection', { full: false }); |
| 77 | +const dbAmbiguousIndexInfo = await db.indexInformation('some-collection', { |
| 78 | + full: ambiguousFullness |
| 79 | +}); |
| 80 | + |
| 81 | +expectAssignable<typeof dbFullIndexInfo>(exampleFullIndexes); |
| 82 | +expectAssignable<typeof dbAmbiguousIndexInfo>(exampleFullIndexes); |
| 83 | +expectAssignable<typeof dbAmbiguousIndexInfo>(exampleCompactIndexes); |
| 84 | + |
| 85 | +expectType<IndexDescriptionCompact>(dbDefaultIndexInfo); |
| 86 | +expectType<IndexDescriptionCompact>(dbEmptyOptionsIndexInfo); |
| 87 | +expectType<IndexDescriptionInfo[]>(dbFullIndexInfo); |
| 88 | +expectType<IndexDescriptionCompact>(dbNotFullIndexInfo); |
| 89 | +expectType<IndexDescriptionInfo[] | IndexDescriptionCompact>(dbAmbiguousIndexInfo); |
| 90 | + |
| 91 | +// test indexInformation with non-literal options |
| 92 | +const options: IndexInformationOptions = {}; |
| 93 | +const indexInfo = await db.collection('some-collection').indexInformation(options); |
| 94 | +const indexes = await db.collection('some-collection').indexes(options); |
| 95 | +const indexDbInfo = await db.indexInformation('some-collection', options); |
| 96 | + |
| 97 | +expectType<IndexDescriptionCompact | IndexDescriptionInfo[]>(indexInfo); |
| 98 | +expectType<IndexDescriptionCompact | IndexDescriptionInfo[]>(indexes); |
| 99 | +expectType<IndexDescriptionCompact | IndexDescriptionInfo[]>(indexDbInfo); |
0 commit comments