Skip to content

feat(NODE-6044): add detailed types for SearchIndexDescription #4052

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/collection.ts
Original file line number Diff line number Diff line change
@@ -73,6 +73,7 @@ import {
CreateSearchIndexesOperation,
type SearchIndexDescription
} from './operations/search_indexes/create';
import type { SearchIndexDefinition } from './operations/search_indexes/definition';
import { DropSearchIndexOperation } from './operations/search_indexes/drop';
import { UpdateSearchIndexOperation } from './operations/search_indexes/update';
import {
@@ -1133,7 +1134,7 @@ export class Collection<TSchema extends Document = Document> {
*
* @remarks Only available when used against a 7.0+ Atlas cluster.
*/
async updateSearchIndex(name: string, definition: Document): Promise<void> {
async updateSearchIndex(name: string, definition: SearchIndexDefinition): Promise<void> {
return executeOperation(
this.client,
new UpdateSearchIndexOperation(this as TODO_NODE_3286, name, definition)
59 changes: 59 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -483,6 +483,65 @@ export type { RemoveUserOptions } from './operations/remove_user';
export type { RenameOptions } from './operations/rename';
export type { RunCommandOptions } from './operations/run_command';
export type { SearchIndexDescription } from './operations/search_indexes/create';
export type {
AsciiFoldingTokenFilter,
AutocompleteFieldMapping,
BooleanFieldMapping,
CharacterFilter,
CustomAnalyzer,
DaitchMokotoffSoundexTokenFilter,
DateFacetFieldMapping,
DateFieldMapping,
DocumentFieldMapping,
EdgeGramTokenFilter,
EdgeGramTokenizer,
EmbeddedDocumentFieldMapping,
EnglishPossessiveTokenFilter,
FieldMapping,
FlattenGraphTokenFilter,
GeoFieldMapping,
HtmlStripCharFilter,
IcuFoldingTokenFilter,
IcuNormalizeCharFilter,
IcuNormalizerTokenFilter,
KeywordTokenizer,
KnnVectorFieldMapping,
KStemmingTokenFilter,
LengthTokenFilter,
LowercaseTokenFilter,
MappingCharFilter,
NgramTokenFilter,
NGramTokenizer,
NumberFacetFieldMapping,
NumberFieldMapping,
ObjectIdFieldMapping,
PersianCharFilter,
PorterStemmingTokenFilter,
RegexCaptureGroupTokenizer,
RegexSplitTokenizer,
RegexTokenFilter,
ReverseTokenFilter,
SearchIndexDefinition,
ShingleTokenFilter,
SnowballStemmingTokenFilter,
SpanishPluralStemming,
StandardTokenizer,
StempelTokenFilter,
StopwordTokenFilter,
StoredSourceDefinition,
StoredSourceExcludeDefinition,
StoredSourceIncludeDefinition,
StringFacetFieldMapping,
StringFieldMapping,
SynonymMappingDefinition,
TokenFieldMapping,
TokenFilter,
Tokenizer,
TrimTokenFilter,
UaxUrlEmailTokenizer,
WhitespaceTokenizer,
WordDelimiterGraphTokenFilter
} from './operations/search_indexes/definition';
export type { SetProfilingLevelOptions } from './operations/set_profiling_level';
export type { DbStatsOptions } from './operations/stats';
export type {
5 changes: 2 additions & 3 deletions src/operations/search_indexes/create.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { Document } from 'bson';

import type { Collection } from '../../collection';
import type { Server } from '../../sdam/server';
import type { ClientSession } from '../../sessions';
import { AbstractOperation } from '../operation';
import type { SearchIndexDefinition } from './definition';

/**
* @public
@@ -13,7 +12,7 @@ export interface SearchIndexDescription {
name?: string;

/** The index definition. */
definition: Document;
definition: SearchIndexDefinition;
}

/** @internal */
480 changes: 480 additions & 0 deletions src/operations/search_indexes/definition.ts

Large diffs are not rendered by default.

Empty file.
5 changes: 2 additions & 3 deletions src/operations/search_indexes/update.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import type { Document } from 'bson';

import type { Collection } from '../../collection';
import type { Server } from '../../sdam/server';
import type { ClientSession } from '../../sessions';
import { AbstractOperation } from '../operation';
import type { SearchIndexDefinition } from './definition';

/** @internal */
export class UpdateSearchIndexOperation extends AbstractOperation<void> {
constructor(
private readonly collection: Collection,
private readonly name: string,
private readonly definition: Document
private readonly definition: SearchIndexDefinition
) {
super();
}
6 changes: 4 additions & 2 deletions test/integration/crud/abstract_operation.test.ts
Original file line number Diff line number Diff line change
@@ -231,7 +231,9 @@ describe('abstract operation', function () {
},
{
subclassCreator: () =>
new mongodb.CreateSearchIndexesOperation(collection, [{ definition: { a: 1 } }]),
new mongodb.CreateSearchIndexesOperation(collection, [
{ definition: { mappings: { dynamic: true } } }
]),
subclassType: mongodb.CreateSearchIndexesOperation,
correctCommandName: 'createSearchIndexes'
},
@@ -243,7 +245,7 @@ describe('abstract operation', function () {
{
subclassCreator: () =>
new mongodb.UpdateSearchIndexOperation(collection, 'dummyName', {
a: 1
mappings: { dynamic: true }
}),
subclassType: mongodb.UpdateSearchIndexOperation,
correctCommandName: 'updateSearchIndex'
96 changes: 96 additions & 0 deletions test/types/search_index_descriptions.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { expectAssignable, expectNotAssignable } from 'tsd';

import type { SearchIndexDescription } from '../../src';

// Test to ensure valid configurations of SearchIndexDescription are allowed
expectAssignable<SearchIndexDescription>({
name: 'mySearchIndex',
definition: {
mappings: {
dynamic: true
}
}
});

expectAssignable<SearchIndexDescription>({
definition: {
analyzer: 'standard',
searchAnalyzer: 'standard',
mappings: {
dynamic: false,
fields: {
title: { type: 'string' }
}
},
storedSource: true
}
});

expectAssignable<SearchIndexDescription>({
definition: {
analyzer: 'custom_analyzer',
analyzers: [
{
name: 'custom_analyzer',
tokenizer: { type: 'standard' }
}
],
mappings: {
dynamic: false,
fields: {
description: { type: 'string', analyzer: 'custom_analyzer' }
}
},
storedSource: {
include: ['title', 'description']
},
synonyms: [
{
analyzer: 'standard',
name: 'synonym_mapping',
source: {
collection: 'synonyms'
}
}
]
}
});

// Test to ensure configurations missing required `definition` are invalid
expectNotAssignable<SearchIndexDescription>({});
expectNotAssignable<SearchIndexDescription>({
name: 'incompleteDefinition'
});

// Test configurations that should not be assignable to SearchIndexDescription due to invalid `definition` structure
expectNotAssignable<SearchIndexDescription>({
name: 'invalidDefinition',
definition: {
mappings: {
dynamic: 'yes' // dynamic should be a boolean
}
}
});

// Test configurations with incorrect field types in `definition`
expectNotAssignable<SearchIndexDescription>({
definition: {
analyzer: 'standard',
mappings: {
dynamic: false,
fields: {
createdAt: { type: 'date', wrongOption: true } // 'wrongOption' is not a valid property for DateFieldMapping
}
}
}
});

// Ensure that `name` as other than string is caught
expectNotAssignable<SearchIndexDescription>({
name: 123, // name should be a string
definition: {
mappings: {
dynamic: true
}
}
});
25 changes: 25 additions & 0 deletions test/types/search_indexes_test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expectType } from 'tsd';

import { MongoClient } from '../../src';

const client = new MongoClient('');
const db = client.db('test');
const collection = db.collection('test.find');

// Promise variant testing
expectType<Promise<string>>(
collection.createSearchIndex({
name: 'test-index',
definition: { mappings: { dynamic: false, fields: { description: { type: 'string' } } } }
})
);

// Explicit check for iterable result
for (const indexName of await collection.createSearchIndexes([
{
name: 'test-index',
definition: { mappings: { dynamic: false, fields: { description: { type: 'string' } } } }
}
])) {
expectType<string>(indexName);
}