Skip to content

Commit

Permalink
Merge pull request #13850 from Automattic/vkarpov15/gh-13771
Browse files Browse the repository at this point in the history
docs(model): add examples of using `diffIndexes()` to `syncIndexes()`and `diffIndexes()` api docs
  • Loading branch information
vkarpov15 authored Sep 12, 2023
2 parents 91bd797 + e14e049 commit c27500b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
20 changes: 18 additions & 2 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,17 @@ Model.createCollection = async function createCollection(options) {
* // Will drop the 'age' index and create an index on `name`
* await Customer.syncIndexes();
*
* You should be careful about running `syncIndexes()` on production applications under heavy load,
* because index builds are expensive operations, and unexpected index drops can lead to degraded
* performance. Before running `syncIndexes()`, you can use the [`diffIndexes()` function](#Model.diffIndexes())
* to check what indexes `syncIndexes()` will drop and create.
*
* #### Example:
*
* const { toDrop, toCreate } = await Model.diffIndexes();
* toDrop; // Array of strings containing names of indexes that `syncIndexes()` will drop
* toCreate; // Array of strings containing names of indexes that `syncIndexes()` will create
*
* @param {Object} [options] options to pass to `ensureIndexes()`
* @param {Boolean} [options.background=null] if specified, overrides each index's `background` property
* @return {Promise}
Expand Down Expand Up @@ -1483,9 +1494,14 @@ Model.syncIndexes = async function syncIndexes(options) {
/**
* Does a dry-run of `Model.syncIndexes()`, returning the indexes that `syncIndexes()` would drop and create if you were to run `syncIndexes()`.
*
* #### Example:
*
* const { toDrop, toCreate } = await Model.diffIndexes();
* toDrop; // Array of strings containing names of indexes that `syncIndexes()` will drop
* toCreate; // Array of strings containing names of indexes that `syncIndexes()` will create
*
* @param {Object} [options]
* @returns {Promise} which contains an object, {toDrop, toCreate}, which
* are indexes that would be dropped in MongoDB and indexes that would be created in MongoDB.
* @return {Promise<Object>} contains the indexes that would be dropped in MongoDB and indexes that would be created in MongoDB as `{ toDrop: string[], toCreate: string[] }`.
*/

Model.diffIndexes = async function diffIndexes() {
Expand Down
2 changes: 1 addition & 1 deletion test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1217,4 +1217,4 @@ function gh13800() {
expectType<string>(this.lastName);
expectError<string>(this.someOtherField);
});
}
}

0 comments on commit c27500b

Please sign in to comment.