From df9db3c160ec625a79c3d9d830427ef7d7c04ef3 Mon Sep 17 00:00:00 2001 From: Mike Noseworthy Date: Tue, 19 Mar 2024 18:48:50 -0230 Subject: [PATCH 1/2] types(schema): add missing search index types Support for MongoDB Atlas search indexes was added in [#14251](https://github.com/Automattic/mongoose/pull/14251) but the types for `Schema.searchIndex` and the `autoSearchIndex` option were missed. Add these types so that the typescript definitions match the capabilities of the JavaScript version of `mongoose`. --- types/index.d.ts | 7 +++++++ types/schemaoptions.d.ts | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/types/index.d.ts b/types/index.d.ts index 6a5a926c654..54803b9f20d 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -274,6 +274,13 @@ declare module 'mongoose' { /** Defines an index (most likely compound) for this schema. */ index(fields: IndexDefinition, options?: IndexOptions): this; + /** + * Define a search index for this schema. + * + * @remarks Search indexes are only supported when used against a 7.0+ Mongo Atlas cluster. + */ + searchIndex(description: mongodb.SearchIndexDescription): Promise; + /** * Returns a list of indexes that this schema declares, via `schema.index()` * or by `index: true` in a path's options. diff --git a/types/schemaoptions.d.ts b/types/schemaoptions.d.ts index abb48a32cc8..32a9e909e38 100644 --- a/types/schemaoptions.d.ts +++ b/types/schemaoptions.d.ts @@ -24,6 +24,11 @@ declare module 'mongoose' { * automatic index builds, you can set autoIndex to false. */ autoIndex?: boolean; + /** + * Similar to autoIndex, except for automatically creates any Atlas search indexes defined in your + * schema. Unlike autoIndex, this option defaults to false. + */ + autoSearchIndex?: boolean; /** * If set to `true`, Mongoose will call Model.createCollection() to create the underlying collection * in MongoDB if autoCreate is set to true. Calling createCollection() sets the collection's default From aea12a1a9095f4042a3648b5f8f3c471f4cbcbb0 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 20 Mar 2024 16:31:50 -0400 Subject: [PATCH 2/2] Update index.d.ts --- types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index 54803b9f20d..b3baf6c8a3e 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -279,7 +279,7 @@ declare module 'mongoose' { * * @remarks Search indexes are only supported when used against a 7.0+ Mongo Atlas cluster. */ - searchIndex(description: mongodb.SearchIndexDescription): Promise; + searchIndex(description: mongodb.SearchIndexDescription): this; /** * Returns a list of indexes that this schema declares, via `schema.index()`