Skip to content

Commit 64ee884

Browse files
committed
docs: add documentarraypath to API docs, including DocumentArrayPath#discriminator()
Fix #8164
1 parent 0291ffa commit 64ee884

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
lines changed

docs/source/api.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const files = [
2424
'lib/virtualtype.js',
2525
'lib/error/index.js',
2626
'lib/types/core_array.js',
27+
'lib/schema/documentarray.js',
2728
'lib/schema/SingleNestedPath.js',
2829
'lib/options/SchemaTypeOptions.js',
2930
'lib/options/SchemaArrayOptions.js',
@@ -63,6 +64,9 @@ function parse() {
6364
if (name === 'core_array') {
6465
name = 'array';
6566
}
67+
if (name === 'documentarray') {
68+
name = 'DocumentArrayPath';
69+
}
6670
const data = {
6771
name: name.charAt(0).toUpperCase() === name.charAt(0) ? name : name.charAt(0).toUpperCase() + name.substr(1),
6872
props: []

lib/model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,7 @@ Model.exists = function exists(filter, options, callback) {
10661066
*
10671067
* @param {String} name discriminator model name
10681068
* @param {Schema} schema discriminator model schema
1069-
* @param {String} value the string stored in the `discriminatorKey` property
1069+
* @param {String} [value] the string stored in the `discriminatorKey` property. If not specified, Mongoose uses the `name` parameter.
10701070
* @api public
10711071
*/
10721072

lib/schema/SingleNestedPath.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,17 +274,18 @@ SingleNestedPath.prototype.doValidateSync = function(value, scope, options) {
274274
* const shapeSchema = Schema({ name: String }, { discriminatorKey: 'kind' });
275275
* const schema = Schema({ shape: shapeSchema });
276276
*
277-
* const singleNestedPath = parentSchema.path('child');
277+
* const singleNestedPath = parentSchema.path('shape');
278278
* singleNestedPath.discriminator('Circle', Schema({ radius: Number }));
279279
*
280280
* @param {String} name
281281
* @param {Schema} schema fields to add to the schema for instances of this sub-class
282+
* @param {String} [value] the string stored in the `discriminatorKey` property. If not specified, Mongoose uses the `name` parameter.
282283
* @see discriminators /docs/discriminators.html
283284
* @api public
284285
*/
285286

286-
SingleNestedPath.prototype.discriminator = function(name, schema, tiedValue) {
287-
discriminator(this.caster, name, schema, tiedValue);
287+
SingleNestedPath.prototype.discriminator = function(name, schema, value) {
288+
discriminator(this.caster, name, schema, value);
288289

289290
this.caster.discriminators[name] = _createConstructor(schema, this.caster);
290291

lib/schema/documentarray.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ let Subdocument;
2929
* @api public
3030
*/
3131

32-
function DocumentArray(key, schema, options, schemaOptions) {
32+
function DocumentArrayPath(key, schema, options, schemaOptions) {
3333
const EmbeddedDocument = _createConstructor(schema, options);
3434
EmbeddedDocument.prototype.$basePath = key;
3535

@@ -62,24 +62,23 @@ function DocumentArray(key, schema, options, schemaOptions) {
6262
*
6363
* @api public
6464
*/
65-
DocumentArray.schemaName = 'DocumentArray';
65+
DocumentArrayPath.schemaName = 'DocumentArray';
6666

6767
/**
6868
* Options for all document arrays.
6969
*
7070
* - `castNonArrays`: `true` by default. If `false`, Mongoose will throw a CastError when a value isn't an array. If `true`, Mongoose will wrap the provided value in an array before casting.
7171
*
72-
* @static options
7372
* @api public
7473
*/
7574

76-
DocumentArray.options = { castNonArrays: true };
75+
DocumentArrayPath.options = { castNonArrays: true };
7776

7877
/*!
7978
* Inherits from ArrayType.
8079
*/
81-
DocumentArray.prototype = Object.create(ArrayType.prototype);
82-
DocumentArray.prototype.constructor = DocumentArray;
80+
DocumentArrayPath.prototype = Object.create(ArrayType.prototype);
81+
DocumentArrayPath.prototype.constructor = DocumentArrayPath;
8382

8483
/*!
8584
* Ignore
@@ -122,11 +121,24 @@ function _createConstructor(schema, options, baseClass) {
122121
return EmbeddedDocument;
123122
}
124123

125-
/*!
126-
* Ignore
124+
/**
125+
* Adds a discriminator to this document array.
126+
*
127+
* ####Example:
128+
* const shapeSchema = Schema({ name: String }, { discriminatorKey: 'kind' });
129+
* const schema = Schema({ shapes: [shapeSchema] });
130+
*
131+
* const docArrayPath = parentSchema.path('shapes');
132+
* docArrayPath.discriminator('Circle', Schema({ radius: Number }));
133+
*
134+
* @param {String} name
135+
* @param {Schema} schema fields to add to the schema for instances of this sub-class
136+
* @param {String} [value] the string stored in the `discriminatorKey` property. If not specified, Mongoose uses the `name` parameter.
137+
* @see discriminators /docs/discriminators.html
138+
* @api public
127139
*/
128140

129-
DocumentArray.prototype.discriminator = function(name, schema, tiedValue) {
141+
DocumentArrayPath.prototype.discriminator = function(name, schema, tiedValue) {
130142
if (typeof name === 'function') {
131143
name = utils.getFunctionName(name);
132144
}
@@ -155,7 +167,7 @@ DocumentArray.prototype.discriminator = function(name, schema, tiedValue) {
155167
* @api private
156168
*/
157169

158-
DocumentArray.prototype.doValidate = function(array, fn, scope, options) {
170+
DocumentArrayPath.prototype.doValidate = function(array, fn, scope, options) {
159171
// lazy load
160172
MongooseDocumentArray || (MongooseDocumentArray = require('../types/documentarray'));
161173

@@ -231,7 +243,7 @@ DocumentArray.prototype.doValidate = function(array, fn, scope, options) {
231243
* @api private
232244
*/
233245

234-
DocumentArray.prototype.doValidateSync = function(array, scope) {
246+
DocumentArrayPath.prototype.doValidateSync = function(array, scope) {
235247
const schemaTypeError = SchemaType.prototype.doValidateSync.call(this, array, scope);
236248
if (schemaTypeError != null) {
237249
schemaTypeError.$isArrayValidatorError = true;
@@ -277,7 +289,7 @@ DocumentArray.prototype.doValidateSync = function(array, scope) {
277289
* ignore
278290
*/
279291

280-
DocumentArray.prototype.getDefault = function(scope) {
292+
DocumentArrayPath.prototype.getDefault = function(scope) {
281293
let ret = typeof this.defaultValue === 'function'
282294
? this.defaultValue.call(scope)
283295
: this.defaultValue;
@@ -316,7 +328,7 @@ DocumentArray.prototype.getDefault = function(scope) {
316328
* @api private
317329
*/
318330

319-
DocumentArray.prototype.cast = function(value, doc, init, prev, options) {
331+
DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) {
320332
// lazy load
321333
MongooseDocumentArray || (MongooseDocumentArray = require('../types/documentarray'));
322334

@@ -326,7 +338,7 @@ DocumentArray.prototype.cast = function(value, doc, init, prev, options) {
326338
const _opts = { transform: false, virtuals: false };
327339

328340
if (!Array.isArray(value)) {
329-
if (!init && !DocumentArray.options.castNonArrays) {
341+
if (!init && !DocumentArrayPath.options.castNonArrays) {
330342
throw new CastError('DocumentArray', util.inspect(value), this.path);
331343
}
332344
// gh-2442 mark whole array as modified if we're initializing a doc from
@@ -416,7 +428,7 @@ DocumentArray.prototype.cast = function(value, doc, init, prev, options) {
416428
* ignore
417429
*/
418430

419-
DocumentArray.prototype.clone = function() {
431+
DocumentArrayPath.prototype.clone = function() {
420432
const options = Object.assign({}, this.options);
421433
const schematype = new this.constructor(this.path, this.schema, options, this.schemaOptions);
422434
schematype.validators = this.validators.slice();
@@ -429,7 +441,7 @@ DocumentArray.prototype.clone = function() {
429441
* Scopes paths selected in a query to this array.
430442
* Necessary for proper default application of subdocument values.
431443
*
432-
* @param {DocumentArray} array - the array to scope `fields` paths
444+
* @param {DocumentArrayPath} array - the array to scope `fields` paths
433445
* @param {Object|undefined} fields - the root fields selected in the query
434446
* @param {Boolean|undefined} init - if we are being created part of a query result
435447
*/
@@ -469,4 +481,4 @@ function scopePaths(array, fields, init) {
469481
* Module exports.
470482
*/
471483

472-
module.exports = DocumentArray;
484+
module.exports = DocumentArrayPath;

0 commit comments

Comments
 (0)