schema.js 52 KB

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