Skip to content

Commit

Permalink
fix(model): support setting excludeIndexes as schema option for sub…
Browse files Browse the repository at this point in the history
…docs

Fix #8343
  • Loading branch information
vkarpov15 committed Nov 18, 2019
1 parent 4c150e0 commit 0bc3455
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/helpers/schema/getIndexes.js
Expand Up @@ -36,7 +36,8 @@ module.exports = function getIndexes(schema) {

if (path.$isMongooseDocumentArray || path.$isSingleNested) {
if (get(path, 'options.excludeIndexes') !== true &&
get(path, 'schemaOptions.excludeIndexes') !== true) {
get(path, 'schemaOptions.excludeIndexes') !== true &&
get(path, 'schema.options.excludeIndexes') !== true) {
collectIndexes(path.schema, prefix + key + '.');
}

Expand Down
48 changes: 48 additions & 0 deletions lib/options/SchemaDocumentArrayOptions.js
@@ -0,0 +1,48 @@
'use strict';

const SchemaTypeOptions = require('./SchemaTypeOptions');

/**
* The options defined on an Document Array schematype.
*
* ####Example:
*
* const schema = new Schema({ users: [{ name: string }] });
* schema.path('users').options; // SchemaDocumentArrayOptions instance
*
* @api public
* @inherits SchemaTypeOptions
* @constructor SchemaDocumentOptions
*/

class SchemaDocumentArrayOptions extends SchemaTypeOptions {}

const opts = require('./propertyOptions');

/**
* If `true`, Mongoose will skip building any indexes defined in this array's schema.
* If not set, Mongoose will build all indexes defined in this array's schema.
*
* ####Example:
*
* const childSchema = Schema({ name: { type: String, index: true } });
* // If `excludeIndexes` is `true`, Mongoose will skip building an index
* // on `arr.name`. Otherwise, Mongoose will build an index on `arr.name`.
* const parentSchema = Schema({
* arr: { type: [childSchema], excludeIndexes: true }
* });
*
* @api public
* @property excludeIndexes
* @memberOf SchemaDocumentArrayOptions
* @type Array
* @instance
*/

Object.defineProperty(SchemaDocumentArrayOptions.prototype, 'excludeIndexes', opts);

/*!
* ignore
*/

module.exports = SchemaArrayOptions;
3 changes: 3 additions & 0 deletions lib/schema.js
Expand Up @@ -72,6 +72,9 @@ let id = 0;
* - [timestamps](/docs/guide.html#timestamps): object or boolean - defaults to `false`. If true, Mongoose adds `createdAt` and `updatedAt` properties to your schema and manages those properties for you.
* - [storeSubdocValidationError](/docs/guide.html#storeSubdocValidationError): boolean - Defaults to true. If false, Mongoose will wrap validation errors in single nested document subpaths into a single validation error on the single nested subdoc's path.
*
* ####Options for Nested Schemas:
* - `excludeIndexes`: bool - defaults to `false`. If `true`, skip building indexes on this schema's paths.
*
* ####Note:
*
* _When nesting schemas, (`children` in the example above), always declare the child schema first before passing it into its parent._
Expand Down

0 comments on commit 0bc3455

Please sign in to comment.