schema.js 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251
  1. 'use strict';
  2. /*!
  3. * Module dependencies.
  4. */
  5. const EventEmitter = require('events').EventEmitter;
  6. const Kareem = require('kareem');
  7. const MongooseError = require('./error/mongooseError');
  8. const SchemaType = require('./schematype');
  9. const SchemaTypeOptions = require('./options/SchemaTypeOptions');
  10. const VirtualOptions = require('./options/VirtualOptions');
  11. const VirtualType = require('./virtualtype');
  12. const addAutoId = require('./helpers/schema/addAutoId');
  13. const get = require('./helpers/get');
  14. const getConstructorName = require('./helpers/getConstructorName');
  15. const getIndexes = require('./helpers/schema/getIndexes');
  16. const idGetter = require('./helpers/schema/idGetter');
  17. const merge = require('./helpers/schema/merge');
  18. const mpath = require('mpath');
  19. const readPref = require('./driver').get().ReadPreference;
  20. const setupTimestamps = require('./helpers/timestamps/setupTimestamps');
  21. const utils = require('./utils');
  22. const validateRef = require('./helpers/populate/validateRef');
  23. let MongooseTypes;
  24. const queryHooks = require('./helpers/query/applyQueryMiddleware').
  25. middlewareFunctions;
  26. const documentHooks = require('./helpers/model/applyHooks').middlewareFunctions;
  27. const hookNames = queryHooks.concat(documentHooks).
  28. reduce((s, hook) => s.add(hook), new Set());
  29. const isPOJO = utils.isPOJO;
  30. let id = 0;
  31. /**
  32. * Schema constructor.
  33. *
  34. * ####Example:
  35. *
  36. * const child = new Schema({ name: String });
  37. * const schema = new Schema({ name: String, age: Number, children: [child] });
  38. * const Tree = mongoose.model('Tree', schema);
  39. *
  40. * // setting schema options
  41. * new Schema({ name: String }, { _id: false, autoIndex: false })
  42. *
  43. * ####Options:
  44. *
  45. * - [autoIndex](/docs/guide.html#autoIndex): bool - defaults to null (which means use the connection's autoIndex option)
  46. * - [autoCreate](/docs/guide.html#autoCreate): bool - defaults to null (which means use the connection's autoCreate option)
  47. * - [bufferCommands](/docs/guide.html#bufferCommands): bool - defaults to true
  48. * - [bufferTimeoutMS](/docs/guide.html#bufferTimeoutMS): number - defaults to 10000 (10 seconds). If `bufferCommands` is enabled, the amount of time Mongoose will wait for connectivity to be restablished before erroring out.
  49. * - [capped](/docs/guide.html#capped): bool - defaults to false
  50. * - [collection](/docs/guide.html#collection): string - no default
  51. * - [discriminatorKey](/docs/guide.html#discriminatorKey): string - defaults to `__t`
  52. * - [id](/docs/guide.html#id): bool - defaults to true
  53. * - [_id](/docs/guide.html#_id): bool - defaults to true
  54. * - [minimize](/docs/guide.html#minimize): bool - controls [document#toObject](#document_Document-toObject) behavior when called manually - defaults to true
  55. * - [read](/docs/guide.html#read): string
  56. * - [writeConcern](/docs/guide.html#writeConcern): object - defaults to null, use to override [the MongoDB server's default write concern settings](https://docs.mongodb.com/manual/reference/write-concern/)
  57. * - [shardKey](/docs/guide.html#shardKey): object - defaults to `null`
  58. * - [strict](/docs/guide.html#strict): bool - defaults to true
  59. * - [strictQuery](/docs/guide.html#strictQuery): bool - defaults to false
  60. * - [toJSON](/docs/guide.html#toJSON) - object - no default
  61. * - [toObject](/docs/guide.html#toObject) - object - no default
  62. * - [typeKey](/docs/guide.html#typeKey) - string - defaults to 'type'
  63. * - [validateBeforeSave](/docs/guide.html#validateBeforeSave) - bool - defaults to `true`
  64. * - [versionKey](/docs/guide.html#versionKey): string or object - defaults to "__v"
  65. * - [optimisticConcurrency](/docs/guide.html#optimisticConcurrency): bool - defaults to false. Set to true to enable [optimistic concurrency](https://thecodebarbarian.com/whats-new-in-mongoose-5-10-optimistic-concurrency.html).
  66. * - [collation](/docs/guide.html#collation): object - defaults to null (which means use no collation)
  67. * - [timeseries](/docs/guide.html#timeseries): object - defaults to null (which means this schema's collection won't be a timeseries collection)
  68. * - [selectPopulatedPaths](/docs/guide.html#selectPopulatedPaths): boolean - defaults to `true`
  69. * - [skipVersioning](/docs/guide.html#skipVersioning): object - paths to exclude from versioning
  70. * - [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.
  71. * - [pluginTags](/docs/guide.html#pluginTags): array of strings - defaults to `undefined`. If set and plugin called with `tags` option, will only apply that plugin to schemas with a matching tag.
  72. *
  73. * ####Options for Nested Schemas:
  74. * - `excludeIndexes`: bool - defaults to `false`. If `true`, skip building indexes on this schema's paths.
  75. *
  76. * ####Note:
  77. *
  78. * _When nesting schemas, (`children` in the example above), always declare the child schema first before passing it into its parent._
  79. *
  80. * @param {Object|Schema|Array} [definition] Can be one of: object describing schema paths, or schema to copy, or array of objects and schemas
  81. * @param {Object} [options]
  82. * @inherits NodeJS EventEmitter http://nodejs.org/api/events.html#events_class_events_eventemitter
  83. * @event `init`: Emitted after the schema is compiled into a `Model`.
  84. * @api public
  85. */
  86. function Schema(obj, options) {
  87. if (!(this instanceof Schema)) {
  88. return new Schema(obj, options);
  89. }
  90. this.obj = obj;
  91. this.paths = {};
  92. this.aliases = {};
  93. this.subpaths = {};
  94. this.virtuals = {};
  95. this.singleNestedPaths = {};
  96. this.nested = {};
  97. this.inherits = {};
  98. this.callQueue = [];
  99. this._indexes = [];
  100. this.methods = {};
  101. this.methodOptions = {};
  102. this.statics = {};
  103. this.tree = {};
  104. this.query = {};
  105. this.childSchemas = [];
  106. this.plugins = [];
  107. // For internal debugging. Do not use this to try to save a schema in MDB.
  108. this.$id = ++id;
  109. this.mapPaths = [];
  110. this.s = {
  111. hooks: new Kareem()
  112. };
  113. this.options = this.defaultOptions(options);
  114. // build paths
  115. if (Array.isArray(obj)) {
  116. for (const definition of obj) {
  117. this.add(definition);
  118. }
  119. } else if (obj) {
  120. this.add(obj);
  121. }
  122. // check if _id's value is a subdocument (gh-2276)
  123. const _idSubDoc = obj && obj._id && utils.isObject(obj._id);
  124. // ensure the documents get an auto _id unless disabled
  125. const auto_id = !this.paths['_id'] &&
  126. (this.options._id) && !_idSubDoc;
  127. if (auto_id) {
  128. addAutoId(this);
  129. }
  130. this.setupTimestamp(this.options.timestamps);
  131. }
  132. /*!
  133. * Create virtual properties with alias field
  134. */
  135. function aliasFields(schema, paths) {
  136. paths = paths || Object.keys(schema.paths);
  137. for (const path of paths) {
  138. const options = get(schema.paths[path], 'options');
  139. if (options == null) {
  140. continue;
  141. }
  142. const prop = schema.paths[path].path;
  143. const alias = options.alias;
  144. if (!alias) {
  145. continue;
  146. }
  147. if (typeof alias !== 'string') {
  148. throw new Error('Invalid value for alias option on ' + prop + ', got ' + alias);
  149. }
  150. schema.aliases[alias] = prop;
  151. schema.
  152. virtual(alias).
  153. get((function(p) {
  154. return function() {
  155. if (typeof this.get === 'function') {
  156. return this.get(p);
  157. }
  158. return this[p];
  159. };
  160. })(prop)).
  161. set((function(p) {
  162. return function(v) {
  163. return this.$set(p, v);
  164. };
  165. })(prop));
  166. }
  167. }
  168. /*!
  169. * Inherit from EventEmitter.
  170. */
  171. Schema.prototype = Object.create(EventEmitter.prototype);
  172. Schema.prototype.constructor = Schema;
  173. Schema.prototype.instanceOfSchema = true;
  174. /*!
  175. * ignore
  176. */
  177. Object.defineProperty(Schema.prototype, '$schemaType', {
  178. configurable: false,
  179. enumerable: false,
  180. writable: true
  181. });
  182. /**
  183. * Array of child schemas (from document arrays and single nested subdocs)
  184. * and their corresponding compiled models. Each element of the array is
  185. * an object with 2 properties: `schema` and `model`.
  186. *
  187. * This property is typically only useful for plugin authors and advanced users.
  188. * You do not need to interact with this property at all to use mongoose.
  189. *
  190. * @api public
  191. * @property childSchemas
  192. * @memberOf Schema
  193. * @instance
  194. */
  195. Object.defineProperty(Schema.prototype, 'childSchemas', {
  196. configurable: false,
  197. enumerable: true,
  198. writable: true
  199. });
  200. /**
  201. * Object containing all virtuals defined on this schema.
  202. * The objects' keys are the virtual paths and values are instances of `VirtualType`.
  203. *
  204. * This property is typically only useful for plugin authors and advanced users.
  205. * You do not need to interact with this property at all to use mongoose.
  206. *
  207. * ####Example:
  208. * const schema = new Schema({});
  209. * schema.virtual('answer').get(() => 42);
  210. *
  211. * console.log(schema.virtuals); // { answer: VirtualType { path: 'answer', ... } }
  212. * console.log(schema.virtuals['answer'].getters[0].call()); // 42
  213. *
  214. * @api public
  215. * @property virtuals
  216. * @memberOf Schema
  217. * @instance
  218. */
  219. Object.defineProperty(Schema.prototype, 'virtuals', {
  220. configurable: false,
  221. enumerable: true,
  222. writable: true
  223. });
  224. /**
  225. * The original object passed to the schema constructor
  226. *
  227. * ####Example:
  228. *
  229. * const schema = new Schema({ a: String }).add({ b: String });
  230. * schema.obj; // { a: String }
  231. *
  232. * @api public
  233. * @property obj
  234. * @memberOf Schema
  235. * @instance
  236. */
  237. Schema.prototype.obj;
  238. /**
  239. * The paths defined on this schema. The keys are the top-level paths
  240. * in this schema, and the values are instances of the SchemaType class.
  241. *
  242. * ####Example:
  243. * const schema = new Schema({ name: String }, { _id: false });
  244. * schema.paths; // { name: SchemaString { ... } }
  245. *
  246. * schema.add({ age: Number });
  247. * schema.paths; // { name: SchemaString { ... }, age: SchemaNumber { ... } }
  248. *
  249. * @api public
  250. * @property paths
  251. * @memberOf Schema
  252. * @instance
  253. */
  254. Schema.prototype.paths;
  255. /**
  256. * Schema as a tree
  257. *
  258. * ####Example:
  259. * {
  260. * '_id' : ObjectId
  261. * , 'nested' : {
  262. * 'key' : String
  263. * }
  264. * }
  265. *
  266. * @api private
  267. * @property tree
  268. * @memberOf Schema
  269. * @instance
  270. */
  271. Schema.prototype.tree;
  272. /**
  273. * Returns a deep copy of the schema
  274. *
  275. * ####Example:
  276. *
  277. * const schema = new Schema({ name: String });
  278. * const clone = schema.clone();
  279. * clone === schema; // false
  280. * clone.path('name'); // SchemaString { ... }
  281. *
  282. * @return {Schema} the cloned schema
  283. * @api public
  284. * @memberOf Schema
  285. * @instance
  286. */
  287. Schema.prototype.clone = function() {
  288. const s = this._clone();
  289. // Bubble up `init` for backwards compat
  290. s.on('init', v => this.emit('init', v));
  291. return s;
  292. };
  293. Schema.prototype._clone = function _clone(Constructor) {
  294. Constructor = Constructor || (this.base == null ? Schema : this.base.Schema);
  295. const s = new Constructor({}, this._userProvidedOptions);
  296. s.base = this.base;
  297. s.obj = this.obj;
  298. s.options = utils.clone(this.options);
  299. s.callQueue = this.callQueue.map(function(f) { return f; });
  300. s.methods = utils.clone(this.methods);
  301. s.methodOptions = utils.clone(this.methodOptions);
  302. s.statics = utils.clone(this.statics);
  303. s.query = utils.clone(this.query);
  304. s.plugins = Array.prototype.slice.call(this.plugins);
  305. s._indexes = utils.clone(this._indexes);
  306. s.s.hooks = this.s.hooks.clone();
  307. s.tree = utils.clone(this.tree);
  308. s.paths = utils.clone(this.paths);
  309. s.nested = utils.clone(this.nested);
  310. s.subpaths = utils.clone(this.subpaths);
  311. s.singleNestedPaths = utils.clone(this.singleNestedPaths);
  312. s.childSchemas = gatherChildSchemas(s);
  313. s.virtuals = utils.clone(this.virtuals);
  314. s.$globalPluginsApplied = this.$globalPluginsApplied;
  315. s.$isRootDiscriminator = this.$isRootDiscriminator;
  316. s.$implicitlyCreated = this.$implicitlyCreated;
  317. s.$id = ++id;
  318. s.$originalSchemaId = this.$id;
  319. s.mapPaths = [].concat(this.mapPaths);
  320. if (this.discriminatorMapping != null) {
  321. s.discriminatorMapping = Object.assign({}, this.discriminatorMapping);
  322. }
  323. if (this.discriminators != null) {
  324. s.discriminators = Object.assign({}, this.discriminators);
  325. }
  326. s.aliases = Object.assign({}, this.aliases);
  327. return s;
  328. };
  329. /**
  330. * Returns a new schema that has the picked `paths` from this schema.
  331. *
  332. * This method is analagous to [Lodash's `pick()` function](https://lodash.com/docs/4.17.15#pick) for Mongoose schemas.
  333. *
  334. * ####Example:
  335. *
  336. * const schema = Schema({ name: String, age: Number });
  337. * // Creates a new schema with the same `name` path as `schema`,
  338. * // but no `age` path.
  339. * const newSchema = schema.pick(['name']);
  340. *
  341. * newSchema.path('name'); // SchemaString { ... }
  342. * newSchema.path('age'); // undefined
  343. *
  344. * @param {Array} paths list of paths to pick
  345. * @param {Object} [options] options to pass to the schema constructor. Defaults to `this.options` if not set.
  346. * @return {Schema}
  347. * @api public
  348. */
  349. Schema.prototype.pick = function(paths, options) {
  350. const newSchema = new Schema({}, options || this.options);
  351. if (!Array.isArray(paths)) {
  352. throw new MongooseError('Schema#pick() only accepts an array argument, ' +
  353. 'got "' + typeof paths + '"');
  354. }
  355. for (const path of paths) {
  356. if (this.nested[path]) {
  357. newSchema.add({ [path]: get(this.tree, path) });
  358. } else {
  359. const schematype = this.path(path);
  360. if (schematype == null) {
  361. throw new MongooseError('Path `' + path + '` is not in the schema');
  362. }
  363. newSchema.add({ [path]: schematype });
  364. }
  365. }
  366. return newSchema;
  367. };
  368. /**
  369. * Returns default options for this schema, merged with `options`.
  370. *
  371. * @param {Object} options
  372. * @return {Object}
  373. * @api private
  374. */
  375. Schema.prototype.defaultOptions = function(options) {
  376. this._userProvidedOptions = options == null ? {} : utils.clone(options);
  377. const baseOptions = get(this, 'base.options', {});
  378. const strict = 'strict' in baseOptions ? baseOptions.strict : true;
  379. options = utils.options({
  380. strict: strict,
  381. strictQuery: 'strict' in this._userProvidedOptions ?
  382. this._userProvidedOptions.strict :
  383. 'strictQuery' in baseOptions ?
  384. baseOptions.strictQuery : strict,
  385. bufferCommands: true,
  386. capped: false, // { size, max, autoIndexId }
  387. versionKey: '__v',
  388. optimisticConcurrency: false,
  389. minimize: true,
  390. autoIndex: null,
  391. discriminatorKey: '__t',
  392. shardKey: null,
  393. read: null,
  394. validateBeforeSave: true,
  395. // the following are only applied at construction time
  396. _id: true,
  397. id: true,
  398. typeKey: 'type'
  399. }, utils.clone(options));
  400. if (options.read) {
  401. options.read = readPref(options.read);
  402. }
  403. if (options.versionKey && typeof options.versionKey !== 'string') {
  404. throw new MongooseError('`versionKey` must be falsy or string, got `' + (typeof options.versionKey) + '`');
  405. }
  406. if (options.optimisticConcurrency && !options.versionKey) {
  407. throw new MongooseError('Must set `versionKey` if using `optimisticConcurrency`');
  408. }
  409. return options;
  410. };
  411. /**
  412. * Adds key path / schema type pairs to this schema.
  413. *
  414. * ####Example:
  415. *
  416. * const ToySchema = new Schema();
  417. * ToySchema.add({ name: 'string', color: 'string', price: 'number' });
  418. *
  419. * const TurboManSchema = new Schema();
  420. * // You can also `add()` another schema and copy over all paths, virtuals,
  421. * // getters, setters, indexes, methods, and statics.
  422. * TurboManSchema.add(ToySchema).add({ year: Number });
  423. *
  424. * @param {Object|Schema} obj plain object with paths to add, or another schema
  425. * @param {String} [prefix] path to prefix the newly added paths with
  426. * @return {Schema} the Schema instance
  427. * @api public
  428. */
  429. Schema.prototype.add = function add(obj, prefix) {
  430. if (obj instanceof Schema || (obj != null && obj.instanceOfSchema)) {
  431. merge(this, obj);
  432. return this;
  433. }
  434. // Special case: setting top-level `_id` to false should convert to disabling
  435. // the `_id` option. This behavior never worked before 5.4.11 but numerous
  436. // codebases use it (see gh-7516, gh-7512).
  437. if (obj._id === false && prefix == null) {
  438. this.options._id = false;
  439. }
  440. prefix = prefix || '';
  441. // avoid prototype pollution
  442. if (prefix === '__proto__.' || prefix === 'constructor.' || prefix === 'prototype.') {
  443. return this;
  444. }
  445. const keys = Object.keys(obj);
  446. const typeKey = this.options.typeKey;
  447. for (const key of keys) {
  448. const fullPath = prefix + key;
  449. const val = obj[key];
  450. if (val == null) {
  451. throw new TypeError('Invalid value for schema path `' + fullPath +
  452. '`, got value "' + val + '"');
  453. }
  454. // Retain `_id: false` but don't set it as a path, re: gh-8274.
  455. if (key === '_id' && val === false) {
  456. continue;
  457. }
  458. if (val instanceof VirtualType || get(val, 'constructor.name', null) === 'VirtualType') {
  459. this.virtual(val);
  460. continue;
  461. }
  462. if (Array.isArray(val) && val.length === 1 && val[0] == null) {
  463. throw new TypeError('Invalid value for schema Array path `' + fullPath +
  464. '`, got value "' + val[0] + '"');
  465. }
  466. if (!(isPOJO(val) || val instanceof SchemaTypeOptions)) {
  467. // Special-case: Non-options definitely a path so leaf at this node
  468. // Examples: Schema instances, SchemaType instances
  469. if (prefix) {
  470. this.nested[prefix.substring(0, prefix.length - 1)] = true;
  471. }
  472. this.path(prefix + key, val);
  473. } else if (Object.keys(val).length < 1) {
  474. // Special-case: {} always interpreted as Mixed path so leaf at this node
  475. if (prefix) {
  476. this.nested[prefix.substring(0, prefix.length - 1)] = true;
  477. }
  478. this.path(fullPath, val); // mixed type
  479. } else if (!val[typeKey] || (typeKey === 'type' && isPOJO(val.type) && val.type.type)) {
  480. // Special-case: POJO with no bona-fide type key - interpret as tree of deep paths so recurse
  481. // nested object `{ last: { name: String } }`. Avoid functions with `.type` re: #10807 because
  482. // NestJS sometimes adds `Date.type`.
  483. this.nested[fullPath] = true;
  484. this.add(val, fullPath + '.');
  485. } else {
  486. // There IS a bona-fide type key that may also be a POJO
  487. const _typeDef = val[typeKey];
  488. if (isPOJO(_typeDef) && Object.keys(_typeDef).length > 0) {
  489. // If a POJO is the value of a type key, make it a subdocument
  490. if (prefix) {
  491. this.nested[prefix.substring(0, prefix.length - 1)] = true;
  492. }
  493. const _schema = new Schema(_typeDef);
  494. const schemaWrappedPath = Object.assign({}, val, { type: _schema });
  495. this.path(prefix + key, schemaWrappedPath);
  496. } else {
  497. // Either the type is non-POJO or we interpret it as Mixed anyway
  498. if (prefix) {
  499. this.nested[prefix.substring(0, prefix.length - 1)] = true;
  500. }
  501. this.path(prefix + key, val);
  502. }
  503. }
  504. }
  505. const addedKeys = Object.keys(obj).
  506. map(key => prefix ? prefix + key : key);
  507. aliasFields(this, addedKeys);
  508. return this;
  509. };
  510. /**
  511. * Reserved document keys.
  512. *
  513. * Keys in this object are names that are warned in schema declarations
  514. * because they have the potential to break Mongoose/ Mongoose plugins functionality. If you create a schema
  515. * using `new Schema()` with one of these property names, Mongoose will log a warning.
  516. *
  517. * - _posts
  518. * - _pres
  519. * - collection
  520. * - emit
  521. * - errors
  522. * - get
  523. * - init
  524. * - isModified
  525. * - isNew
  526. * - listeners
  527. * - modelName
  528. * - on
  529. * - once
  530. * - populated
  531. * - prototype
  532. * - remove
  533. * - removeListener
  534. * - save
  535. * - schema
  536. * - toObject
  537. * - validate
  538. *
  539. * _NOTE:_ Use of these terms as method names is permitted, but play at your own risk, as they may be existing mongoose document methods you are stomping on.
  540. *
  541. * const schema = new Schema(..);
  542. * schema.methods.init = function () {} // potentially breaking
  543. */
  544. Schema.reserved = Object.create(null);
  545. Schema.prototype.reserved = Schema.reserved;
  546. const reserved = Schema.reserved;
  547. // Core object
  548. reserved['prototype'] =
  549. // EventEmitter
  550. reserved.emit =
  551. reserved.listeners =
  552. reserved.on =
  553. reserved.removeListener =
  554. // document properties and functions
  555. reserved.collection =
  556. reserved.errors =
  557. reserved.get =
  558. reserved.init =
  559. reserved.isModified =
  560. reserved.isNew =
  561. reserved.populated =
  562. reserved.remove =
  563. reserved.save =
  564. reserved.toObject =
  565. reserved.validate = 1;
  566. reserved.collection = 1;
  567. /**
  568. * Gets/sets schema paths.
  569. *
  570. * Sets a path (if arity 2)
  571. * Gets a path (if arity 1)
  572. *
  573. * ####Example
  574. *
  575. * schema.path('name') // returns a SchemaType
  576. * schema.path('name', Number) // changes the schemaType of `name` to Number
  577. *
  578. * @param {String} path
  579. * @param {Object} constructor
  580. * @api public
  581. */
  582. Schema.prototype.path = function(path, obj) {
  583. // Convert to '.$' to check subpaths re: gh-6405
  584. const cleanPath = _pathToPositionalSyntax(path);
  585. if (obj === undefined) {
  586. let schematype = _getPath(this, path, cleanPath);
  587. if (schematype != null) {
  588. return schematype;
  589. }
  590. // Look for maps
  591. const mapPath = getMapPath(this, path);
  592. if (mapPath != null) {
  593. return mapPath;
  594. }
  595. // Look if a parent of this path is mixed
  596. schematype = this.hasMixedParent(cleanPath);
  597. if (schematype != null) {
  598. return schematype;
  599. }
  600. // subpaths?
  601. return /\.\d+\.?.*$/.test(path)
  602. ? getPositionalPath(this, path)
  603. : undefined;
  604. }
  605. // some path names conflict with document methods
  606. const firstPieceOfPath = path.split('.')[0];
  607. if (reserved[firstPieceOfPath] && !this.options.supressReservedKeysWarning) {
  608. const errorMessage = `\`${firstPieceOfPath}\` is a reserved schema pathname and may break some functionality. ` +
  609. 'You are allowed to use it, but use at your own risk. ' +
  610. 'To disable this warning pass `supressReservedKeysWarning` as a schema option.';
  611. utils.warn(errorMessage);
  612. }
  613. if (typeof obj === 'object' && utils.hasUserDefinedProperty(obj, 'ref')) {
  614. validateRef(obj.ref, path);
  615. }
  616. // update the tree
  617. const subpaths = path.split(/\./);
  618. const last = subpaths.pop();
  619. let branch = this.tree;
  620. let fullPath = '';
  621. for (const sub of subpaths) {
  622. fullPath = fullPath += (fullPath.length > 0 ? '.' : '') + sub;
  623. if (!branch[sub]) {
  624. this.nested[fullPath] = true;
  625. branch[sub] = {};
  626. }
  627. if (typeof branch[sub] !== 'object') {
  628. const msg = 'Cannot set nested path `' + path + '`. '
  629. + 'Parent path `'
  630. + fullPath
  631. + '` already set to type ' + branch[sub].name
  632. + '.';
  633. throw new Error(msg);
  634. }
  635. branch = branch[sub];
  636. }
  637. branch[last] = utils.clone(obj);
  638. this.paths[path] = this.interpretAsType(path, obj, this.options);
  639. const schemaType = this.paths[path];
  640. if (schemaType.$isSchemaMap) {
  641. // Maps can have arbitrary keys, so `$*` is internal shorthand for "any key"
  642. // The '$' is to imply this path should never be stored in MongoDB so we
  643. // can easily build a regexp out of this path, and '*' to imply "any key."
  644. const mapPath = path + '.$*';
  645. this.paths[mapPath] = schemaType.$__schemaType;
  646. this.mapPaths.push(this.paths[mapPath]);
  647. }
  648. if (schemaType.$isSingleNested) {
  649. for (const key of Object.keys(schemaType.schema.paths)) {
  650. this.singleNestedPaths[path + '.' + key] = schemaType.schema.paths[key];
  651. }
  652. for (const key of Object.keys(schemaType.schema.singleNestedPaths)) {
  653. this.singleNestedPaths[path + '.' + key] =
  654. schemaType.schema.singleNestedPaths[key];
  655. }
  656. for (const key of Object.keys(schemaType.schema.subpaths)) {
  657. this.singleNestedPaths[path + '.' + key] =
  658. schemaType.schema.subpaths[key];
  659. }
  660. for (const key of Object.keys(schemaType.schema.nested)) {
  661. this.singleNestedPaths[path + '.' + key] = 'nested';
  662. }
  663. Object.defineProperty(schemaType.schema, 'base', {
  664. configurable: true,
  665. enumerable: false,
  666. writable: false,
  667. value: this.base
  668. });
  669. schemaType.caster.base = this.base;
  670. this.childSchemas.push({
  671. schema: schemaType.schema,
  672. model: schemaType.caster
  673. });
  674. } else if (schemaType.$isMongooseDocumentArray) {
  675. Object.defineProperty(schemaType.schema, 'base', {
  676. configurable: true,
  677. enumerable: false,
  678. writable: false,
  679. value: this.base
  680. });
  681. schemaType.casterConstructor.base = this.base;
  682. this.childSchemas.push({
  683. schema: schemaType.schema,
  684. model: schemaType.casterConstructor
  685. });
  686. }
  687. if (schemaType.$isMongooseArray && schemaType.caster instanceof SchemaType) {
  688. let arrayPath = path;
  689. let _schemaType = schemaType;
  690. const toAdd = [];
  691. while (_schemaType.$isMongooseArray) {
  692. arrayPath = arrayPath + '.$';
  693. // Skip arrays of document arrays
  694. if (_schemaType.$isMongooseDocumentArray) {
  695. _schemaType.$embeddedSchemaType._arrayPath = arrayPath;
  696. _schemaType.$embeddedSchemaType._arrayParentPath = path;
  697. _schemaType = _schemaType.$embeddedSchemaType.clone();
  698. } else {
  699. _schemaType.caster._arrayPath = arrayPath;
  700. _schemaType.caster._arrayParentPath = path;
  701. _schemaType = _schemaType.caster.clone();
  702. }
  703. _schemaType.path = arrayPath;
  704. toAdd.push(_schemaType);
  705. }
  706. for (const _schemaType of toAdd) {
  707. this.subpaths[_schemaType.path] = _schemaType;
  708. }
  709. }
  710. if (schemaType.$isMongooseDocumentArray) {
  711. for (const key of Object.keys(schemaType.schema.paths)) {
  712. const _schemaType = schemaType.schema.paths[key];
  713. this.subpaths[path + '.' + key] = _schemaType;
  714. if (typeof _schemaType === 'object' && _schemaType != null) {
  715. _schemaType.$isUnderneathDocArray = true;
  716. }
  717. }
  718. for (const key of Object.keys(schemaType.schema.subpaths)) {
  719. const _schemaType = schemaType.schema.subpaths[key];
  720. this.subpaths[path + '.' + key] = _schemaType;
  721. if (typeof _schemaType === 'object' && _schemaType != null) {
  722. _schemaType.$isUnderneathDocArray = true;
  723. }
  724. }
  725. for (const key of Object.keys(schemaType.schema.singleNestedPaths)) {
  726. const _schemaType = schemaType.schema.singleNestedPaths[key];
  727. this.subpaths[path + '.' + key] = _schemaType;
  728. if (typeof _schemaType === 'object' && _schemaType != null) {
  729. _schemaType.$isUnderneathDocArray = true;
  730. }
  731. }
  732. }
  733. return this;
  734. };
  735. /*!
  736. * ignore
  737. */
  738. function gatherChildSchemas(schema) {
  739. const childSchemas = [];
  740. for (const path of Object.keys(schema.paths)) {
  741. const schematype = schema.paths[path];
  742. if (schematype.$isMongooseDocumentArray || schematype.$isSingleNested) {
  743. childSchemas.push({ schema: schematype.schema, model: schematype.caster });
  744. }
  745. }
  746. return childSchemas;
  747. }
  748. /*!
  749. * ignore
  750. */
  751. function _getPath(schema, path, cleanPath) {
  752. if (schema.paths.hasOwnProperty(path)) {
  753. return schema.paths[path];
  754. }
  755. if (schema.subpaths.hasOwnProperty(cleanPath)) {
  756. return schema.subpaths[cleanPath];
  757. }
  758. if (schema.singleNestedPaths.hasOwnProperty(cleanPath) && typeof schema.singleNestedPaths[cleanPath] === 'object') {
  759. return schema.singleNestedPaths[cleanPath];
  760. }
  761. return null;
  762. }
  763. /*!
  764. * ignore
  765. */
  766. function _pathToPositionalSyntax(path) {
  767. if (!/\.\d+/.test(path)) {
  768. return path;
  769. }
  770. return path.replace(/\.\d+\./g, '.$.').replace(/\.\d+$/, '.$');
  771. }
  772. /*!
  773. * ignore
  774. */
  775. function getMapPath(schema, path) {
  776. if (schema.mapPaths.length === 0) {
  777. return null;
  778. }
  779. for (const val of schema.mapPaths) {
  780. const _path = val.path;
  781. const re = new RegExp('^' + _path.replace(/\.\$\*/g, '\\.[^.]+') + '$');
  782. if (re.test(path)) {
  783. return schema.paths[_path];
  784. }
  785. }
  786. return null;
  787. }
  788. /**
  789. * The Mongoose instance this schema is associated with
  790. *
  791. * @property base
  792. * @api private
  793. */
  794. Object.defineProperty(Schema.prototype, 'base', {
  795. configurable: true,
  796. enumerable: false,
  797. writable: true,
  798. value: null
  799. });
  800. /**
  801. * Converts type arguments into Mongoose Types.
  802. *
  803. * @param {String} path
  804. * @param {Object} obj constructor
  805. * @api private
  806. */
  807. Schema.prototype.interpretAsType = function(path, obj, options) {
  808. if (obj instanceof SchemaType) {
  809. if (obj.path === path) {
  810. return obj;
  811. }
  812. const clone = obj.clone();
  813. clone.path = path;
  814. return clone;
  815. }
  816. // If this schema has an associated Mongoose object, use the Mongoose object's
  817. // copy of SchemaTypes re: gh-7158 gh-6933
  818. const MongooseTypes = this.base != null ? this.base.Schema.Types : Schema.Types;
  819. if (!utils.isPOJO(obj) && !(obj instanceof SchemaTypeOptions)) {
  820. const constructorName = utils.getFunctionName(obj.constructor);
  821. if (constructorName !== 'Object') {
  822. const oldObj = obj;
  823. obj = {};
  824. obj[options.typeKey] = oldObj;
  825. }
  826. }
  827. // Get the type making sure to allow keys named "type"
  828. // and default to mixed if not specified.
  829. // { type: { type: String, default: 'freshcut' } }
  830. let type = obj[options.typeKey] && (obj[options.typeKey] instanceof Function || options.typeKey !== 'type' || !obj.type.type)
  831. ? obj[options.typeKey]
  832. : {};
  833. let name;
  834. if (utils.isPOJO(type) || type === 'mixed') {
  835. return new MongooseTypes.Mixed(path, obj);
  836. }
  837. if (Array.isArray(type) || type === Array || type === 'array' || type === MongooseTypes.Array) {
  838. // if it was specified through { type } look for `cast`
  839. let cast = (type === Array || type === 'array')
  840. ? obj.cast || obj.of
  841. : type[0];
  842. // new Schema({ path: [new Schema({ ... })] })
  843. if (cast && cast.instanceOfSchema) {
  844. if (!(cast instanceof Schema)) {
  845. throw new TypeError('Schema for array path `' + path +
  846. '` is from a different copy of the Mongoose module. Please make sure you\'re using the same version ' +
  847. 'of Mongoose everywhere with `npm list mongoose`.');
  848. }
  849. return new MongooseTypes.DocumentArray(path, cast, obj);
  850. }
  851. if (cast &&
  852. cast[options.typeKey] &&
  853. cast[options.typeKey].instanceOfSchema) {
  854. if (!(cast[options.typeKey] instanceof Schema)) {
  855. throw new TypeError('Schema for array path `' + path +
  856. '` is from a different copy of the Mongoose module. Please make sure you\'re using the same version ' +
  857. 'of Mongoose everywhere with `npm list mongoose`.');
  858. }
  859. return new MongooseTypes.DocumentArray(path, cast[options.typeKey], obj, cast);
  860. }
  861. if (Array.isArray(cast)) {
  862. return new MongooseTypes.Array(path, this.interpretAsType(path, cast, options), obj);
  863. }
  864. // Handle both `new Schema({ arr: [{ subpath: String }] })` and `new Schema({ arr: [{ type: { subpath: string } }] })`
  865. const castFromTypeKey = (cast != null && cast[options.typeKey] && (options.typeKey !== 'type' || !cast.type.type)) ?
  866. cast[options.typeKey] :
  867. cast;
  868. if (typeof cast === 'string') {
  869. cast = MongooseTypes[cast.charAt(0).toUpperCase() + cast.substring(1)];
  870. } else if (utils.isPOJO(castFromTypeKey)) {
  871. if (Object.keys(castFromTypeKey).length) {
  872. // The `minimize` and `typeKey` options propagate to child schemas
  873. // declared inline, like `{ arr: [{ val: { $type: String } }] }`.
  874. // See gh-3560
  875. const childSchemaOptions = { minimize: options.minimize };
  876. if (options.typeKey) {
  877. childSchemaOptions.typeKey = options.typeKey;
  878. }
  879. // propagate 'strict' option to child schema
  880. if (options.hasOwnProperty('strict')) {
  881. childSchemaOptions.strict = options.strict;
  882. }
  883. if (this._userProvidedOptions.hasOwnProperty('_id')) {
  884. childSchemaOptions._id = this._userProvidedOptions._id;
  885. } else if (Schema.Types.DocumentArray.defaultOptions._id != null) {
  886. childSchemaOptions._id = Schema.Types.DocumentArray.defaultOptions._id;
  887. }
  888. const childSchema = new Schema(castFromTypeKey, childSchemaOptions);
  889. childSchema.$implicitlyCreated = true;
  890. return new MongooseTypes.DocumentArray(path, childSchema, obj);
  891. } else {
  892. // Special case: empty object becomes mixed
  893. return new MongooseTypes.Array(path, MongooseTypes.Mixed, obj);
  894. }
  895. }
  896. if (cast) {
  897. type = cast[options.typeKey] && (options.typeKey !== 'type' || !cast.type.type)
  898. ? cast[options.typeKey]
  899. : cast;
  900. name = typeof type === 'string'
  901. ? type
  902. : type.schemaName || utils.getFunctionName(type);
  903. // For Jest 26+, see #10296
  904. if (name === 'ClockDate') {
  905. name = 'Date';
  906. }
  907. if (name === void 0) {
  908. throw new TypeError('Invalid schema configuration: ' +
  909. `Could not determine the embedded type for array \`${path}\`. ` +
  910. 'See https://mongoosejs.com/docs/guide.html#definition for more info on supported schema syntaxes.');
  911. }
  912. if (!MongooseTypes.hasOwnProperty(name)) {
  913. throw new TypeError('Invalid schema configuration: ' +
  914. `\`${name}\` is not a valid type within the array \`${path}\`.` +
  915. 'See http://bit.ly/mongoose-schematypes for a list of valid schema types.');
  916. }
  917. }
  918. return new MongooseTypes.Array(path, cast || MongooseTypes.Mixed, obj, options);
  919. }
  920. if (type && type.instanceOfSchema) {
  921. return new MongooseTypes.Subdocument(type, path, obj);
  922. }
  923. if (Buffer.isBuffer(type)) {
  924. name = 'Buffer';
  925. } else if (typeof type === 'function' || typeof type === 'object') {
  926. name = type.schemaName || utils.getFunctionName(type);
  927. } else {
  928. name = type == null ? '' + type : type.toString();
  929. }
  930. if (name) {
  931. name = name.charAt(0).toUpperCase() + name.substring(1);
  932. }
  933. // Special case re: gh-7049 because the bson `ObjectID` class' capitalization
  934. // doesn't line up with Mongoose's.
  935. if (name === 'ObjectID') {
  936. name = 'ObjectId';
  937. }
  938. // For Jest 26+, see #10296
  939. if (name === 'ClockDate') {
  940. name = 'Date';
  941. }
  942. if (name === void 0) {
  943. throw new TypeError(`Invalid schema configuration: \`${path}\` schematype definition is ` +
  944. 'invalid. See ' +
  945. 'https://mongoosejs.com/docs/guide.html#definition for more info on supported schema syntaxes.');
  946. }
  947. if (MongooseTypes[name] == null) {
  948. throw new TypeError(`Invalid schema configuration: \`${name}\` is not ` +
  949. `a valid type at path \`${path}\`. See ` +
  950. 'http://bit.ly/mongoose-schematypes for a list of valid schema types.');
  951. }
  952. const schemaType = new MongooseTypes[name](path, obj);
  953. if (schemaType.$isSchemaMap) {
  954. createMapNestedSchemaType(this, schemaType, path, obj, options);
  955. }
  956. return schemaType;
  957. };
  958. /*!
  959. * ignore
  960. */
  961. function createMapNestedSchemaType(schema, schemaType, path, obj, options) {
  962. const mapPath = path + '.$*';
  963. let _mapType = { type: {} };
  964. if (utils.hasUserDefinedProperty(obj, 'of')) {
  965. const isInlineSchema = utils.isPOJO(obj.of) &&
  966. Object.keys(obj.of).length > 0 &&
  967. !utils.hasUserDefinedProperty(obj.of, schema.options.typeKey);
  968. if (isInlineSchema) {
  969. _mapType = { [schema.options.typeKey]: new Schema(obj.of) };
  970. } else if (utils.isPOJO(obj.of)) {
  971. _mapType = Object.assign({}, obj.of);
  972. } else {
  973. _mapType = { [schema.options.typeKey]: obj.of };
  974. }
  975. if (utils.hasUserDefinedProperty(obj, 'ref')) {
  976. _mapType.ref = obj.ref;
  977. }
  978. }
  979. schemaType.$__schemaType = schema.interpretAsType(mapPath, _mapType, options);
  980. }
  981. /**
  982. * Iterates the schemas paths similar to Array#forEach.
  983. *
  984. * The callback is passed the pathname and the schemaType instance.
  985. *
  986. * ####Example:
  987. *
  988. * const userSchema = new Schema({ name: String, registeredAt: Date });
  989. * userSchema.eachPath((pathname, schematype) => {
  990. * // Prints twice:
  991. * // name SchemaString { ... }
  992. * // registeredAt SchemaDate { ... }
  993. * console.log(pathname, schematype);
  994. * });
  995. *
  996. * @param {Function} fn callback function
  997. * @return {Schema} this
  998. * @api public
  999. */
  1000. Schema.prototype.eachPath = function(fn) {
  1001. const keys = Object.keys(this.paths);
  1002. const len = keys.length;
  1003. for (let i = 0; i < len; ++i) {
  1004. fn(keys[i], this.paths[keys[i]]);
  1005. }
  1006. return this;
  1007. };
  1008. /**
  1009. * Returns an Array of path strings that are required by this schema.
  1010. *
  1011. * ####Example:
  1012. * const s = new Schema({
  1013. * name: { type: String, required: true },
  1014. * age: { type: String, required: true },
  1015. * notes: String
  1016. * });
  1017. * s.requiredPaths(); // [ 'age', 'name' ]
  1018. *
  1019. * @api public
  1020. * @param {Boolean} invalidate refresh the cache
  1021. * @return {Array}
  1022. */
  1023. Schema.prototype.requiredPaths = function requiredPaths(invalidate) {
  1024. if (this._requiredpaths && !invalidate) {
  1025. return this._requiredpaths;
  1026. }
  1027. const paths = Object.keys(this.paths);
  1028. let i = paths.length;
  1029. const ret = [];
  1030. while (i--) {
  1031. const path = paths[i];
  1032. if (this.paths[path].isRequired) {
  1033. ret.push(path);
  1034. }
  1035. }
  1036. this._requiredpaths = ret;
  1037. return this._requiredpaths;
  1038. };
  1039. /**
  1040. * Returns indexes from fields and schema-level indexes (cached).
  1041. *
  1042. * @api private
  1043. * @return {Array}
  1044. */
  1045. Schema.prototype.indexedPaths = function indexedPaths() {
  1046. if (this._indexedpaths) {
  1047. return this._indexedpaths;
  1048. }
  1049. this._indexedpaths = this.indexes();
  1050. return this._indexedpaths;
  1051. };
  1052. /**
  1053. * Returns the pathType of `path` for this schema.
  1054. *
  1055. * Given a path, returns whether it is a real, virtual, nested, or ad-hoc/undefined path.
  1056. *
  1057. * ####Example:
  1058. * const s = new Schema({ name: String, nested: { foo: String } });
  1059. * s.virtual('foo').get(() => 42);
  1060. * s.pathType('name'); // "real"
  1061. * s.pathType('nested'); // "nested"
  1062. * s.pathType('foo'); // "virtual"
  1063. * s.pathType('fail'); // "adhocOrUndefined"
  1064. *
  1065. * @param {String} path
  1066. * @return {String}
  1067. * @api public
  1068. */
  1069. Schema.prototype.pathType = function(path) {
  1070. // Convert to '.$' to check subpaths re: gh-6405
  1071. const cleanPath = _pathToPositionalSyntax(path);
  1072. if (this.paths.hasOwnProperty(path)) {
  1073. return 'real';
  1074. }
  1075. if (this.virtuals.hasOwnProperty(path)) {
  1076. return 'virtual';
  1077. }
  1078. if (this.nested.hasOwnProperty(path)) {
  1079. return 'nested';
  1080. }
  1081. if (this.subpaths.hasOwnProperty(cleanPath) || this.subpaths.hasOwnProperty(path)) {
  1082. return 'real';
  1083. }
  1084. const singleNestedPath = this.singleNestedPaths.hasOwnProperty(cleanPath) || this.singleNestedPaths.hasOwnProperty(path);
  1085. if (singleNestedPath) {
  1086. return singleNestedPath === 'nested' ? 'nested' : 'real';
  1087. }
  1088. // Look for maps
  1089. const mapPath = getMapPath(this, path);
  1090. if (mapPath != null) {
  1091. return 'real';
  1092. }
  1093. if (/\.\d+\.|\.\d+$/.test(path)) {
  1094. return getPositionalPathType(this, path);
  1095. }
  1096. return 'adhocOrUndefined';
  1097. };
  1098. /**
  1099. * Returns true iff this path is a child of a mixed schema.
  1100. *
  1101. * @param {String} path
  1102. * @return {Boolean}
  1103. * @api private
  1104. */
  1105. Schema.prototype.hasMixedParent = function(path) {
  1106. const subpaths = path.split(/\./g);
  1107. path = '';
  1108. for (let i = 0; i < subpaths.length; ++i) {
  1109. path = i > 0 ? path + '.' + subpaths[i] : subpaths[i];
  1110. if (this.paths.hasOwnProperty(path) &&
  1111. this.paths[path] instanceof MongooseTypes.Mixed) {
  1112. return this.paths[path];
  1113. }
  1114. }
  1115. return null;
  1116. };
  1117. /**
  1118. * Setup updatedAt and createdAt timestamps to documents if enabled
  1119. *
  1120. * @param {Boolean|Object} timestamps timestamps options
  1121. * @api private
  1122. */
  1123. Schema.prototype.setupTimestamp = function(timestamps) {
  1124. return setupTimestamps(this, timestamps);
  1125. };
  1126. /*!
  1127. * ignore. Deprecated re: #6405
  1128. */
  1129. function getPositionalPathType(self, path) {
  1130. const subpaths = path.split(/\.(\d+)\.|\.(\d+)$/).filter(Boolean);
  1131. if (subpaths.length < 2) {
  1132. return self.paths.hasOwnProperty(subpaths[0]) ?
  1133. self.paths[subpaths[0]] :
  1134. 'adhocOrUndefined';
  1135. }
  1136. let val = self.path(subpaths[0]);
  1137. let isNested = false;
  1138. if (!val) {
  1139. return 'adhocOrUndefined';
  1140. }
  1141. const last = subpaths.length - 1;
  1142. for (let i = 1; i < subpaths.length; ++i) {
  1143. isNested = false;
  1144. const subpath = subpaths[i];
  1145. if (i === last && val && !/\D/.test(subpath)) {
  1146. if (val.$isMongooseDocumentArray) {
  1147. val = val.$embeddedSchemaType;
  1148. } else if (val instanceof MongooseTypes.Array) {
  1149. // StringSchema, NumberSchema, etc
  1150. val = val.caster;
  1151. } else {
  1152. val = undefined;
  1153. }
  1154. break;
  1155. }
  1156. // ignore if its just a position segment: path.0.subpath
  1157. if (!/\D/.test(subpath)) {
  1158. // Nested array
  1159. if (val instanceof MongooseTypes.Array && i !== last) {
  1160. val = val.caster;
  1161. }
  1162. continue;
  1163. }
  1164. if (!(val && val.schema)) {
  1165. val = undefined;
  1166. break;
  1167. }
  1168. const type = val.schema.pathType(subpath);
  1169. isNested = (type === 'nested');
  1170. val = val.schema.path(subpath);
  1171. }
  1172. self.subpaths[path] = val;
  1173. if (val) {
  1174. return 'real';
  1175. }
  1176. if (isNested) {
  1177. return 'nested';
  1178. }
  1179. return 'adhocOrUndefined';
  1180. }
  1181. /*!
  1182. * ignore
  1183. */
  1184. function getPositionalPath(self, path) {
  1185. getPositionalPathType(self, path);
  1186. return self.subpaths[path];
  1187. }
  1188. /**
  1189. * Adds a method call to the queue.
  1190. *
  1191. * ####Example:
  1192. *
  1193. * schema.methods.print = function() { console.log(this); };
  1194. * schema.queue('print', []); // Print the doc every one is instantiated
  1195. *
  1196. * const Model = mongoose.model('Test', schema);
  1197. * new Model({ name: 'test' }); // Prints '{"_id": ..., "name": "test" }'
  1198. *
  1199. * @param {String} name name of the document method to call later
  1200. * @param {Array} args arguments to pass to the method
  1201. * @api public
  1202. */
  1203. Schema.prototype.queue = function(name, args) {
  1204. this.callQueue.push([name, args]);
  1205. return this;
  1206. };
  1207. /**
  1208. * Defines a pre hook for the model.
  1209. *
  1210. * ####Example
  1211. *
  1212. * const toySchema = new Schema({ name: String, created: Date });
  1213. *
  1214. * toySchema.pre('save', function(next) {
  1215. * if (!this.created) this.created = new Date;
  1216. * next();
  1217. * });
  1218. *
  1219. * toySchema.pre('validate', function(next) {
  1220. * if (this.name !== 'Woody') this.name = 'Woody';
  1221. * next();
  1222. * });
  1223. *
  1224. * // Equivalent to calling `pre()` on `find`, `findOne`, `findOneAndUpdate`.
  1225. * toySchema.pre(/^find/, function(next) {
  1226. * console.log(this.getFilter());
  1227. * });
  1228. *
  1229. * // Equivalent to calling `pre()` on `updateOne`, `findOneAndUpdate`.
  1230. * toySchema.pre(['updateOne', 'findOneAndUpdate'], function(next) {
  1231. * console.log(this.getFilter());
  1232. * });
  1233. *
  1234. * toySchema.pre('deleteOne', function() {
  1235. * // Runs when you call `Toy.deleteOne()`
  1236. * });
  1237. *
  1238. * toySchema.pre('deleteOne', { document: true }, function() {
  1239. * // Runs when you call `doc.deleteOne()`
  1240. * });
  1241. *
  1242. * @param {String|RegExp} The method name or regular expression to match method name
  1243. * @param {Object} [options]
  1244. * @param {Boolean} [options.document] If `name` is a hook for both document and query middleware, set to `true` to run on document middleware. For example, set `options.document` to `true` to apply this hook to `Document#deleteOne()` rather than `Query#deleteOne()`.
  1245. * @param {Boolean} [options.query] If `name` is a hook for both document and query middleware, set to `true` to run on query middleware.
  1246. * @param {Function} callback
  1247. * @api public
  1248. */
  1249. Schema.prototype.pre = function(name) {
  1250. if (name instanceof RegExp) {
  1251. const remainingArgs = Array.prototype.slice.call(arguments, 1);
  1252. for (const fn of hookNames) {
  1253. if (name.test(fn)) {
  1254. this.pre.apply(this, [fn].concat(remainingArgs));
  1255. }
  1256. }
  1257. return this;
  1258. }
  1259. if (Array.isArray(name)) {
  1260. const remainingArgs = Array.prototype.slice.call(arguments, 1);
  1261. for (const el of name) {
  1262. this.pre.apply(this, [el].concat(remainingArgs));
  1263. }
  1264. return this;
  1265. }
  1266. this.s.hooks.pre.apply(this.s.hooks, arguments);
  1267. return this;
  1268. };
  1269. /**
  1270. * Defines a post hook for the document
  1271. *
  1272. * const schema = new Schema(..);
  1273. * schema.post('save', function (doc) {
  1274. * console.log('this fired after a document was saved');
  1275. * });
  1276. *
  1277. * schema.post('find', function(docs) {
  1278. * console.log('this fired after you ran a find query');
  1279. * });
  1280. *
  1281. * schema.post(/Many$/, function(res) {
  1282. * console.log('this fired after you ran `updateMany()` or `deleteMany()`);
  1283. * });
  1284. *
  1285. * const Model = mongoose.model('Model', schema);
  1286. *
  1287. * const m = new Model(..);
  1288. * m.save(function(err) {
  1289. * console.log('this fires after the `post` hook');
  1290. * });
  1291. *
  1292. * m.find(function(err, docs) {
  1293. * console.log('this fires after the post find hook');
  1294. * });
  1295. *
  1296. * @param {String|RegExp} The method name or regular expression to match method name
  1297. * @param {Object} [options]
  1298. * @param {Boolean} [options.document] If `name` is a hook for both document and query middleware, set to `true` to run on document middleware.
  1299. * @param {Boolean} [options.query] If `name` is a hook for both document and query middleware, set to `true` to run on query middleware.
  1300. * @param {Function} fn callback
  1301. * @see middleware http://mongoosejs.com/docs/middleware.html
  1302. * @see kareem http://npmjs.org/package/kareem
  1303. * @api public
  1304. */
  1305. Schema.prototype.post = function(name) {
  1306. if (name instanceof RegExp) {
  1307. const remainingArgs = Array.prototype.slice.call(arguments, 1);
  1308. for (const fn of hookNames) {
  1309. if (name.test(fn)) {
  1310. this.post.apply(this, [fn].concat(remainingArgs));
  1311. }
  1312. }
  1313. return this;
  1314. }
  1315. if (Array.isArray(name)) {
  1316. const remainingArgs = Array.prototype.slice.call(arguments, 1);
  1317. for (const el of name) {
  1318. this.post.apply(this, [el].concat(remainingArgs));
  1319. }
  1320. return this;
  1321. }
  1322. this.s.hooks.post.apply(this.s.hooks, arguments);
  1323. return this;
  1324. };
  1325. /**
  1326. * Registers a plugin for this schema.
  1327. *
  1328. * ####Example:
  1329. *
  1330. * const s = new Schema({ name: String });
  1331. * s.plugin(schema => console.log(schema.path('name').path));
  1332. * mongoose.model('Test', s); // Prints 'name'
  1333. *
  1334. * @param {Function} plugin callback
  1335. * @param {Object} [opts]
  1336. * @see plugins
  1337. * @api public
  1338. */
  1339. Schema.prototype.plugin = function(fn, opts) {
  1340. if (typeof fn !== 'function') {
  1341. throw new Error('First param to `schema.plugin()` must be a function, ' +
  1342. 'got "' + (typeof fn) + '"');
  1343. }
  1344. if (opts && opts.deduplicate) {
  1345. for (const plugin of this.plugins) {
  1346. if (plugin.fn === fn) {
  1347. return this;
  1348. }
  1349. }
  1350. }
  1351. this.plugins.push({ fn: fn, opts: opts });
  1352. fn(this, opts);
  1353. return this;
  1354. };
  1355. /**
  1356. * Adds an instance method to documents constructed from Models compiled from this schema.
  1357. *
  1358. * ####Example
  1359. *
  1360. * const schema = kittySchema = new Schema(..);
  1361. *
  1362. * schema.method('meow', function () {
  1363. * console.log('meeeeeoooooooooooow');
  1364. * })
  1365. *
  1366. * const Kitty = mongoose.model('Kitty', schema);
  1367. *
  1368. * const fizz = new Kitty;
  1369. * fizz.meow(); // meeeeeooooooooooooow
  1370. *
  1371. * If a hash of name/fn pairs is passed as the only argument, each name/fn pair will be added as methods.
  1372. *
  1373. * schema.method({
  1374. * purr: function () {}
  1375. * , scratch: function () {}
  1376. * });
  1377. *
  1378. * // later
  1379. * fizz.purr();
  1380. * fizz.scratch();
  1381. *
  1382. * NOTE: `Schema.method()` adds instance methods to the `Schema.methods` object. You can also add instance methods directly to the `Schema.methods` object as seen in the [guide](/docs/guide.html#methods)
  1383. *
  1384. * @param {String|Object} method name
  1385. * @param {Function} [fn]
  1386. * @api public
  1387. */
  1388. Schema.prototype.method = function(name, fn, options) {
  1389. if (typeof name !== 'string') {
  1390. for (const i in name) {
  1391. this.methods[i] = name[i];
  1392. this.methodOptions[i] = utils.clone(options);
  1393. }
  1394. } else {
  1395. this.methods[name] = fn;
  1396. this.methodOptions[name] = utils.clone(options);
  1397. }
  1398. return this;
  1399. };
  1400. /**
  1401. * Adds static "class" methods to Models compiled from this schema.
  1402. *
  1403. * ####Example
  1404. *
  1405. * const schema = new Schema(..);
  1406. * // Equivalent to `schema.statics.findByName = function(name) {}`;
  1407. * schema.static('findByName', function(name) {
  1408. * return this.find({ name: name });
  1409. * });
  1410. *
  1411. * const Drink = mongoose.model('Drink', schema);
  1412. * await Drink.findByName('LaCroix');
  1413. *
  1414. * If a hash of name/fn pairs is passed as the only argument, each name/fn pair will be added as statics.
  1415. *
  1416. * @param {String|Object} name
  1417. * @param {Function} [fn]
  1418. * @api public
  1419. * @see Statics /docs/guide.html#statics
  1420. */
  1421. Schema.prototype.static = function(name, fn) {
  1422. if (typeof name !== 'string') {
  1423. for (const i in name) {
  1424. this.statics[i] = name[i];
  1425. }
  1426. } else {
  1427. this.statics[name] = fn;
  1428. }
  1429. return this;
  1430. };
  1431. /**
  1432. * Defines an index (most likely compound) for this schema.
  1433. *
  1434. * ####Example
  1435. *
  1436. * schema.index({ first: 1, last: -1 })
  1437. *
  1438. * @param {Object} fields
  1439. * @param {Object} [options] Options to pass to [MongoDB driver's `createIndex()` function](http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#createIndex)
  1440. * @param {String | number} [options.expires=null] Mongoose-specific syntactic sugar, uses [ms](https://www.npmjs.com/package/ms) to convert `expires` option into seconds for the `expireAfterSeconds` in the above link.
  1441. * @api public
  1442. */
  1443. Schema.prototype.index = function(fields, options) {
  1444. fields || (fields = {});
  1445. options || (options = {});
  1446. if (options.expires) {
  1447. utils.expires(options);
  1448. }
  1449. this._indexes.push([fields, options]);
  1450. return this;
  1451. };
  1452. /**
  1453. * Sets a schema option.
  1454. *
  1455. * ####Example
  1456. *
  1457. * schema.set('strict'); // 'true' by default
  1458. * schema.set('strict', false); // Sets 'strict' to false
  1459. * schema.set('strict'); // 'false'
  1460. *
  1461. * @param {String} key option name
  1462. * @param {Object} [value] if not passed, the current option value is returned
  1463. * @see Schema ./
  1464. * @api public
  1465. */
  1466. Schema.prototype.set = function(key, value, _tags) {
  1467. if (arguments.length === 1) {
  1468. return this.options[key];
  1469. }
  1470. switch (key) {
  1471. case 'read':
  1472. this.options[key] = readPref(value, _tags);
  1473. this._userProvidedOptions[key] = this.options[key];
  1474. break;
  1475. case 'timestamps':
  1476. this.setupTimestamp(value);
  1477. this.options[key] = value;
  1478. this._userProvidedOptions[key] = this.options[key];
  1479. break;
  1480. case '_id':
  1481. this.options[key] = value;
  1482. this._userProvidedOptions[key] = this.options[key];
  1483. if (value && !this.paths['_id']) {
  1484. addAutoId(this);
  1485. } else if (!value && this.paths['_id'] != null && this.paths['_id'].auto) {
  1486. this.remove('_id');
  1487. }
  1488. break;
  1489. default:
  1490. this.options[key] = value;
  1491. this._userProvidedOptions[key] = this.options[key];
  1492. break;
  1493. }
  1494. return this;
  1495. };
  1496. /**
  1497. * Gets a schema option.
  1498. *
  1499. * ####Example:
  1500. *
  1501. * schema.get('strict'); // true
  1502. * schema.set('strict', false);
  1503. * schema.get('strict'); // false
  1504. *
  1505. * @param {String} key option name
  1506. * @api public
  1507. * @return {Any} the option's value
  1508. */
  1509. Schema.prototype.get = function(key) {
  1510. return this.options[key];
  1511. };
  1512. /**
  1513. * The allowed index types
  1514. *
  1515. * @receiver Schema
  1516. * @static indexTypes
  1517. * @api public
  1518. */
  1519. const indexTypes = '2d 2dsphere hashed text'.split(' ');
  1520. Object.defineProperty(Schema, 'indexTypes', {
  1521. get: function() {
  1522. return indexTypes;
  1523. },
  1524. set: function() {
  1525. throw new Error('Cannot overwrite Schema.indexTypes');
  1526. }
  1527. });
  1528. /**
  1529. * Returns a list of indexes that this schema declares, via `schema.index()` or by `index: true` in a path's options.
  1530. * Indexes are expressed as an array `[spec, options]`.
  1531. *
  1532. * ####Example:
  1533. *
  1534. * const userSchema = new Schema({
  1535. * email: { type: String, required: true, unique: true },
  1536. * registeredAt: { type: Date, index: true }
  1537. * });
  1538. *
  1539. * // [ [ { email: 1 }, { unique: true, background: true } ],
  1540. * // [ { registeredAt: 1 }, { background: true } ] ]
  1541. * userSchema.indexes();
  1542. *
  1543. * [Plugins](/docs/plugins.html) can use the return value of this function to modify a schema's indexes.
  1544. * For example, the below plugin makes every index unique by default.
  1545. *
  1546. * function myPlugin(schema) {
  1547. * for (const index of schema.indexes()) {
  1548. * if (index[1].unique === undefined) {
  1549. * index[1].unique = true;
  1550. * }
  1551. * }
  1552. * }
  1553. *
  1554. * @api public
  1555. * @return {Array} list of indexes defined in the schema
  1556. */
  1557. Schema.prototype.indexes = function() {
  1558. return getIndexes(this);
  1559. };
  1560. /**
  1561. * Creates a virtual type with the given name.
  1562. *
  1563. * @param {String} name
  1564. * @param {Object} [options]
  1565. * @param {String|Model} [options.ref] model name or model instance. Marks this as a [populate virtual](/docs/populate.html#populate-virtuals).
  1566. * @param {String|Function} [options.localField] Required for populate virtuals. See [populate virtual docs](/docs/populate.html#populate-virtuals) for more information.
  1567. * @param {String|Function} [options.foreignField] Required for populate virtuals. See [populate virtual docs](/docs/populate.html#populate-virtuals) for more information.
  1568. * @param {Boolean|Function} [options.justOne=false] Only works with populate virtuals. If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), will be a single doc or `null`. Otherwise, the populate virtual will be an array.
  1569. * @param {Boolean} [options.count=false] Only works with populate virtuals. If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), this populate virtual will contain the number of documents rather than the documents themselves when you `populate()`.
  1570. * @param {Function|null} [options.get=null] Adds a [getter](/docs/tutorials/getters-setters.html) to this virtual to transform the populated doc.
  1571. * @return {VirtualType}
  1572. */
  1573. Schema.prototype.virtual = function(name, options) {
  1574. if (name instanceof VirtualType || getConstructorName(name) === 'VirtualType') {
  1575. return this.virtual(name.path, name.options);
  1576. }
  1577. options = new VirtualOptions(options);
  1578. if (utils.hasUserDefinedProperty(options, ['ref', 'refPath'])) {
  1579. if (options.localField == null) {
  1580. throw new Error('Reference virtuals require `localField` option');
  1581. }
  1582. if (options.foreignField == null) {
  1583. throw new Error('Reference virtuals require `foreignField` option');
  1584. }
  1585. this.pre('init', function(obj) {
  1586. if (mpath.has(name, obj)) {
  1587. const _v = mpath.get(name, obj);
  1588. if (!this.$$populatedVirtuals) {
  1589. this.$$populatedVirtuals = {};
  1590. }
  1591. if (options.justOne || options.count) {
  1592. this.$$populatedVirtuals[name] = Array.isArray(_v) ?
  1593. _v[0] :
  1594. _v;
  1595. } else {
  1596. this.$$populatedVirtuals[name] = Array.isArray(_v) ?
  1597. _v :
  1598. _v == null ? [] : [_v];
  1599. }
  1600. mpath.unset(name, obj);
  1601. }
  1602. });
  1603. const virtual = this.virtual(name);
  1604. virtual.options = options;
  1605. virtual.
  1606. set(function(_v) {
  1607. if (!this.$$populatedVirtuals) {
  1608. this.$$populatedVirtuals = {};
  1609. }
  1610. if (options.justOne || options.count) {
  1611. this.$$populatedVirtuals[name] = Array.isArray(_v) ?
  1612. _v[0] :
  1613. _v;
  1614. if (typeof this.$$populatedVirtuals[name] !== 'object') {
  1615. this.$$populatedVirtuals[name] = options.count ? _v : null;
  1616. }
  1617. } else {
  1618. this.$$populatedVirtuals[name] = Array.isArray(_v) ?
  1619. _v :
  1620. _v == null ? [] : [_v];
  1621. this.$$populatedVirtuals[name] = this.$$populatedVirtuals[name].filter(function(doc) {
  1622. return doc && typeof doc === 'object';
  1623. });
  1624. }
  1625. });
  1626. if (typeof options.get === 'function') {
  1627. virtual.get(options.get);
  1628. }
  1629. // Workaround for gh-8198: if virtual is under document array, make a fake
  1630. // virtual. See gh-8210
  1631. const parts = name.split('.');
  1632. let cur = parts[0];
  1633. for (let i = 0; i < parts.length - 1; ++i) {
  1634. if (this.paths[cur] != null && this.paths[cur].$isMongooseDocumentArray) {
  1635. const remnant = parts.slice(i + 1).join('.');
  1636. this.paths[cur].schema.virtual(remnant, options);
  1637. break;
  1638. }
  1639. cur += '.' + parts[i + 1];
  1640. }
  1641. return virtual;
  1642. }
  1643. const virtuals = this.virtuals;
  1644. const parts = name.split('.');
  1645. if (this.pathType(name) === 'real') {
  1646. throw new Error('Virtual path "' + name + '"' +
  1647. ' conflicts with a real path in the schema');
  1648. }
  1649. virtuals[name] = parts.reduce(function(mem, part, i) {
  1650. mem[part] || (mem[part] = (i === parts.length - 1)
  1651. ? new VirtualType(options, name)
  1652. : {});
  1653. return mem[part];
  1654. }, this.tree);
  1655. return virtuals[name];
  1656. };
  1657. /**
  1658. * Returns the virtual type with the given `name`.
  1659. *
  1660. * @param {String} name
  1661. * @return {VirtualType}
  1662. */
  1663. Schema.prototype.virtualpath = function(name) {
  1664. return this.virtuals.hasOwnProperty(name) ? this.virtuals[name] : null;
  1665. };
  1666. /**
  1667. * Removes the given `path` (or [`paths`]).
  1668. *
  1669. * ####Example:
  1670. *
  1671. * const schema = new Schema({ name: String, age: Number });
  1672. * schema.remove('name');
  1673. * schema.path('name'); // Undefined
  1674. * schema.path('age'); // SchemaNumber { ... }
  1675. *
  1676. * @param {String|Array} path
  1677. * @return {Schema} the Schema instance
  1678. * @api public
  1679. */
  1680. Schema.prototype.remove = function(path) {
  1681. if (typeof path === 'string') {
  1682. path = [path];
  1683. }
  1684. if (Array.isArray(path)) {
  1685. path.forEach(function(name) {
  1686. if (this.path(name) == null && !this.nested[name]) {
  1687. return;
  1688. }
  1689. if (this.nested[name]) {
  1690. const allKeys = Object.keys(this.paths).
  1691. concat(Object.keys(this.nested));
  1692. for (const path of allKeys) {
  1693. if (path.startsWith(name + '.')) {
  1694. delete this.paths[path];
  1695. delete this.nested[path];
  1696. _deletePath(this, path);
  1697. }
  1698. }
  1699. delete this.nested[name];
  1700. _deletePath(this, name);
  1701. return;
  1702. }
  1703. delete this.paths[name];
  1704. _deletePath(this, name);
  1705. }, this);
  1706. }
  1707. return this;
  1708. };
  1709. /*!
  1710. * ignore
  1711. */
  1712. function _deletePath(schema, name) {
  1713. const pieces = name.split('.');
  1714. const last = pieces.pop();
  1715. let branch = schema.tree;
  1716. for (const piece of pieces) {
  1717. branch = branch[piece];
  1718. }
  1719. delete branch[last];
  1720. }
  1721. /**
  1722. * Loads an ES6 class into a schema. Maps [setters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set) + [getters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get), [static methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static),
  1723. * and [instance methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#Class_body_and_method_definitions)
  1724. * to schema [virtuals](/docs/guide.html#virtuals),
  1725. * [statics](/docs/guide.html#statics), and
  1726. * [methods](/docs/guide.html#methods).
  1727. *
  1728. * ####Example:
  1729. *
  1730. * ```javascript
  1731. * const md5 = require('md5');
  1732. * const userSchema = new Schema({ email: String });
  1733. * class UserClass {
  1734. * // `gravatarImage` becomes a virtual
  1735. * get gravatarImage() {
  1736. * const hash = md5(this.email.toLowerCase());
  1737. * return `https://www.gravatar.com/avatar/${hash}`;
  1738. * }
  1739. *
  1740. * // `getProfileUrl()` becomes a document method
  1741. * getProfileUrl() {
  1742. * return `https://mysite.com/${this.email}`;
  1743. * }
  1744. *
  1745. * // `findByEmail()` becomes a static
  1746. * static findByEmail(email) {
  1747. * return this.findOne({ email });
  1748. * }
  1749. * }
  1750. *
  1751. * // `schema` will now have a `gravatarImage` virtual, a `getProfileUrl()` method,
  1752. * // and a `findByEmail()` static
  1753. * userSchema.loadClass(UserClass);
  1754. * ```
  1755. *
  1756. * @param {Function} model
  1757. * @param {Boolean} [virtualsOnly] if truthy, only pulls virtuals from the class, not methods or statics
  1758. */
  1759. Schema.prototype.loadClass = function(model, virtualsOnly) {
  1760. if (model === Object.prototype ||
  1761. model === Function.prototype ||
  1762. model.prototype.hasOwnProperty('$isMongooseModelPrototype')) {
  1763. return this;
  1764. }
  1765. this.loadClass(Object.getPrototypeOf(model), virtualsOnly);
  1766. // Add static methods
  1767. if (!virtualsOnly) {
  1768. Object.getOwnPropertyNames(model).forEach(function(name) {
  1769. if (name.match(/^(length|name|prototype|constructor|__proto__)$/)) {
  1770. return;
  1771. }
  1772. const prop = Object.getOwnPropertyDescriptor(model, name);
  1773. if (prop.hasOwnProperty('value')) {
  1774. this.static(name, prop.value);
  1775. }
  1776. }, this);
  1777. }
  1778. // Add methods and virtuals
  1779. Object.getOwnPropertyNames(model.prototype).forEach(function(name) {
  1780. if (name.match(/^(constructor)$/)) {
  1781. return;
  1782. }
  1783. const method = Object.getOwnPropertyDescriptor(model.prototype, name);
  1784. if (!virtualsOnly) {
  1785. if (typeof method.value === 'function') {
  1786. this.method(name, method.value);
  1787. }
  1788. }
  1789. if (typeof method.get === 'function') {
  1790. if (this.virtuals[name]) {
  1791. this.virtuals[name].getters = [];
  1792. }
  1793. this.virtual(name).get(method.get);
  1794. }
  1795. if (typeof method.set === 'function') {
  1796. if (this.virtuals[name]) {
  1797. this.virtuals[name].setters = [];
  1798. }
  1799. this.virtual(name).set(method.set);
  1800. }
  1801. }, this);
  1802. return this;
  1803. };
  1804. /*!
  1805. * ignore
  1806. */
  1807. Schema.prototype._getSchema = function(path) {
  1808. const _this = this;
  1809. const pathschema = _this.path(path);
  1810. const resultPath = [];
  1811. if (pathschema) {
  1812. pathschema.$fullPath = path;
  1813. return pathschema;
  1814. }
  1815. function search(parts, schema) {
  1816. let p = parts.length + 1;
  1817. let foundschema;
  1818. let trypath;
  1819. while (p--) {
  1820. trypath = parts.slice(0, p).join('.');
  1821. foundschema = schema.path(trypath);
  1822. if (foundschema) {
  1823. resultPath.push(trypath);
  1824. if (foundschema.caster) {
  1825. // array of Mixed?
  1826. if (foundschema.caster instanceof MongooseTypes.Mixed) {
  1827. foundschema.caster.$fullPath = resultPath.join('.');
  1828. return foundschema.caster;
  1829. }
  1830. // Now that we found the array, we need to check if there
  1831. // are remaining document paths to look up for casting.
  1832. // Also we need to handle array.$.path since schema.path
  1833. // doesn't work for that.
  1834. // If there is no foundschema.schema we are dealing with
  1835. // a path like array.$
  1836. if (p !== parts.length) {
  1837. if (foundschema.schema) {
  1838. let ret;
  1839. if (parts[p] === '$' || isArrayFilter(parts[p])) {
  1840. if (p + 1 === parts.length) {
  1841. // comments.$
  1842. return foundschema;
  1843. }
  1844. // comments.$.comments.$.title
  1845. ret = search(parts.slice(p + 1), foundschema.schema);
  1846. if (ret) {
  1847. ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
  1848. !foundschema.schema.$isSingleNested;
  1849. }
  1850. return ret;
  1851. }
  1852. // this is the last path of the selector
  1853. ret = search(parts.slice(p), foundschema.schema);
  1854. if (ret) {
  1855. ret.$isUnderneathDocArray = ret.$isUnderneathDocArray ||
  1856. !foundschema.schema.$isSingleNested;
  1857. }
  1858. return ret;
  1859. }
  1860. }
  1861. } else if (foundschema.$isSchemaMap) {
  1862. if (p >= parts.length) {
  1863. return foundschema;
  1864. }
  1865. // Any path in the map will be an instance of the map's embedded schematype
  1866. if (p + 1 >= parts.length) {
  1867. return foundschema.$__schemaType;
  1868. }
  1869. const ret = search(parts.slice(p + 1), foundschema.$__schemaType.schema);
  1870. return ret;
  1871. }
  1872. foundschema.$fullPath = resultPath.join('.');
  1873. return foundschema;
  1874. }
  1875. }
  1876. }
  1877. // look for arrays
  1878. const parts = path.split('.');
  1879. for (let i = 0; i < parts.length; ++i) {
  1880. if (parts[i] === '$' || isArrayFilter(parts[i])) {
  1881. // Re: gh-5628, because `schema.path()` doesn't take $ into account.
  1882. parts[i] = '0';
  1883. }
  1884. }
  1885. return search(parts, _this);
  1886. };
  1887. /*!
  1888. * ignore
  1889. */
  1890. Schema.prototype._getPathType = function(path) {
  1891. const _this = this;
  1892. const pathschema = _this.path(path);
  1893. if (pathschema) {
  1894. return 'real';
  1895. }
  1896. function search(parts, schema) {
  1897. let p = parts.length + 1,
  1898. foundschema,
  1899. trypath;
  1900. while (p--) {
  1901. trypath = parts.slice(0, p).join('.');
  1902. foundschema = schema.path(trypath);
  1903. if (foundschema) {
  1904. if (foundschema.caster) {
  1905. // array of Mixed?
  1906. if (foundschema.caster instanceof MongooseTypes.Mixed) {
  1907. return { schema: foundschema, pathType: 'mixed' };
  1908. }
  1909. // Now that we found the array, we need to check if there
  1910. // are remaining document paths to look up for casting.
  1911. // Also we need to handle array.$.path since schema.path
  1912. // doesn't work for that.
  1913. // If there is no foundschema.schema we are dealing with
  1914. // a path like array.$
  1915. if (p !== parts.length && foundschema.schema) {
  1916. if (parts[p] === '$' || isArrayFilter(parts[p])) {
  1917. if (p === parts.length - 1) {
  1918. return { schema: foundschema, pathType: 'nested' };
  1919. }
  1920. // comments.$.comments.$.title
  1921. return search(parts.slice(p + 1), foundschema.schema);
  1922. }
  1923. // this is the last path of the selector
  1924. return search(parts.slice(p), foundschema.schema);
  1925. }
  1926. return {
  1927. schema: foundschema,
  1928. pathType: foundschema.$isSingleNested ? 'nested' : 'array'
  1929. };
  1930. }
  1931. return { schema: foundschema, pathType: 'real' };
  1932. } else if (p === parts.length && schema.nested[trypath]) {
  1933. return { schema: schema, pathType: 'nested' };
  1934. }
  1935. }
  1936. return { schema: foundschema || schema, pathType: 'undefined' };
  1937. }
  1938. // look for arrays
  1939. return search(path.split('.'), _this);
  1940. };
  1941. /*!
  1942. * ignore
  1943. */
  1944. function isArrayFilter(piece) {
  1945. return piece.startsWith('$[') && piece.endsWith(']');
  1946. }
  1947. /*!
  1948. * Called by `compile()` _right before_ compiling. Good for making any changes to
  1949. * the schema that should respect options set by plugins, like `id`
  1950. */
  1951. Schema.prototype._preCompile = function _preCompile() {
  1952. idGetter(this);
  1953. };
  1954. /*!
  1955. * Module exports.
  1956. */
  1957. module.exports = exports = Schema;
  1958. // require down here because of reference issues
  1959. /**
  1960. * The various built-in Mongoose Schema Types.
  1961. *
  1962. * ####Example:
  1963. *
  1964. * const mongoose = require('mongoose');
  1965. * const ObjectId = mongoose.Schema.Types.ObjectId;
  1966. *
  1967. * ####Types:
  1968. *
  1969. * - [String](/docs/schematypes.html#strings)
  1970. * - [Number](/docs/schematypes.html#numbers)
  1971. * - [Boolean](/docs/schematypes.html#booleans) | Bool
  1972. * - [Array](/docs/schematypes.html#arrays)
  1973. * - [Buffer](/docs/schematypes.html#buffers)
  1974. * - [Date](/docs/schematypes.html#dates)
  1975. * - [ObjectId](/docs/schematypes.html#objectids) | Oid
  1976. * - [Mixed](/docs/schematypes.html#mixed)
  1977. *
  1978. * Using this exposed access to the `Mixed` SchemaType, we can use them in our schema.
  1979. *
  1980. * const Mixed = mongoose.Schema.Types.Mixed;
  1981. * new mongoose.Schema({ _user: Mixed })
  1982. *
  1983. * @api public
  1984. */
  1985. Schema.Types = MongooseTypes = require('./schema/index');
  1986. /*!
  1987. * ignore
  1988. */
  1989. exports.ObjectId = MongooseTypes.ObjectId;