array.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. 'use strict';
  2. /*!
  3. * Module dependencies.
  4. */
  5. const $exists = require('./operators/exists');
  6. const $type = require('./operators/type');
  7. const MongooseError = require('../error/mongooseError');
  8. const SchemaArrayOptions = require('../options/SchemaArrayOptions');
  9. const SchemaType = require('../schematype');
  10. const CastError = SchemaType.CastError;
  11. const Mixed = require('./mixed');
  12. const arrayDepth = require('../helpers/arrayDepth');
  13. const cast = require('../cast');
  14. const isOperator = require('../helpers/query/isOperator');
  15. const util = require('util');
  16. const utils = require('../utils');
  17. const castToNumber = require('./operators/helpers').castToNumber;
  18. const geospatial = require('./operators/geospatial');
  19. const getDiscriminatorByValue = require('../helpers/discriminator/getDiscriminatorByValue');
  20. let MongooseArray;
  21. let EmbeddedDoc;
  22. const isNestedArraySymbol = Symbol('mongoose#isNestedArray');
  23. const emptyOpts = Object.freeze({});
  24. /**
  25. * Array SchemaType constructor
  26. *
  27. * @param {String} key
  28. * @param {SchemaType} cast
  29. * @param {Object} options
  30. * @param {Object} schemaOptions
  31. * @inherits SchemaType
  32. * @api public
  33. */
  34. function SchemaArray(key, cast, options, schemaOptions) {
  35. // lazy load
  36. EmbeddedDoc || (EmbeddedDoc = require('../types').Embedded);
  37. let typeKey = 'type';
  38. if (schemaOptions && schemaOptions.typeKey) {
  39. typeKey = schemaOptions.typeKey;
  40. }
  41. this.schemaOptions = schemaOptions;
  42. if (cast) {
  43. let castOptions = {};
  44. if (utils.isPOJO(cast)) {
  45. if (cast[typeKey]) {
  46. // support { type: Woot }
  47. castOptions = utils.clone(cast); // do not alter user arguments
  48. delete castOptions[typeKey];
  49. cast = cast[typeKey];
  50. } else {
  51. cast = Mixed;
  52. }
  53. }
  54. if (options != null && options.ref != null && castOptions.ref == null) {
  55. castOptions.ref = options.ref;
  56. }
  57. if (cast === Object) {
  58. cast = Mixed;
  59. }
  60. // support { type: 'String' }
  61. const name = typeof cast === 'string'
  62. ? cast
  63. : utils.getFunctionName(cast);
  64. const Types = require('./index.js');
  65. const caster = Types.hasOwnProperty(name) ? Types[name] : cast;
  66. this.casterConstructor = caster;
  67. if (this.casterConstructor instanceof SchemaArray) {
  68. this.casterConstructor[isNestedArraySymbol] = true;
  69. }
  70. if (typeof caster === 'function' &&
  71. !caster.$isArraySubdocument &&
  72. !caster.$isSchemaMap) {
  73. const path = this.caster instanceof EmbeddedDoc ? null : key;
  74. this.caster = new caster(path, castOptions);
  75. } else {
  76. this.caster = caster;
  77. if (!(this.caster instanceof EmbeddedDoc)) {
  78. this.caster.path = key;
  79. }
  80. }
  81. this.$embeddedSchemaType = this.caster;
  82. }
  83. this.$isMongooseArray = true;
  84. SchemaType.call(this, key, options, 'Array');
  85. let defaultArr;
  86. let fn;
  87. if (this.defaultValue != null) {
  88. defaultArr = this.defaultValue;
  89. fn = typeof defaultArr === 'function';
  90. }
  91. if (!('defaultValue' in this) || this.defaultValue !== void 0) {
  92. const defaultFn = function() {
  93. // Leave it up to `cast()` to convert the array
  94. return fn
  95. ? defaultArr.call(this)
  96. : defaultArr != null
  97. ? [].concat(defaultArr)
  98. : [];
  99. };
  100. defaultFn.$runBeforeSetters = !fn;
  101. this.default(defaultFn);
  102. }
  103. }
  104. /**
  105. * This schema type's name, to defend against minifiers that mangle
  106. * function names.
  107. *
  108. * @api public
  109. */
  110. SchemaArray.schemaName = 'Array';
  111. /**
  112. * Options for all arrays.
  113. *
  114. * - `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.
  115. *
  116. * @static options
  117. * @api public
  118. */
  119. SchemaArray.options = { castNonArrays: true };
  120. /*!
  121. * ignore
  122. */
  123. SchemaArray.defaultOptions = {};
  124. /**
  125. * Sets a default option for all Array instances.
  126. *
  127. * #### Example:
  128. *
  129. * // Make all Array instances have `required` of true by default.
  130. * mongoose.Schema.Array.set('required', true);
  131. *
  132. * const User = mongoose.model('User', new Schema({ test: Array }));
  133. * new User({ }).validateSync().errors.test.message; // Path `test` is required.
  134. *
  135. * @param {String} option - The option you'd like to set the value for
  136. * @param {*} value - value for option
  137. * @return {undefined}
  138. * @function set
  139. * @api public
  140. */
  141. SchemaArray.set = SchemaType.set;
  142. /*!
  143. * Inherits from SchemaType.
  144. */
  145. SchemaArray.prototype = Object.create(SchemaType.prototype);
  146. SchemaArray.prototype.constructor = SchemaArray;
  147. SchemaArray.prototype.OptionsConstructor = SchemaArrayOptions;
  148. /*!
  149. * ignore
  150. */
  151. SchemaArray._checkRequired = SchemaType.prototype.checkRequired;
  152. /**
  153. * Override the function the required validator uses to check whether an array
  154. * passes the `required` check.
  155. *
  156. * #### Example:
  157. *
  158. * // Require non-empty array to pass `required` check
  159. * mongoose.Schema.Types.Array.checkRequired(v => Array.isArray(v) && v.length);
  160. *
  161. * const M = mongoose.model({ arr: { type: Array, required: true } });
  162. * new M({ arr: [] }).validateSync(); // `null`, validation fails!
  163. *
  164. * @param {Function} fn
  165. * @return {Function}
  166. * @function checkRequired
  167. * @api public
  168. */
  169. SchemaArray.checkRequired = SchemaType.checkRequired;
  170. /**
  171. * Check if the given value satisfies the `required` validator.
  172. *
  173. * @param {Any} value
  174. * @param {Document} doc
  175. * @return {Boolean}
  176. * @api public
  177. */
  178. SchemaArray.prototype.checkRequired = function checkRequired(value, doc) {
  179. if (typeof value === 'object' && SchemaType._isRef(this, value, doc, true)) {
  180. return !!value;
  181. }
  182. // `require('util').inherits()` does **not** copy static properties, and
  183. // plugins like mongoose-float use `inherits()` for pre-ES6.
  184. const _checkRequired = typeof this.constructor.checkRequired === 'function' ?
  185. this.constructor.checkRequired() :
  186. SchemaArray.checkRequired();
  187. return _checkRequired(value);
  188. };
  189. /**
  190. * Adds an enum validator if this is an array of strings or numbers. Equivalent to
  191. * `SchemaString.prototype.enum()` or `SchemaNumber.prototype.enum()`
  192. *
  193. * @param {String|Object} [args...] enumeration values
  194. * @return {SchemaArray} this
  195. */
  196. SchemaArray.prototype.enum = function() {
  197. let arr = this;
  198. while (true) {
  199. const instance = arr &&
  200. arr.caster &&
  201. arr.caster.instance;
  202. if (instance === 'Array') {
  203. arr = arr.caster;
  204. continue;
  205. }
  206. if (instance !== 'String' && instance !== 'Number') {
  207. throw new Error('`enum` can only be set on an array of strings or numbers ' +
  208. ', not ' + instance);
  209. }
  210. break;
  211. }
  212. let enumArray = arguments;
  213. if (!Array.isArray(arguments) && utils.isObject(arguments)) {
  214. enumArray = utils.object.vals(enumArray);
  215. }
  216. arr.caster.enum.apply(arr.caster, enumArray);
  217. return this;
  218. };
  219. /**
  220. * Overrides the getters application for the population special-case
  221. *
  222. * @param {Object} value
  223. * @param {Object} scope
  224. * @api private
  225. */
  226. SchemaArray.prototype.applyGetters = function(value, scope) {
  227. if (scope != null && scope.$__ != null && scope.$populated(this.path)) {
  228. // means the object id was populated
  229. return value;
  230. }
  231. const ret = SchemaType.prototype.applyGetters.call(this, value, scope);
  232. if (Array.isArray(ret)) {
  233. const rawValue = utils.isMongooseArray(ret) ? ret.__array : ret;
  234. const len = rawValue.length;
  235. for (let i = 0; i < len; ++i) {
  236. rawValue[i] = this.caster.applyGetters(rawValue[i], scope);
  237. }
  238. }
  239. return ret;
  240. };
  241. SchemaArray.prototype._applySetters = function(value, scope, init, priorVal) {
  242. if (this.casterConstructor.$isMongooseArray &&
  243. SchemaArray.options.castNonArrays &&
  244. !this[isNestedArraySymbol]) {
  245. // Check nesting levels and wrap in array if necessary
  246. let depth = 0;
  247. let arr = this;
  248. while (arr != null &&
  249. arr.$isMongooseArray &&
  250. !arr.$isMongooseDocumentArray) {
  251. ++depth;
  252. arr = arr.casterConstructor;
  253. }
  254. // No need to wrap empty arrays
  255. if (value != null && value.length !== 0) {
  256. const valueDepth = arrayDepth(value);
  257. if (valueDepth.min === valueDepth.max && valueDepth.max < depth && valueDepth.containsNonArrayItem) {
  258. for (let i = valueDepth.max; i < depth; ++i) {
  259. value = [value];
  260. }
  261. }
  262. }
  263. }
  264. return SchemaType.prototype._applySetters.call(this, value, scope, init, priorVal);
  265. };
  266. /**
  267. * Casts values for set().
  268. *
  269. * @param {Object} value
  270. * @param {Document} doc document that triggers the casting
  271. * @param {Boolean} init whether this is an initialization cast
  272. * @api private
  273. */
  274. SchemaArray.prototype.cast = function(value, doc, init, prev, options) {
  275. // lazy load
  276. MongooseArray || (MongooseArray = require('../types').Array);
  277. let i;
  278. let l;
  279. if (Array.isArray(value)) {
  280. const len = value.length;
  281. if (!len && doc) {
  282. const indexes = doc.schema.indexedPaths();
  283. const arrayPath = this.path;
  284. for (i = 0, l = indexes.length; i < l; ++i) {
  285. const pathIndex = indexes[i][0][arrayPath];
  286. if (pathIndex === '2dsphere' || pathIndex === '2d') {
  287. return;
  288. }
  289. }
  290. // Special case: if this index is on the parent of what looks like
  291. // GeoJSON, skip setting the default to empty array re: #1668, #3233
  292. const arrayGeojsonPath = this.path.endsWith('.coordinates') ?
  293. this.path.substring(0, this.path.lastIndexOf('.')) : null;
  294. if (arrayGeojsonPath != null) {
  295. for (i = 0, l = indexes.length; i < l; ++i) {
  296. const pathIndex = indexes[i][0][arrayGeojsonPath];
  297. if (pathIndex === '2dsphere') {
  298. return;
  299. }
  300. }
  301. }
  302. }
  303. options = options || emptyOpts;
  304. let rawValue = utils.isMongooseArray(value) ? value.__array : value;
  305. value = MongooseArray(rawValue, options.path || this._arrayPath || this.path, doc, this);
  306. rawValue = value.__array;
  307. if (init && doc != null && doc.$__ != null && doc.$populated(this.path)) {
  308. return value;
  309. }
  310. const caster = this.caster;
  311. const isMongooseArray = caster.$isMongooseArray;
  312. if (caster && this.casterConstructor !== Mixed) {
  313. try {
  314. const len = rawValue.length;
  315. for (i = 0; i < len; i++) {
  316. const opts = {};
  317. // Perf: creating `arrayPath` is expensive for large arrays.
  318. // We only need `arrayPath` if this is a nested array, so
  319. // skip if possible.
  320. if (isMongooseArray) {
  321. if (options.arrayPath != null) {
  322. opts.arrayPathIndex = i;
  323. } else if (caster._arrayParentPath != null) {
  324. opts.arrayPathIndex = i;
  325. }
  326. }
  327. rawValue[i] = caster.applySetters(rawValue[i], doc, init, void 0, opts);
  328. }
  329. } catch (e) {
  330. // rethrow
  331. throw new CastError('[' + e.kind + ']', util.inspect(value), this.path + '.' + i, e, this);
  332. }
  333. }
  334. return value;
  335. }
  336. if (init || SchemaArray.options.castNonArrays) {
  337. // gh-2442: if we're loading this from the db and its not an array, mark
  338. // the whole array as modified.
  339. if (!!doc && !!init) {
  340. doc.markModified(this.path);
  341. }
  342. return this.cast([value], doc, init);
  343. }
  344. throw new CastError('Array', util.inspect(value), this.path, null, this);
  345. };
  346. /*!
  347. * ignore
  348. */
  349. SchemaArray.prototype._castForPopulate = function _castForPopulate(value, doc) {
  350. // lazy load
  351. MongooseArray || (MongooseArray = require('../types').Array);
  352. if (Array.isArray(value)) {
  353. let i;
  354. const rawValue = value.__array ? value.__array : value;
  355. const len = rawValue.length;
  356. const caster = this.caster;
  357. if (caster && this.casterConstructor !== Mixed) {
  358. try {
  359. for (i = 0; i < len; i++) {
  360. const opts = {};
  361. // Perf: creating `arrayPath` is expensive for large arrays.
  362. // We only need `arrayPath` if this is a nested array, so
  363. // skip if possible.
  364. if (caster.$isMongooseArray && caster._arrayParentPath != null) {
  365. opts.arrayPathIndex = i;
  366. }
  367. rawValue[i] = caster.cast(rawValue[i], doc, false, void 0, opts);
  368. }
  369. } catch (e) {
  370. // rethrow
  371. throw new CastError('[' + e.kind + ']', util.inspect(value), this.path + '.' + i, e, this);
  372. }
  373. }
  374. return value;
  375. }
  376. throw new CastError('Array', util.inspect(value), this.path, null, this);
  377. };
  378. SchemaArray.prototype.$toObject = SchemaArray.prototype.toObject;
  379. /*!
  380. * Ignore
  381. */
  382. SchemaArray.prototype.discriminator = function(name, schema) {
  383. let arr = this;
  384. while (arr.$isMongooseArray && !arr.$isMongooseDocumentArray) {
  385. arr = arr.casterConstructor;
  386. if (arr == null || typeof arr === 'function') {
  387. throw new MongooseError('You can only add an embedded discriminator on ' +
  388. 'a document array, ' + this.path + ' is a plain array');
  389. }
  390. }
  391. return arr.discriminator(name, schema);
  392. };
  393. /*!
  394. * ignore
  395. */
  396. SchemaArray.prototype.clone = function() {
  397. const options = Object.assign({}, this.options);
  398. const schematype = new this.constructor(this.path, this.caster, options, this.schemaOptions);
  399. schematype.validators = this.validators.slice();
  400. if (this.requiredValidator !== undefined) {
  401. schematype.requiredValidator = this.requiredValidator;
  402. }
  403. return schematype;
  404. };
  405. /**
  406. * Casts values for queries.
  407. *
  408. * @param {String} $conditional
  409. * @param {any} [value]
  410. * @api private
  411. */
  412. SchemaArray.prototype.castForQuery = function($conditional, value) {
  413. let handler;
  414. let val;
  415. if (arguments.length === 2) {
  416. handler = this.$conditionalHandlers[$conditional];
  417. if (!handler) {
  418. throw new Error('Can\'t use ' + $conditional + ' with Array.');
  419. }
  420. val = handler.call(this, value);
  421. } else {
  422. val = $conditional;
  423. let Constructor = this.casterConstructor;
  424. if (val &&
  425. Constructor.discriminators &&
  426. Constructor.schema &&
  427. Constructor.schema.options &&
  428. Constructor.schema.options.discriminatorKey) {
  429. if (typeof val[Constructor.schema.options.discriminatorKey] === 'string' &&
  430. Constructor.discriminators[val[Constructor.schema.options.discriminatorKey]]) {
  431. Constructor = Constructor.discriminators[val[Constructor.schema.options.discriminatorKey]];
  432. } else {
  433. const constructorByValue = getDiscriminatorByValue(Constructor.discriminators, val[Constructor.schema.options.discriminatorKey]);
  434. if (constructorByValue) {
  435. Constructor = constructorByValue;
  436. }
  437. }
  438. }
  439. const proto = this.casterConstructor.prototype;
  440. let method = proto && (proto.castForQuery || proto.cast);
  441. if (!method && Constructor.castForQuery) {
  442. method = Constructor.castForQuery;
  443. }
  444. const caster = this.caster;
  445. if (Array.isArray(val)) {
  446. this.setters.reverse().forEach(setter => {
  447. val = setter.call(this, val, this);
  448. });
  449. val = val.map(function(v) {
  450. if (utils.isObject(v) && v.$elemMatch) {
  451. return v;
  452. }
  453. if (method) {
  454. v = method.call(caster, v);
  455. return v;
  456. }
  457. if (v != null) {
  458. v = new Constructor(v);
  459. return v;
  460. }
  461. return v;
  462. });
  463. } else if (method) {
  464. val = method.call(caster, val);
  465. } else if (val != null) {
  466. val = new Constructor(val);
  467. }
  468. }
  469. return val;
  470. };
  471. function cast$all(val) {
  472. if (!Array.isArray(val)) {
  473. val = [val];
  474. }
  475. val = val.map(function(v) {
  476. if (!utils.isObject(v)) {
  477. return v;
  478. }
  479. if (v.$elemMatch != null) {
  480. return { $elemMatch: cast(this.casterConstructor.schema, v.$elemMatch) };
  481. }
  482. const o = {};
  483. o[this.path] = v;
  484. return cast(this.casterConstructor.schema, o)[this.path];
  485. }, this);
  486. return this.castForQuery(val);
  487. }
  488. function cast$elemMatch(val) {
  489. const keys = Object.keys(val);
  490. const numKeys = keys.length;
  491. for (let i = 0; i < numKeys; ++i) {
  492. const key = keys[i];
  493. const value = val[key];
  494. if (isOperator(key) && value != null) {
  495. val[key] = this.castForQuery(key, value);
  496. }
  497. }
  498. // Is this an embedded discriminator and is the discriminator key set?
  499. // If so, use the discriminator schema. See gh-7449
  500. const discriminatorKey = this &&
  501. this.casterConstructor &&
  502. this.casterConstructor.schema &&
  503. this.casterConstructor.schema.options &&
  504. this.casterConstructor.schema.options.discriminatorKey;
  505. const discriminators = this &&
  506. this.casterConstructor &&
  507. this.casterConstructor.schema &&
  508. this.casterConstructor.schema.discriminators || {};
  509. if (discriminatorKey != null &&
  510. val[discriminatorKey] != null &&
  511. discriminators[val[discriminatorKey]] != null) {
  512. return cast(discriminators[val[discriminatorKey]], val);
  513. }
  514. return cast(this.casterConstructor.schema, val);
  515. }
  516. const handle = SchemaArray.prototype.$conditionalHandlers = {};
  517. handle.$all = cast$all;
  518. handle.$options = String;
  519. handle.$elemMatch = cast$elemMatch;
  520. handle.$geoIntersects = geospatial.cast$geoIntersects;
  521. handle.$or = createLogicalQueryOperatorHandler('$or');
  522. handle.$and = createLogicalQueryOperatorHandler('$and');
  523. handle.$nor = createLogicalQueryOperatorHandler('$nor');
  524. function createLogicalQueryOperatorHandler(op) {
  525. return function logicalQueryOperatorHandler(val) {
  526. if (!Array.isArray(val)) {
  527. throw new TypeError('conditional ' + op + ' requires an array');
  528. }
  529. const ret = [];
  530. for (const obj of val) {
  531. ret.push(cast(this.casterConstructor.schema, obj));
  532. }
  533. return ret;
  534. };
  535. }
  536. handle.$near =
  537. handle.$nearSphere = geospatial.cast$near;
  538. handle.$within =
  539. handle.$geoWithin = geospatial.cast$within;
  540. handle.$size =
  541. handle.$minDistance =
  542. handle.$maxDistance = castToNumber;
  543. handle.$exists = $exists;
  544. handle.$type = $type;
  545. handle.$eq =
  546. handle.$gt =
  547. handle.$gte =
  548. handle.$lt =
  549. handle.$lte =
  550. handle.$ne =
  551. handle.$not =
  552. handle.$regex = SchemaArray.prototype.castForQuery;
  553. // `$in` is special because you can also include an empty array in the query
  554. // like `$in: [1, []]`, see gh-5913
  555. handle.$nin = SchemaType.prototype.$conditionalHandlers.$nin;
  556. handle.$in = SchemaType.prototype.$conditionalHandlers.$in;
  557. /*!
  558. * Module exports.
  559. */
  560. module.exports = SchemaArray;