array.js 16 KB

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