mongoose 6.5.1 → 6.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/connection.js CHANGED
@@ -1117,7 +1117,7 @@ Connection.prototype.collection = function(name, options) {
1117
1117
  * @param {Function} fn plugin callback
1118
1118
  * @param {Object} [opts] optional options
1119
1119
  * @return {Connection} this
1120
- * @see plugins ./plugins.html
1120
+ * @see plugins /docs/plugins
1121
1121
  * @api public
1122
1122
  */
1123
1123
 
@@ -54,7 +54,7 @@ class ChangeStream extends EventEmitter {
54
54
 
55
55
  ['close', 'change', 'end', 'error'].forEach(ev => {
56
56
  this.driverChangeStream.on(ev, data => {
57
- if (data.fullDocument != null && this.options && this.options.hydrate) {
57
+ if (data != null && data.fullDocument != null && this.options && this.options.hydrate) {
58
58
  data.fullDocument = this.options.model.hydrate(data.fullDocument);
59
59
  }
60
60
  this.emit(ev, data);
package/lib/document.js CHANGED
@@ -4217,6 +4217,7 @@ Document.prototype.equals = function(doc) {
4217
4217
  *
4218
4218
  * #### Example:
4219
4219
  *
4220
+ * // Given a document, `populate()` lets you pull in referenced docs
4220
4221
  * await doc.populate([
4221
4222
  * 'stories',
4222
4223
  * { path: 'fans', sort: { name: -1 } }
@@ -4225,12 +4226,15 @@ Document.prototype.equals = function(doc) {
4225
4226
  * doc.stories[0].title; // 'Casino Royale'
4226
4227
  * doc.populated('fans'); // Array of ObjectIds
4227
4228
  *
4228
- * await doc.populate('fans', '-email');
4229
- * doc.fans[0].email // not populated
4229
+ * // If the referenced doc has been deleted, `populate()` will
4230
+ * // remove that entry from the array.
4231
+ * await Story.delete({ title: 'Casino Royale' });
4232
+ * await doc.populate('stories'); // Empty array
4230
4233
  *
4231
- * await doc.populate('author fans', '-email');
4232
- * doc.author.email // not populated
4233
- * doc.fans[0].email // not populated
4234
+ * // You can also pass additional query options to `populate()`,
4235
+ * // like projections:
4236
+ * await doc.populate('fans', '-email');
4237
+ * doc.fans[0].email // undefined because of 2nd param `select`
4234
4238
  *
4235
4239
  * @param {String|Object|Array} path either the path to populate or an object specifying all parameters, or either an array of those
4236
4240
  * @param {Object|String} [select] Field selection for the population query
@@ -4246,7 +4250,7 @@ Document.prototype.equals = function(doc) {
4246
4250
  * @param {Function} [options.transform=null] Function that Mongoose will call on every populated document that allows you to transform the populated document.
4247
4251
  * @param {Object} [options.options=null] Additional options like `limit` and `lean`.
4248
4252
  * @param {Function} [callback] Callback
4249
- * @see population ./populate.html
4253
+ * @see population /docs/populate
4250
4254
  * @see Query#select #query_Query-select
4251
4255
  * @see Model.populate #model_Model-populate
4252
4256
  * @memberOf Document
@@ -7,23 +7,32 @@ const emittedSymbol = Symbol('mongoose:emitted');
7
7
 
8
8
  module.exports = function promiseOrCallback(callback, fn, ee, Promise) {
9
9
  if (typeof callback === 'function') {
10
- return fn(function(error) {
11
- if (error != null) {
12
- if (ee != null && ee.listeners != null && ee.listeners('error').length > 0 && !error[emittedSymbol]) {
13
- error[emittedSymbol] = true;
14
- ee.emit('error', error);
10
+ try {
11
+ return fn(function(error) {
12
+ if (error != null) {
13
+ if (ee != null && ee.listeners != null && ee.listeners('error').length > 0 && !error[emittedSymbol]) {
14
+ error[emittedSymbol] = true;
15
+ ee.emit('error', error);
16
+ }
17
+ try {
18
+ callback(error);
19
+ } catch (error) {
20
+ return immediate(() => {
21
+ throw error;
22
+ });
23
+ }
24
+ return;
15
25
  }
16
- try {
17
- callback(error);
18
- } catch (error) {
19
- return immediate(() => {
20
- throw error;
21
- });
22
- }
23
- return;
26
+ callback.apply(this, arguments);
27
+ });
28
+ } catch (error) {
29
+ if (ee != null && ee.listeners != null && ee.listeners('error').length > 0 && !error[emittedSymbol]) {
30
+ error[emittedSymbol] = true;
31
+ ee.emit('error', error);
24
32
  }
25
- callback.apply(this, arguments);
26
- });
33
+
34
+ return callback(error);
35
+ }
27
36
  }
28
37
 
29
38
  Promise = Promise || PromiseProvider.get();
package/lib/index.js CHANGED
@@ -688,7 +688,7 @@ Mongoose.prototype._applyPlugins = function(schema, options) {
688
688
  * @param {Function} fn plugin callback
689
689
  * @param {Object} [opts] optional options
690
690
  * @return {Mongoose} this
691
- * @see plugins ./plugins.html
691
+ * @see plugins /docs/plugins
692
692
  * @api public
693
693
  */
694
694
 
package/lib/model.js CHANGED
@@ -3120,10 +3120,9 @@ Model.create = function create(doc, options, callback) {
3120
3120
  }
3121
3121
  const _done = (error, res) => {
3122
3122
  const savedDocs = [];
3123
- const len = res.length;
3124
- for (let i = 0; i < len; ++i) {
3125
- if (res[i].doc) {
3126
- savedDocs.push(res[i].doc);
3123
+ for (const val of res) {
3124
+ if (val.doc) {
3125
+ savedDocs.push(val.doc);
3127
3126
  }
3128
3127
  }
3129
3128
 
@@ -3575,13 +3574,17 @@ Model.bulkWrite = function(ops, options, callback) {
3575
3574
  return cb(null, getDefaultBulkwriteResult());
3576
3575
  }
3577
3576
 
3578
- this.$__collection.bulkWrite(ops, options, (error, res) => {
3579
- if (error) {
3580
- return cb(error);
3581
- }
3577
+ try {
3578
+ this.$__collection.bulkWrite(ops, options, (error, res) => {
3579
+ if (error) {
3580
+ return cb(error);
3581
+ }
3582
3582
 
3583
- cb(null, res);
3584
- });
3583
+ cb(null, res);
3584
+ });
3585
+ } catch (err) {
3586
+ return cb(err);
3587
+ }
3585
3588
  });
3586
3589
  }, this.events);
3587
3590
  };
@@ -4189,7 +4192,7 @@ function _update(model, op, conditions, doc, options, callback) {
4189
4192
  * @param {String} [opts.out.reduce] add the results to collectionName: if dups are detected, uses the reducer / finalize functions
4190
4193
  * @param {String} [opts.out.merge] add the results to collectionName: if dups exist the new docs overwrite the old
4191
4194
  * @param {Function} [callback] optional callback
4192
- * @see https://www.mongodb.org/display/DOCS/MapReduce
4195
+ * @see MongoDB MapReduce https://www.mongodb.org/display/DOCS/MapReduce
4193
4196
  * @return {Promise}
4194
4197
  * @api public
4195
4198
  */
package/lib/query.js CHANGED
@@ -166,7 +166,7 @@ Query.base = mquery.prototype;
166
166
  *
167
167
  * MongoDB 2.4 deprecated the use of `$within`, replacing it with `$geoWithin`. Mongoose uses `$geoWithin` by default (which is 100% backward compatible with `$within`). If you are running an older version of MongoDB, set this flag to `false` so your `within()` queries continue to work.
168
168
  *
169
- * @see https://docs.mongodb.org/manual/reference/operator/geoWithin/
169
+ * @see geoWithin https://docs.mongodb.org/manual/reference/operator/geoWithin/
170
170
  * @default true
171
171
  * @property use$geoWithin
172
172
  * @memberOf Query
@@ -1072,7 +1072,7 @@ Query.prototype.projection = function(arg) {
1072
1072
  * @instance
1073
1073
  * @param {Object|String|String[]} arg
1074
1074
  * @return {Query} this
1075
- * @see SchemaType
1075
+ * @see SchemaType /docs/api/schematype
1076
1076
  * @api public
1077
1077
  */
1078
1078
 
@@ -1599,6 +1599,8 @@ Query.prototype.setOptions = function(options, overwrite) {
1599
1599
  throw new Error('Options must be an object, got "' + options + '"');
1600
1600
  }
1601
1601
 
1602
+ options = Object.assign({}, options);
1603
+
1602
1604
  if (Array.isArray(options.populate)) {
1603
1605
  const populate = options.populate;
1604
1606
  delete options.populate;
@@ -2042,6 +2044,9 @@ Query.prototype.set = function(path, val) {
2042
2044
  }
2043
2045
 
2044
2046
  this._update = this._update || {};
2047
+ if (path in this._update) {
2048
+ delete this._update[path];
2049
+ }
2045
2050
  this._update.$set = this._update.$set || {};
2046
2051
  this._update.$set[path] = val;
2047
2052
  return this;
@@ -5071,7 +5076,7 @@ function castQuery(query) {
5071
5076
  * @param {Object|Function} [options.match=null] Add an additional filter to the populate query. Can be a filter object containing [MongoDB query syntax](https://docs.mongodb.com/manual/tutorial/query-documents/), or a function that returns a filter object.
5072
5077
  * @param {Function} [options.transform=null] Function that Mongoose will call on every populated document that allows you to transform the populated document.
5073
5078
  * @param {Object} [options.options=null] Additional options like `limit` and `lean`.
5074
- * @see population ./populate.html
5079
+ * @see population /docs/populate
5075
5080
  * @see Query#select #query_Query-select
5076
5081
  * @see Model.populate #model_Model-populate
5077
5082
  * @return {Query} this
@@ -5342,7 +5347,7 @@ Query.prototype._applyPaths = function applyPaths() {
5342
5347
  *
5343
5348
  * @return {QueryCursor}
5344
5349
  * @param {Object} [options]
5345
- * @see QueryCursor
5350
+ * @see QueryCursor /docs/api/querycursor
5346
5351
  * @api public
5347
5352
  */
5348
5353
 
@@ -5498,8 +5503,8 @@ Query.prototype.tailable = function(val, opts) {
5498
5503
  * @param {Object} object Must contain a `type` property which is a String and a `coordinates` property which is an Array. See the examples.
5499
5504
  * @return {Query} this
5500
5505
  * @see $geometry https://docs.mongodb.org/manual/reference/operator/geometry/
5501
- * @see https://docs.mongodb.org/manual/release-notes/2.4/#new-geospatial-indexes-with-geojson-and-improved-spherical-geometry
5502
- * @see https://www.mongodb.org/display/DOCS/Geospatial+Indexing
5506
+ * @see Geospatial Support Enhancements https://www.mongodb.com/docs/manual/release-notes/2.4/#geospatial-support-enhancements
5507
+ * @see MongoDB Geospatial Indexing https://www.mongodb.org/display/DOCS/Geospatial+Indexing
5503
5508
  * @api public
5504
5509
  */
5505
5510
 
@@ -5524,7 +5529,7 @@ Query.prototype.tailable = function(val, opts) {
5524
5529
  * @see $near https://docs.mongodb.org/manual/reference/operator/near/
5525
5530
  * @see $nearSphere https://docs.mongodb.org/manual/reference/operator/nearSphere/
5526
5531
  * @see $maxDistance https://docs.mongodb.org/manual/reference/operator/maxDistance/
5527
- * @see https://www.mongodb.org/display/DOCS/Geospatial+Indexing
5532
+ * @see MongoDB Geospatial Indexing https://www.mongodb.org/display/DOCS/Geospatial+Indexing
5528
5533
  * @api public
5529
5534
  */
5530
5535
 
@@ -5660,7 +5665,7 @@ if (Symbol.asyncIterator != null) {
5660
5665
  * @param {Array|Object} [coordinatePairs...]
5661
5666
  * @return {Query} this
5662
5667
  * @see $polygon https://docs.mongodb.org/manual/reference/operator/polygon/
5663
- * @see https://www.mongodb.org/display/DOCS/Geospatial+Indexing
5668
+ * @see MongoDB Geospatial Indexing https://www.mongodb.org/display/DOCS/Geospatial+Indexing
5664
5669
  * @api public
5665
5670
  */
5666
5671
 
@@ -5680,7 +5685,7 @@ if (Symbol.asyncIterator != null) {
5680
5685
  * @instance
5681
5686
  * @see $box https://docs.mongodb.org/manual/reference/operator/box/
5682
5687
  * @see within() Query#within #query_Query-within
5683
- * @see https://www.mongodb.org/display/DOCS/Geospatial+Indexing
5688
+ * @see MongoDB Geospatial Indexing https://www.mongodb.org/display/DOCS/Geospatial+Indexing
5684
5689
  * @param {Object|Array<Number>} val1 Lower Left Coordinates OR a object of lower-left(ll) and upper-right(ur) Coordinates
5685
5690
  * @param {Array<Number>} [val2] Upper Right Coordinates
5686
5691
  * @return {Query} this
@@ -5726,7 +5731,7 @@ Query.prototype.box = function(ll, ur) {
5726
5731
  * @see $center https://docs.mongodb.org/manual/reference/operator/center/
5727
5732
  * @see $centerSphere https://docs.mongodb.org/manual/reference/operator/centerSphere/
5728
5733
  * @see $geoWithin https://docs.mongodb.org/manual/reference/operator/geoWithin/
5729
- * @see https://www.mongodb.org/display/DOCS/Geospatial+Indexing
5734
+ * @see MongoDB Geospatial Indexing https://www.mongodb.org/display/DOCS/Geospatial+Indexing
5730
5735
  * @api public
5731
5736
  */
5732
5737
 
@@ -5758,7 +5763,7 @@ Query.prototype.center = Query.base.circle;
5758
5763
  * @param {String} [path]
5759
5764
  * @param {Object} val
5760
5765
  * @return {Query} this
5761
- * @see https://www.mongodb.org/display/DOCS/Geospatial+Indexing
5766
+ * @see MongoDB Geospatial Indexing https://www.mongodb.org/display/DOCS/Geospatial+Indexing
5762
5767
  * @see $centerSphere https://docs.mongodb.org/manual/reference/operator/centerSphere/
5763
5768
  * @api public
5764
5769
  */
package/lib/schema.js CHANGED
@@ -1868,7 +1868,7 @@ Schema.prototype.index = function(fields, options) {
1868
1868
  *
1869
1869
  * @param {String} key The name of the option to set the value to
1870
1870
  * @param {Object} [value] The value to set the option to, if not passed, the option will be reset to default
1871
- * @see Schema ./
1871
+ * @see Schema #schema_Schema
1872
1872
  * @api public
1873
1873
  */
1874
1874
 
@@ -177,7 +177,7 @@ utils.each(
177
177
  * bson.BSON_BINARY_SUBTYPE_USER_DEFINED
178
178
  * doc.buffer.toObject(bson.BSON_BINARY_SUBTYPE_USER_DEFINED);
179
179
  *
180
- * @see https://bsonspec.org/#/specification
180
+ * @see bsonspec https://bsonspec.org/#/specification
181
181
  * @param {Hex} [subtype]
182
182
  * @return {Binary}
183
183
  * @api public
@@ -249,7 +249,7 @@ MongooseBuffer.mixin.equals = function(other) {
249
249
  *
250
250
  * doc.buffer.subtype(bson.BSON_BINARY_SUBTYPE_UUID);
251
251
  *
252
- * @see https://bsonspec.org/#/specification
252
+ * @see bsonspec https://bsonspec.org/#/specification
253
253
  * @param {Hex} subtype
254
254
  * @api public
255
255
  * @method subtype
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "6.5.1",
4
+ "version": "6.5.2",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -77,18 +77,18 @@
77
77
  "docs:copy:tmp:legacy": "rimraf ./docs/5.x && ncp ./tmp ./docs/5.x",
78
78
  "docs:checkout:gh-pages": "git checkout gh-pages",
79
79
  "docs:checkout:legacy": "git checkout 5.x",
80
- "docs:generate": "node website.js",
80
+ "docs:generate": "node ./scripts/website.js",
81
81
  "docs:generate:search": "node docs/search.js",
82
82
  "docs:merge:stable": "git merge master",
83
83
  "docs:merge:legacy": "git merge 5.x",
84
84
  "docs:test": "npm run docs:generate && npm run docs:generate:search",
85
- "docs:view": "node website.js && node static.js",
85
+ "docs:view": "node ./scripts/static.js",
86
86
  "docs:prepare:publish:stable": "npm run docs:checkout:gh-pages && npm run docs:merge:stable && npm run docs:clean:stable && npm run docs:generate && npm run docs:generate:search",
87
87
  "docs:prepare:publish:legacy": "npm run docs:checkout:legacy && npm run docs:merge:legacy && npm run docs:clean:stable && npm run docs:generate && npm run docs:copy:tmp && docs:checkout:gh-pages && docs:copy:tmp:legacy",
88
88
  "lint": "eslint .",
89
89
  "lint-js": "eslint . --ext .js",
90
90
  "lint-ts": "eslint . --ext .ts",
91
- "build-browser": "node build-browser.js",
91
+ "build-browser": "node ./scripts/build-browser.js",
92
92
  "prepublishOnly": "npm run build-browser",
93
93
  "release": "git pull && git push origin master --tags && npm publish",
94
94
  "release-legacy": "git pull origin 5.x && git push origin 5.x --tags && npm publish --tag legacy",
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const config = require('./webpack.config.js');
3
+ const config = require('../webpack.config.js');
4
4
  const webpack = require('webpack');
5
5
 
6
6
  const compiler = webpack(config);
@@ -142,9 +142,15 @@ declare module 'mongoose' {
142
142
  readonly models: Readonly<{ [index: string]: Model<any> }>;
143
143
 
144
144
  /** Defines or retrieves a model. */
145
+ model<TSchema extends Schema = any>(
146
+ name: string,
147
+ schema?: TSchema,
148
+ collection?: string,
149
+ options?: CompileModelOptions
150
+ ): Model<InferSchemaType<TSchema>, ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>, ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>, {}, TSchema> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;
145
151
  model<T, U, TQueryHelpers = {}>(
146
152
  name: string,
147
- schema?: Schema<T, any, any, TQueryHelpers, any, any>,
153
+ schema?: Schema<T, U, any, TQueryHelpers, any, any>,
148
154
  collection?: string,
149
155
  options?: CompileModelOptions
150
156
  ): U;
package/types/index.d.ts CHANGED
@@ -157,9 +157,20 @@ declare module 'mongoose' {
157
157
 
158
158
  type QueryResultType<T> = T extends Query<infer ResultType, any> ? ResultType : never;
159
159
 
160
- type PluginFunction<DocType> = (schema: Schema<DocType>, opts?: any) => void;
161
-
162
- export class Schema<EnforcedDocType = any, M = Model<EnforcedDocType, any, any, any>, TInstanceMethods = {}, TQueryHelpers = {}, TVirtuals = {},
160
+ type PluginFunction<
161
+ DocType,
162
+ M = Model<DocType, any, any, any>,
163
+ TInstanceMethods = {},
164
+ TQueryHelpers = {},
165
+ TVirtuals = {},
166
+ TStaticMethods = {}> = (schema: Schema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods>, opts?: any) => void;
167
+
168
+ export class Schema<
169
+ EnforcedDocType = any,
170
+ M = Model<EnforcedDocType, any, any, any>,
171
+ TInstanceMethods = {},
172
+ TQueryHelpers = {},
173
+ TVirtuals = {},
163
174
  TStaticMethods = {},
164
175
  TPathTypeKey extends TypeKeyBaseType = DefaultTypeKey,
165
176
  DocType extends ObtainDocumentType<DocType, EnforcedDocType, TPathTypeKey> = ObtainDocumentType<any, EnforcedDocType, TPathTypeKey>>
@@ -241,7 +252,7 @@ declare module 'mongoose' {
241
252
  pathType(path: string): string;
242
253
 
243
254
  /** Registers a plugin for this schema. */
244
- plugin<PFunc extends PluginFunction<DocType>, POptions extends Parameters<PFunc>[1] = Parameters<PFunc>[1]>(fn: PFunc, opts?: POptions): this;
255
+ plugin<PFunc extends PluginFunction<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods>, POptions extends Parameters<PFunc>[1] = Parameters<PFunc>[1]>(fn: PFunc, opts?: POptions): this;
245
256
 
246
257
  /** Defines a post hook for the model. */
247
258
  post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: PostMiddlewareFunction<T, T>): this;
@@ -294,15 +305,16 @@ declare module 'mongoose' {
294
305
  set<K extends keyof SchemaOptions>(key: K, value: SchemaOptions[K], _tags?: any): this;
295
306
 
296
307
  /** Adds static "class" methods to Models compiled from this schema. */
308
+ static<K extends keyof TStaticMethods>(name: K, fn: TStaticMethods[K]): this;
309
+ static(obj: { [F in keyof TStaticMethods]: TStaticMethods[F] } & { [name: string]: (this: M, ...args: any[]) => any }): this;
297
310
  static(name: string, fn: (this: M, ...args: any[]) => any): this;
298
- static(obj: { [name: string]: (this: M, ...args: any[]) => any }): this;
299
311
 
300
312
  /** Object of currently defined statics on this schema. */
301
- statics: { [name: string]: (this: M, ...args: any[]) => any };
313
+ statics: { [F in keyof TStaticMethods]: TStaticMethods[F] } & { [name: string]: (this: M, ...args: any[]) => any };
302
314
 
303
315
  /** Creates a virtual type with the given name. */
304
316
  virtual<T = HydratedDocument<DocType, TInstanceMethods>>(
305
- name: string,
317
+ name: keyof TVirtuals | string,
306
318
  options?: VirtualTypeOptions<T, DocType>
307
319
  ): VirtualType<T>;
308
320
 
package/types/models.d.ts CHANGED
@@ -144,9 +144,9 @@ declare module 'mongoose' {
144
144
  * if you use `create()`) because with `bulkWrite()` there is only one network
145
145
  * round trip to the MongoDB server.
146
146
  */
147
- bulkWrite(writes: Array<mongodb.AnyBulkWriteOperation<T>>, options: mongodb.BulkWriteOptions & MongooseBulkWriteOptions, callback: Callback<mongodb.BulkWriteResult>): void;
148
- bulkWrite(writes: Array<mongodb.AnyBulkWriteOperation<T>>, callback: Callback<mongodb.BulkWriteResult>): void;
149
- bulkWrite(writes: Array<mongodb.AnyBulkWriteOperation<T>>, options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions): Promise<mongodb.BulkWriteResult>;
147
+ bulkWrite(writes: Array<mongodb.AnyBulkWriteOperation<T extends {} ? T : any>>, options: mongodb.BulkWriteOptions & MongooseBulkWriteOptions, callback: Callback<mongodb.BulkWriteResult>): void;
148
+ bulkWrite(writes: Array<mongodb.AnyBulkWriteOperation<T extends {} ? T : any>>, callback: Callback<mongodb.BulkWriteResult>): void;
149
+ bulkWrite(writes: Array<mongodb.AnyBulkWriteOperation<T extends {} ? T : any>>, options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions): Promise<mongodb.BulkWriteResult>;
150
150
 
151
151
  /**
152
152
  * Sends multiple `save()` calls in a single `bulkWrite()`. This is faster than