-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(NODE-6029): update types for collection listing indexes #4072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
baileympearson
merged 7 commits into
mongodb:main
from
prenaissance:NODE-6029-indexes-return-type
Apr 11, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bada1ec
fix(NODE-6029): update types for collection listing collection indexes
prenaissance e8f4ab1
fix(NODE-6029): fix review comments
prenaissance 1ba6e8d
test(NODE-6029): add type tests for Db.prototype.indexInformation
prenaissance d859a6a
test(NODE-6029): add comment
prenaissance 1f18df2
fix(NODE-6029): remove unused type
prenaissance cce822e
style(NODE-6029): explicitly declare return types
prenaissance 6f8450d
fix(NODE-6029): fix non-literal index options return type
prenaissance File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.