forked from mongodb/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(NODE-6029): update types for collection listing indexes (mongodb#…
- Loading branch information
1 parent
acb3cf2
commit 71458a9
Showing
5 changed files
with
154 additions
and
17 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
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
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 |
---|---|---|
@@ -1,16 +1,99 @@ | ||
import { expectType } from 'tsd'; | ||
import { expectAssignable, expectType } from 'tsd'; | ||
|
||
import { type Document, MongoClient } from '../../src'; | ||
import { type IndexInformationOptions, MongoClient } from '../../src'; | ||
import { type IndexDescriptionCompact, type IndexDescriptionInfo } from '../mongodb'; | ||
|
||
const client = new MongoClient(''); | ||
const db = client.db('test'); | ||
const collection = db.collection('test.find'); | ||
|
||
// Promise variant testing | ||
expectType<Promise<Document[]>>(collection.indexes()); | ||
expectType<Promise<Document[]>>(collection.indexes({})); | ||
const exampleFullIndexes: IndexDescriptionInfo[] = [ | ||
{ v: 2, key: { _id: 1 }, name: '_id_' }, | ||
{ v: 2, key: { listingName: 'hashed' }, name: 'listingName_hashed' }, | ||
{ | ||
v: 2, | ||
key: { 'auctionDetails.listingId': 1 }, | ||
name: 'auctionDetails_listingId_1', | ||
unique: true | ||
} | ||
]; | ||
const exampleCompactIndexes: IndexDescriptionCompact = { | ||
_id_: [['_id', 1]], | ||
listingName_hashed: [['listingName', 'hashed']], | ||
auctionDetails_listingId_1: [['auctionDetails.listingId', 1]] | ||
}; | ||
|
||
const ambiguousFullness = Math.random() > 0.5; | ||
|
||
// test Collection.prototype.indexes | ||
|
||
const defaultIndexes = await collection.indexes(); | ||
const emptyOptionsIndexes = await collection.indexes({}); | ||
const fullIndexes = await collection.indexes({ full: true }); | ||
const notFullIndexes = await collection.indexes({ full: false }); | ||
const ambiguousIndexes = await collection.indexes({ full: ambiguousFullness }); | ||
|
||
expectAssignable<typeof fullIndexes>(exampleFullIndexes); | ||
expectAssignable<typeof ambiguousIndexes>(exampleFullIndexes); | ||
expectAssignable<typeof ambiguousIndexes>(exampleCompactIndexes); | ||
expectAssignable<typeof notFullIndexes>(exampleCompactIndexes); | ||
|
||
expectType<IndexDescriptionInfo[]>(defaultIndexes); | ||
expectType<IndexDescriptionInfo[]>(emptyOptionsIndexes); | ||
expectType<IndexDescriptionInfo[]>(fullIndexes); | ||
expectType<IndexDescriptionCompact>(notFullIndexes); | ||
expectType<IndexDescriptionInfo[] | IndexDescriptionCompact>(ambiguousIndexes); | ||
|
||
// test Collection.prototype.indexInformation | ||
|
||
const defaultIndexInfo = await collection.indexInformation(); | ||
const emptyOptionsIndexInfo = await collection.indexInformation({}); | ||
const fullIndexInfo = await collection.indexInformation({ full: true }); | ||
const notFullIndexInfo = await collection.indexInformation({ full: false }); | ||
const ambiguousIndexInfo = await collection.indexInformation({ full: ambiguousFullness }); | ||
|
||
expectAssignable<typeof fullIndexInfo>(exampleFullIndexes); | ||
expectAssignable<typeof ambiguousIndexInfo>(exampleFullIndexes); | ||
expectAssignable<typeof ambiguousIndexInfo>(exampleCompactIndexes); | ||
expectAssignable<typeof notFullIndexInfo>(exampleCompactIndexes); | ||
|
||
expectType<IndexDescriptionCompact>(defaultIndexInfo); | ||
expectType<IndexDescriptionCompact>(emptyOptionsIndexInfo); | ||
expectType<IndexDescriptionInfo[]>(fullIndexInfo); | ||
expectType<IndexDescriptionCompact>(notFullIndexInfo); | ||
expectType<IndexDescriptionInfo[] | IndexDescriptionCompact>(ambiguousIndexInfo); | ||
|
||
// Explicit check for iterable result | ||
for (const index of await collection.indexes()) { | ||
expectType<Document>(index); | ||
expectType<IndexDescriptionInfo>(index); | ||
} | ||
|
||
// test Db.prototype.indexInformation | ||
|
||
const dbDefaultIndexInfo = await db.indexInformation('some-collection'); | ||
const dbEmptyOptionsIndexInfo = await db.indexInformation('some-collection', {}); | ||
const dbFullIndexInfo = await db.indexInformation('some-collection', { full: true }); | ||
const dbNotFullIndexInfo = await db.indexInformation('some-collection', { full: false }); | ||
const dbAmbiguousIndexInfo = await db.indexInformation('some-collection', { | ||
full: ambiguousFullness | ||
}); | ||
|
||
expectAssignable<typeof dbFullIndexInfo>(exampleFullIndexes); | ||
expectAssignable<typeof dbAmbiguousIndexInfo>(exampleFullIndexes); | ||
expectAssignable<typeof dbAmbiguousIndexInfo>(exampleCompactIndexes); | ||
|
||
expectType<IndexDescriptionCompact>(dbDefaultIndexInfo); | ||
expectType<IndexDescriptionCompact>(dbEmptyOptionsIndexInfo); | ||
expectType<IndexDescriptionInfo[]>(dbFullIndexInfo); | ||
expectType<IndexDescriptionCompact>(dbNotFullIndexInfo); | ||
expectType<IndexDescriptionInfo[] | IndexDescriptionCompact>(dbAmbiguousIndexInfo); | ||
|
||
// test indexInformation with non-literal options | ||
const options: IndexInformationOptions = {}; | ||
const indexInfo = await db.collection('some-collection').indexInformation(options); | ||
const indexes = await db.collection('some-collection').indexes(options); | ||
const indexDbInfo = await db.indexInformation('some-collection', options); | ||
|
||
expectType<IndexDescriptionCompact | IndexDescriptionInfo[]>(indexInfo); | ||
expectType<IndexDescriptionCompact | IndexDescriptionInfo[]>(indexes); | ||
expectType<IndexDescriptionCompact | IndexDescriptionInfo[]>(indexDbInfo); |