Skip to content

Commit c27500b

Browse files
authored
Merge pull request #13850 from Automattic/vkarpov15/gh-13771
docs(model): add examples of using `diffIndexes()` to `syncIndexes()`and `diffIndexes()` api docs
2 parents 91bd797 + e14e049 commit c27500b

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/model.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,17 @@ Model.createCollection = async function createCollection(options) {
14511451
* // Will drop the 'age' index and create an index on `name`
14521452
* await Customer.syncIndexes();
14531453
*
1454+
* You should be careful about running `syncIndexes()` on production applications under heavy load,
1455+
* because index builds are expensive operations, and unexpected index drops can lead to degraded
1456+
* performance. Before running `syncIndexes()`, you can use the [`diffIndexes()` function](#Model.diffIndexes())
1457+
* to check what indexes `syncIndexes()` will drop and create.
1458+
*
1459+
* #### Example:
1460+
*
1461+
* const { toDrop, toCreate } = await Model.diffIndexes();
1462+
* toDrop; // Array of strings containing names of indexes that `syncIndexes()` will drop
1463+
* toCreate; // Array of strings containing names of indexes that `syncIndexes()` will create
1464+
*
14541465
* @param {Object} [options] options to pass to `ensureIndexes()`
14551466
* @param {Boolean} [options.background=null] if specified, overrides each index's `background` property
14561467
* @return {Promise}
@@ -1483,9 +1494,14 @@ Model.syncIndexes = async function syncIndexes(options) {
14831494
/**
14841495
* Does a dry-run of `Model.syncIndexes()`, returning the indexes that `syncIndexes()` would drop and create if you were to run `syncIndexes()`.
14851496
*
1497+
* #### Example:
1498+
*
1499+
* const { toDrop, toCreate } = await Model.diffIndexes();
1500+
* toDrop; // Array of strings containing names of indexes that `syncIndexes()` will drop
1501+
* toCreate; // Array of strings containing names of indexes that `syncIndexes()` will create
1502+
*
14861503
* @param {Object} [options]
1487-
* @returns {Promise} which contains an object, {toDrop, toCreate}, which
1488-
* are indexes that would be dropped in MongoDB and indexes that would be created in MongoDB.
1504+
* @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[] }`.
14891505
*/
14901506

14911507
Model.diffIndexes = async function diffIndexes() {

test/types/schema.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1217,4 +1217,4 @@ function gh13800() {
12171217
expectType<string>(this.lastName);
12181218
expectError<string>(this.someOtherField);
12191219
});
1220-
}
1220+
}

0 commit comments

Comments
 (0)