Skip to content

Commit

Permalink
add TypeScript types, jsdoc, and options support for Model.listSearch…
Browse files Browse the repository at this point in the history
…Indexes()
  • Loading branch information
vkarpov15 committed Apr 16, 2024
1 parent 6605629 commit ec518ed
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1655,11 +1655,27 @@ Model.dropSearchIndex = async function dropSearchIndex(name) {
return await this.$__collection.dropSearchIndex(name);
};

/**
* List all [Atlas search indexes](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) on this model's collection.
* 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.createSearchIndex({ name: 'test', definition: { mappings: { dynamic: true } } });
* const res = await Customer.listSearchIndexes(); // Includes `[{ name: 'test' }]`
*
* @param {Object} [options]
* @return {Promise<Array>}
* @api public
*/

Model.listSearchIndexes = async function listSearchIndexes() {
Model.listSearchIndexes = async function listSearchIndexes(options) {
_checkContext(this, 'listSearchIndexes');

const cursor = await this.$__collection.listSearchIndexes();
const cursor = await this.$__collection.listSearchIndexes(options);

return await cursor.toArray();
};
Expand Down
6 changes: 6 additions & 0 deletions types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,12 @@ declare module 'mongoose' {
Array<MergeType<THydratedDocumentType, Omit<DocContents, '_id'>>>
>;

/**
* List all [Atlas search indexes](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) on this model's collection.
* This function only works when connected to MongoDB Atlas.
*/
listSearchIndexes(options?: mongodb.ListSearchIndexesOptions): Promise<Array<{ name: string }>>;

/** The name of the model */
modelName: string;

Expand Down

0 comments on commit ec518ed

Please sign in to comment.