Skip to content
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

fix(NODE-3441): fix typings for createIndexes #2915

Merged
merged 2 commits into from Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/operations/indexes.ts
Expand Up @@ -35,6 +35,7 @@ const VALID_INDEX_OPTIONS = new Set([
'expireAfterSeconds',
'storageEngine',
'collation',
'version',

// text indexes
'weights',
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions 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<IndexDescription>({ key: {}, background: true });
expectAssignable<IndexDescription>({ key: {}, expireAfterSeconds: 2400 });
expectAssignable<IndexDescription>({ key: {}, name: 'index_1' });
expectAssignable<IndexDescription>({ key: {}, sparse: true });
expectAssignable<IndexDescription>({ key: {}, storageEngine: {} });
expectAssignable<IndexDescription>({ key: {}, unique: true });
expectAssignable<IndexDescription>({ key: {}, version: 1 });
expectAssignable<IndexDescription>({ key: {}, default_language: 'english' });
expectAssignable<IndexDescription>({ key: {}, language_override: 'english' });
expectAssignable<IndexDescription>({ key: {}, textIndexVersion: 2 });
expectAssignable<IndexDescription>({ key: {}, weights: {} });
expectAssignable<IndexDescription>({ key: {}, '2dsphereIndexVersion': 2 });
expectAssignable<IndexDescription>({ key: {}, bits: 1 });
expectAssignable<IndexDescription>({ key: {}, max: 1.1 });
expectAssignable<IndexDescription>({ key: {}, min: 9.9 });
expectAssignable<IndexDescription>({ key: {}, bucketSize: 100 });
expectAssignable<IndexDescription>({ key: {}, partialFilterExpression: {} });
expectAssignable<IndexDescription>({ key: {}, collation: { locale: 'en' } });
expectAssignable<IndexDescription>({ key: {}, wildcardProjection: {} });
expectAssignable<IndexDescription>({ key: {}, hidden: true });
expectNotAssignable<IndexDescription>({ key: {}, invalidOption: 2400 });
1 change: 0 additions & 1 deletion 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';
Expand Down