Skip to content

Commit

Permalink
feat(model): add updateSearchIndex and dropSearchIndex helpers re: #1…
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jan 11, 2024
1 parent 6bd415b commit a76a99a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lib/model.js
Expand Up @@ -1529,6 +1529,50 @@ Model.createSearchIndex = async function createSearchIndex(description) {
return await this.$__collection.createSearchIndex(description);
};

/**
* Update an existing [Atlas search index](https://www.mongodb.com/docs/atlas/atlas-search/create-index/).
* This function only works when connected to MongoDB Atlas.
*
* #### Example:
*
* const schema = new Schema({ name: { type: String, unique: true } });
* const Customer = mongoose.model('Customer', schema);
* await Customer.updateSearchIndex('test', { mappings: { dynamic: true } });
*
* @param {String} name
* @param {Object} definition
* @return {Promise}
* @api public
*/

Model.updateSearchIndex = async function updateSearchIndex(name, definition) {
_checkContext(this, 'updateSearchIndex');

return await this.$__collection.updateSearchIndex(name, definition);
};

/**
* Delete an existing [Atlas search index](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) by name.
* This function only works when connected to MongoDB Atlas.
*
* #### Example:
*
* const schema = new Schema({ name: { type: String, unique: true } });
* const Customer = mongoose.model('Customer', schema);
* await Customer.dropSearchIndex('test');
*
* @param {String} name
* @param {Object} definition
* @return {Promise}
* @api public
*/

Model.dropSearchIndex = async function dropSearchIndex(name) {
_checkContext(this, 'dropSearchIndex');

return await this.$__collection.dropSearchIndex(name);
};

/**
* Does a dry-run of `Model.syncIndexes()`, returning the indexes that `syncIndexes()` would drop and create if you were to run `syncIndexes()`.
*
Expand Down
4 changes: 4 additions & 0 deletions types/models.d.ts
Expand Up @@ -300,6 +300,8 @@ declare module 'mongoose' {
'deleteOne'
>;

dropSearchIndex(name: string): Promise<void>;

/**
* Event emitter that reports any errors that occurred. Useful for global error
* handling.
Expand Down Expand Up @@ -475,6 +477,8 @@ declare module 'mongoose' {
doc: any, options: PopulateOptions | Array<PopulateOptions> | string
): Promise<MergeType<THydratedDocumentType, Paths>>;

updateSearchIndex(name: string, definition: AnyObject): Promise<void>;

/** Casts and validates the given object against this model's schema, passing the given `context` to custom validators. */
validate(): Promise<void>;
validate(obj: any): Promise<void>;
Expand Down

0 comments on commit a76a99a

Please sign in to comment.