diff --git a/src/operations/indexes.ts b/src/operations/indexes.ts index 6d9bd2fa15..30a05494e2 100644 --- a/src/operations/indexes.ts +++ b/src/operations/indexes.ts @@ -35,6 +35,7 @@ const VALID_INDEX_OPTIONS = new Set([ 'expireAfterSeconds', 'storageEngine', 'collation', + 'version', // text indexes 'weights', @@ -70,7 +71,28 @@ export type IndexSpecification = OneOrMore< >; /** @public */ -export interface IndexDescription { +export interface IndexDescription + extends Pick< + CreateIndexesOptions, + | 'background' + | 'unique' + | 'partialFilterExpression' + | 'sparse' + | 'hidden' + | 'expireAfterSeconds' + | 'storageEngine' + | 'version' + | 'weights' + | 'default_language' + | 'language_override' + | 'textIndexVersion' + | '2dsphereIndexVersion' + | 'bits' + | 'min' + | 'max' + | 'bucketSize' + | 'wildcardProjection' + > { collation?: CollationOptions; name?: string; key: Document; @@ -90,9 +112,12 @@ export interface CreateIndexesOptions extends CommandOperationOptions { sparse?: boolean; /** Allows you to expire data on indexes applied to a data (MongoDB 2.2 or higher) */ expireAfterSeconds?: number; + /** Allows users to configure the storage engine on a per-index basis when creating an index. (MongoDB 3.0 or higher) */ storageEngine?: Document; /** (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. */ commitQuorum?: number | string; + /** Specifies the index version number, either 0 or 1. */ + version?: number; // text indexes weights?: Document; default_language?: string; @@ -110,6 +135,8 @@ export interface CreateIndexesOptions extends CommandOperationOptions { bucketSize?: number; // wildcard indexes wildcardProjection?: Document; + /** 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) */ + hidden?: boolean; } function makeIndexSpec(indexSpec: IndexSpecification, options: any): IndexDescription { diff --git a/test/types/index_options.test-d.ts b/test/types/index_options.test-d.ts new file mode 100644 index 0000000000..717b0430f5 --- /dev/null +++ b/test/types/index_options.test-d.ts @@ -0,0 +1,25 @@ +import { expectAssignable, expectNotAssignable } from 'tsd'; +import type { IndexDescription } from '../../src'; + +// test that all valid index options are allowed in IndexDescription +expectAssignable({ key: {}, background: true }); +expectAssignable({ key: {}, expireAfterSeconds: 2400 }); +expectAssignable({ key: {}, name: 'index_1' }); +expectAssignable({ key: {}, sparse: true }); +expectAssignable({ key: {}, storageEngine: {} }); +expectAssignable({ key: {}, unique: true }); +expectAssignable({ key: {}, version: 1 }); +expectAssignable({ key: {}, default_language: 'english' }); +expectAssignable({ key: {}, language_override: 'english' }); +expectAssignable({ key: {}, textIndexVersion: 2 }); +expectAssignable({ key: {}, weights: {} }); +expectAssignable({ key: {}, '2dsphereIndexVersion': 2 }); +expectAssignable({ key: {}, bits: 1 }); +expectAssignable({ key: {}, max: 1.1 }); +expectAssignable({ key: {}, min: 9.9 }); +expectAssignable({ key: {}, bucketSize: 100 }); +expectAssignable({ key: {}, partialFilterExpression: {} }); +expectAssignable({ key: {}, collation: { locale: 'en' } }); +expectAssignable({ key: {}, wildcardProjection: {} }); +expectAssignable({ key: {}, hidden: true }); +expectNotAssignable({ key: {}, invalidOption: 2400 }); diff --git a/test/types/mongodb.test-d.ts b/test/types/mongodb.test-d.ts index 9bd8c54e70..6f62ec5c3b 100644 --- a/test/types/mongodb.test-d.ts +++ b/test/types/mongodb.test-d.ts @@ -1,5 +1,4 @@ import { expectType, expectDeprecated } from 'tsd'; - import { MongoClient } from '../../src/mongo_client'; import { Collection } from '../../src/collection'; import { AggregationCursor } from '../../src/cursor/aggregation_cursor';