schema.js 53 KB

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