Skip to content

Commit 5be3eec

Browse files
authored
Merge pull request #15475 from Automattic/8.16
8.16
2 parents 064cfe6 + 59abae9 commit 5be3eec

File tree

4 files changed

+67
-9
lines changed

4 files changed

+67
-9
lines changed

lib/model.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,11 +1099,7 @@ Model.init = function init() {
10991099
return;
11001100
}
11011101

1102-
const results = [];
1103-
for (const searchIndex of this.schema._searchIndexes) {
1104-
results.push(await this.createSearchIndex(searchIndex));
1105-
}
1106-
return results;
1102+
return await this.ensureSearchIndexes();
11071103
};
11081104
const _createCollection = async() => {
11091105
let autoCreate = utils.getOption(
@@ -1792,6 +1788,35 @@ function _ensureIndexes(model, options, callback) {
17921788
}
17931789
}
17941790

1791+
/**
1792+
* Creates all [Atlas search indexes](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) defined in this model's schema.
1793+
* This function only works when connected to MongoDB Atlas.
1794+
*
1795+
* #### Example:
1796+
*
1797+
* const schema = new Schema({
1798+
* name: String,
1799+
* description: String
1800+
* });
1801+
* schema.searchIndex({ name: 'test', definition: { mappings: { dynamic: true } } });
1802+
* const Product = mongoose.model('Product', schema);
1803+
*
1804+
* // Creates the search index defined in the schema
1805+
* await Product.createSearchIndexes();
1806+
*
1807+
* @api public
1808+
* @return {Promise} resolves to the results of creating the search indexes
1809+
*/
1810+
1811+
Model.createSearchIndexes = async function createSearchIndexes() {
1812+
_checkContext(this, 'createSearchIndexes');
1813+
const results = [];
1814+
for (const searchIndex of this.schema._searchIndexes) {
1815+
results.push(await this.createSearchIndex(searchIndex));
1816+
}
1817+
return results;
1818+
};
1819+
17951820
/**
17961821
* Schema the model uses.
17971822
*

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
"type": "commonjs",
2121
"license": "MIT",
2222
"dependencies": {
23-
"bson": "^6.10.3",
23+
"bson": "^6.10.4",
2424
"kareem": "2.6.3",
25-
"mongodb": "~6.16.0",
25+
"mongodb": "~6.17.0",
2626
"mpath": "0.9.0",
2727
"mquery": "5.0.0",
2828
"ms": "2.1.3",

test/model.test.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
/**
44
* Test dependencies.
55
*/
6-
const sinon = require('sinon');
76
const start = require('./common');
87

98
const CastError = require('../lib/error/cast');
109
const assert = require('assert');
10+
const model = require('../lib/model');
1111
const { once } = require('events');
1212
const random = require('./util').random;
1313
const util = require('./util');
14-
const model = require('../lib/model');
14+
const sinon = require('sinon');
1515

1616
const mongoose = start.mongoose;
1717
const Schema = mongoose.Schema;
@@ -8686,6 +8686,33 @@ describe('Model', function() {
86868686
assert.equal(doc.name, undefined);
86878687
});
86888688
});
8689+
8690+
it('createSearchIndexes creates an index for each search index in schema (gh-15465)', async function() {
8691+
const sinon = require('sinon');
8692+
const schema = new mongoose.Schema({
8693+
name: String,
8694+
description: String
8695+
});
8696+
8697+
schema.searchIndex({ name: 'test', definition: { mappings: { dynamic: true } } });
8698+
8699+
const TestModel = db.model('Test', schema);
8700+
8701+
const createSearchIndexStub = sinon.stub(TestModel, 'createSearchIndex').resolves({ acknowledged: true });
8702+
8703+
try {
8704+
const results = await TestModel.createSearchIndexes();
8705+
8706+
assert.equal(createSearchIndexStub.callCount, 1);
8707+
assert.equal(results.length, 1);
8708+
assert.deepEqual(results, [{ acknowledged: true }]);
8709+
8710+
// Verify that createSearchIndex was called with the correct arguments
8711+
assert.ok(createSearchIndexStub.firstCall.calledWithMatch({ name: 'test', definition: { mappings: { dynamic: true } } }));
8712+
} finally {
8713+
sinon.restore();
8714+
}
8715+
});
86898716
});
86908717

86918718

types/models.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,12 @@ declare module 'mongoose' {
356356
*/
357357
createSearchIndex(description: SearchIndexDescription): Promise<string>;
358358

359+
/**
360+
* Creates all [Atlas search indexes](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) defined in this model's schema.
361+
* This function only works when connected to MongoDB Atlas.
362+
*/
363+
createSearchIndexes(): Promise<string[]>;
364+
359365
/** Connection the model uses. */
360366
db: Connection;
361367

0 commit comments

Comments
 (0)