Skip to content

Commit f87f376

Browse files
authored
fix(NODE-3441): fix typings for createIndexes (#2915)
1 parent ac52605 commit f87f376

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

src/operations/indexes.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const VALID_INDEX_OPTIONS = new Set([
3535
'expireAfterSeconds',
3636
'storageEngine',
3737
'collation',
38+
'version',
3839

3940
// text indexes
4041
'weights',
@@ -70,7 +71,28 @@ export type IndexSpecification = OneOrMore<
7071
>;
7172

7273
/** @public */
73-
export interface IndexDescription {
74+
export interface IndexDescription
75+
extends Pick<
76+
CreateIndexesOptions,
77+
| 'background'
78+
| 'unique'
79+
| 'partialFilterExpression'
80+
| 'sparse'
81+
| 'hidden'
82+
| 'expireAfterSeconds'
83+
| 'storageEngine'
84+
| 'version'
85+
| 'weights'
86+
| 'default_language'
87+
| 'language_override'
88+
| 'textIndexVersion'
89+
| '2dsphereIndexVersion'
90+
| 'bits'
91+
| 'min'
92+
| 'max'
93+
| 'bucketSize'
94+
| 'wildcardProjection'
95+
> {
7496
collation?: CollationOptions;
7597
name?: string;
7698
key: Document;
@@ -90,9 +112,12 @@ export interface CreateIndexesOptions extends CommandOperationOptions {
90112
sparse?: boolean;
91113
/** Allows you to expire data on indexes applied to a data (MongoDB 2.2 or higher) */
92114
expireAfterSeconds?: number;
115+
/** Allows users to configure the storage engine on a per-index basis when creating an index. (MongoDB 3.0 or higher) */
93116
storageEngine?: Document;
94117
/** (MongoDB 4.4. or higher) Specifies how many data-bearing members of a replica set, including the primary, must complete the index builds successfully before the primary marks the indexes as ready. This option accepts the same values for the "w" field in a write concern plus "votingMembers", which indicates all voting data-bearing nodes. */
95118
commitQuorum?: number | string;
119+
/** Specifies the index version number, either 0 or 1. */
120+
version?: number;
96121
// text indexes
97122
weights?: Document;
98123
default_language?: string;
@@ -110,6 +135,8 @@ export interface CreateIndexesOptions extends CommandOperationOptions {
110135
bucketSize?: number;
111136
// wildcard indexes
112137
wildcardProjection?: Document;
138+
/** Specifies that the index should exist on the target collection but should not be used by the query planner when executing operations. (MongoDB 4.4 or higher) */
139+
hidden?: boolean;
113140
}
114141

115142
function makeIndexSpec(indexSpec: IndexSpecification, options: any): IndexDescription {

test/types/index_options.test-d.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { expectAssignable, expectNotAssignable } from 'tsd';
2+
import type { IndexDescription } from '../../src';
3+
4+
// test that all valid index options are allowed in IndexDescription
5+
expectAssignable<IndexDescription>({ key: {}, background: true });
6+
expectAssignable<IndexDescription>({ key: {}, expireAfterSeconds: 2400 });
7+
expectAssignable<IndexDescription>({ key: {}, name: 'index_1' });
8+
expectAssignable<IndexDescription>({ key: {}, sparse: true });
9+
expectAssignable<IndexDescription>({ key: {}, storageEngine: {} });
10+
expectAssignable<IndexDescription>({ key: {}, unique: true });
11+
expectAssignable<IndexDescription>({ key: {}, version: 1 });
12+
expectAssignable<IndexDescription>({ key: {}, default_language: 'english' });
13+
expectAssignable<IndexDescription>({ key: {}, language_override: 'english' });
14+
expectAssignable<IndexDescription>({ key: {}, textIndexVersion: 2 });
15+
expectAssignable<IndexDescription>({ key: {}, weights: {} });
16+
expectAssignable<IndexDescription>({ key: {}, '2dsphereIndexVersion': 2 });
17+
expectAssignable<IndexDescription>({ key: {}, bits: 1 });
18+
expectAssignable<IndexDescription>({ key: {}, max: 1.1 });
19+
expectAssignable<IndexDescription>({ key: {}, min: 9.9 });
20+
expectAssignable<IndexDescription>({ key: {}, bucketSize: 100 });
21+
expectAssignable<IndexDescription>({ key: {}, partialFilterExpression: {} });
22+
expectAssignable<IndexDescription>({ key: {}, collation: { locale: 'en' } });
23+
expectAssignable<IndexDescription>({ key: {}, wildcardProjection: {} });
24+
expectAssignable<IndexDescription>({ key: {}, hidden: true });
25+
expectNotAssignable<IndexDescription>({ key: {}, invalidOption: 2400 });

test/types/mongodb.test-d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { expectType, expectDeprecated } from 'tsd';
2-
32
import { MongoClient } from '../../src/mongo_client';
43
import { Collection } from '../../src/collection';
54
import { AggregationCursor } from '../../src/cursor/aggregation_cursor';

0 commit comments

Comments
 (0)