mongoose 8.20.0 → 9.0.0-rc0

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.
Files changed (90) hide show
  1. package/eslint.config.mjs +198 -0
  2. package/lib/aggregate.js +17 -73
  3. package/lib/cast/bigint.js +1 -1
  4. package/lib/cast/double.js +1 -1
  5. package/lib/cast/uuid.js +5 -48
  6. package/lib/cast.js +2 -2
  7. package/lib/connection.js +0 -1
  8. package/lib/cursor/aggregationCursor.js +14 -24
  9. package/lib/cursor/queryCursor.js +7 -14
  10. package/lib/document.js +125 -121
  11. package/lib/drivers/node-mongodb-native/connection.js +3 -10
  12. package/lib/error/objectParameter.js +1 -2
  13. package/lib/error/validation.js +0 -8
  14. package/lib/helpers/clone.js +1 -1
  15. package/lib/helpers/common.js +1 -1
  16. package/lib/helpers/indexes/isIndexEqual.js +0 -1
  17. package/lib/helpers/model/applyDefaultsToPOJO.js +2 -2
  18. package/lib/helpers/model/applyHooks.js +43 -53
  19. package/lib/helpers/model/applyMethods.js +2 -2
  20. package/lib/helpers/model/applyStaticHooks.js +1 -48
  21. package/lib/helpers/model/castBulkWrite.js +1 -1
  22. package/lib/helpers/parallelLimit.js +18 -36
  23. package/lib/helpers/pluralize.js +3 -3
  24. package/lib/helpers/populate/assignRawDocsToIdStructure.js +1 -8
  25. package/lib/helpers/populate/createPopulateQueryFilter.js +1 -1
  26. package/lib/helpers/populate/getModelsMapForPopulate.js +17 -9
  27. package/lib/helpers/populate/getSchemaTypes.js +5 -5
  28. package/lib/helpers/query/cast$expr.js +8 -10
  29. package/lib/helpers/query/castFilterPath.js +1 -1
  30. package/lib/helpers/query/castUpdate.js +14 -12
  31. package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +1 -1
  32. package/lib/helpers/schema/applyPlugins.js +1 -1
  33. package/lib/helpers/schema/getIndexes.js +1 -7
  34. package/lib/helpers/timestamps/setupTimestamps.js +3 -6
  35. package/lib/helpers/updateValidators.js +57 -111
  36. package/lib/model.js +419 -607
  37. package/lib/mongoose.js +41 -13
  38. package/lib/plugins/saveSubdocs.js +24 -51
  39. package/lib/plugins/sharding.js +5 -4
  40. package/lib/plugins/validateBeforeSave.js +3 -13
  41. package/lib/query.js +101 -145
  42. package/lib/queryHelpers.js +2 -2
  43. package/lib/schema/array.js +41 -84
  44. package/lib/schema/documentArray.js +57 -94
  45. package/lib/schema/documentArrayElement.js +16 -11
  46. package/lib/schema/string.js +1 -1
  47. package/lib/schema/subdocument.js +22 -28
  48. package/lib/schema/uuid.js +0 -21
  49. package/lib/schema.js +81 -39
  50. package/lib/schemaType.js +39 -57
  51. package/lib/types/array/index.js +2 -2
  52. package/lib/types/array/methods/index.js +4 -4
  53. package/lib/types/arraySubdocument.js +1 -1
  54. package/lib/types/buffer.js +10 -10
  55. package/lib/types/decimal128.js +1 -1
  56. package/lib/types/documentArray/index.js +1 -1
  57. package/lib/types/documentArray/methods/index.js +5 -3
  58. package/lib/types/double.js +1 -1
  59. package/lib/types/objectid.js +1 -1
  60. package/lib/types/subdocument.js +15 -43
  61. package/lib/types/uuid.js +1 -1
  62. package/lib/utils.js +1 -8
  63. package/lib/validOptions.js +3 -3
  64. package/package.json +11 -24
  65. package/types/connection.d.ts +20 -11
  66. package/types/document.d.ts +95 -26
  67. package/types/index.d.ts +143 -39
  68. package/types/inferhydrateddoctype.d.ts +115 -0
  69. package/types/inferrawdoctype.d.ts +99 -75
  70. package/types/inferschematype.d.ts +17 -3
  71. package/types/middlewares.d.ts +0 -2
  72. package/types/models.d.ts +131 -199
  73. package/types/mongooseoptions.d.ts +6 -5
  74. package/types/pipelinestage.d.ts +1 -1
  75. package/types/query.d.ts +71 -139
  76. package/types/schemaoptions.d.ts +1 -1
  77. package/types/schematypes.d.ts +14 -10
  78. package/types/types.d.ts +3 -4
  79. package/types/utility.d.ts +68 -48
  80. package/types/validation.d.ts +18 -14
  81. package/browser.js +0 -8
  82. package/dist/browser.umd.js +0 -2
  83. package/lib/browser.js +0 -141
  84. package/lib/browserDocument.js +0 -101
  85. package/lib/documentProvider.js +0 -30
  86. package/lib/drivers/browser/binary.js +0 -14
  87. package/lib/drivers/browser/decimal128.js +0 -7
  88. package/lib/drivers/browser/index.js +0 -13
  89. package/lib/drivers/browser/objectid.js +0 -29
  90. package/lib/helpers/promiseOrCallback.js +0 -54
package/lib/browser.js DELETED
@@ -1,141 +0,0 @@
1
- /* eslint-env browser */
2
-
3
- 'use strict';
4
-
5
- require('./driver').set(require('./drivers/browser'));
6
-
7
- const DocumentProvider = require('./documentProvider.js');
8
-
9
- DocumentProvider.setBrowser(true);
10
-
11
- /**
12
- * The [MongooseError](https://mongoosejs.com/docs/api/error.html#Error()) constructor.
13
- *
14
- * @method Error
15
- * @api public
16
- */
17
-
18
- exports.Error = require('./error/index');
19
-
20
- /**
21
- * The Mongoose [Schema](https://mongoosejs.com/docs/api/schema.html#Schema()) constructor
22
- *
23
- * #### Example:
24
- *
25
- * const mongoose = require('mongoose');
26
- * const Schema = mongoose.Schema;
27
- * const CatSchema = new Schema(..);
28
- *
29
- * @method Schema
30
- * @api public
31
- */
32
-
33
- exports.Schema = require('./schema');
34
-
35
- /**
36
- * The various Mongoose Types.
37
- *
38
- * #### Example:
39
- *
40
- * const mongoose = require('mongoose');
41
- * const array = mongoose.Types.Array;
42
- *
43
- * #### Types:
44
- *
45
- * - [Array](https://mongoosejs.com/docs/schematypes.html#arrays)
46
- * - [Buffer](https://mongoosejs.com/docs/schematypes.html#buffers)
47
- * - [Embedded](https://mongoosejs.com/docs/schematypes.html#schemas)
48
- * - [DocumentArray](https://mongoosejs.com/docs/api/documentarraypath.html)
49
- * - [Decimal128](https://mongoosejs.com/docs/api/decimal128.html#Decimal128())
50
- * - [ObjectId](https://mongoosejs.com/docs/schematypes.html#objectids)
51
- * - [Map](https://mongoosejs.com/docs/schematypes.html#maps)
52
- * - [Subdocument](https://mongoosejs.com/docs/schematypes.html#schemas)
53
- *
54
- * Using this exposed access to the `ObjectId` type, we can construct ids on demand.
55
- *
56
- * const ObjectId = mongoose.Types.ObjectId;
57
- * const id1 = new ObjectId;
58
- *
59
- * @property Types
60
- * @api public
61
- */
62
- exports.Types = require('./types');
63
-
64
- /**
65
- * The Mongoose [VirtualType](https://mongoosejs.com/docs/api/virtualtype.html#VirtualType()) constructor
66
- *
67
- * @method VirtualType
68
- * @api public
69
- */
70
- exports.VirtualType = require('./virtualType');
71
-
72
- /**
73
- * The various Mongoose SchemaTypes.
74
- *
75
- * #### Note:
76
- *
77
- * _Alias of mongoose.Schema.Types for backwards compatibility._
78
- *
79
- * @property SchemaTypes
80
- * @see Schema.SchemaTypes https://mongoosejs.com/docs/api/schema.html#Schema.Types
81
- * @api public
82
- */
83
-
84
- exports.SchemaType = require('./schemaType.js');
85
-
86
- /**
87
- * The constructor used for schematype options
88
- *
89
- * @method SchemaTypeOptions
90
- * @api public
91
- */
92
-
93
- exports.SchemaTypeOptions = require('./options/schemaTypeOptions');
94
-
95
- /**
96
- * Internal utils
97
- *
98
- * @property utils
99
- * @api private
100
- */
101
-
102
- exports.utils = require('./utils.js');
103
-
104
- /**
105
- * The Mongoose browser [Document](/api/document.html) constructor.
106
- *
107
- * @method Document
108
- * @api public
109
- */
110
- exports.Document = DocumentProvider();
111
-
112
- /**
113
- * Return a new browser model. In the browser, a model is just
114
- * a simplified document with a schema - it does **not** have
115
- * functions like `findOne()`, etc.
116
- *
117
- * @method model
118
- * @api public
119
- * @param {String} name
120
- * @param {Schema} schema
121
- * @return Class
122
- */
123
- exports.model = function(name, schema) {
124
- class Model extends exports.Document {
125
- constructor(obj, fields) {
126
- super(obj, schema, fields);
127
- }
128
- }
129
- Model.modelName = name;
130
-
131
- return Model;
132
- };
133
-
134
- /*!
135
- * Module exports.
136
- */
137
-
138
- if (typeof window !== 'undefined') {
139
- window.mongoose = module.exports;
140
- window.Buffer = Buffer;
141
- }
@@ -1,101 +0,0 @@
1
- /*!
2
- * Module dependencies.
3
- */
4
-
5
- 'use strict';
6
-
7
- const NodeJSDocument = require('./document');
8
- const EventEmitter = require('events').EventEmitter;
9
- const MongooseError = require('./error/index');
10
- const Schema = require('./schema');
11
- const ObjectId = require('./types/objectid');
12
- const ValidationError = MongooseError.ValidationError;
13
- const applyHooks = require('./helpers/model/applyHooks');
14
- const isObject = require('./helpers/isObject');
15
-
16
- /**
17
- * Document constructor.
18
- *
19
- * @param {Object} obj the values to set
20
- * @param {Object} schema
21
- * @param {Object} [fields] optional object containing the fields which were selected in the query returning this document and any populated paths data
22
- * @param {Boolean} [skipId] bool, should we auto create an ObjectId _id
23
- * @inherits NodeJS EventEmitter https://nodejs.org/api/events.html#class-eventemitter
24
- * @event `init`: Emitted on a document after it has was retrieved from the db and fully hydrated by Mongoose.
25
- * @event `save`: Emitted when the document is successfully saved
26
- * @api private
27
- */
28
-
29
- function Document(obj, schema, fields, skipId, skipInit) {
30
- if (!(this instanceof Document)) {
31
- return new Document(obj, schema, fields, skipId, skipInit);
32
- }
33
-
34
- if (isObject(schema) && !schema.instanceOfSchema) {
35
- schema = new Schema(schema);
36
- }
37
-
38
- // When creating EmbeddedDocument, it already has the schema and he doesn't need the _id
39
- schema = this.schema || schema;
40
-
41
- // Generate ObjectId if it is missing, but it requires a scheme
42
- if (!this.schema && schema.options._id) {
43
- obj = obj || {};
44
-
45
- if (obj._id === undefined) {
46
- obj._id = new ObjectId();
47
- }
48
- }
49
-
50
- if (!schema) {
51
- throw new MongooseError.MissingSchemaError();
52
- }
53
-
54
- this.$__setSchema(schema);
55
-
56
- NodeJSDocument.call(this, obj, fields, skipId, skipInit);
57
-
58
- applyHooks(this, schema, { decorateDoc: true });
59
-
60
- // apply methods
61
- for (const m in schema.methods) {
62
- this[m] = schema.methods[m];
63
- }
64
- // apply statics
65
- for (const s in schema.statics) {
66
- this[s] = schema.statics[s];
67
- }
68
- }
69
-
70
- /*!
71
- * Inherit from the NodeJS document
72
- */
73
-
74
- Document.prototype = Object.create(NodeJSDocument.prototype);
75
- Document.prototype.constructor = Document;
76
-
77
- /*!
78
- * ignore
79
- */
80
-
81
- Document.events = new EventEmitter();
82
-
83
- /*!
84
- * Browser doc exposes the event emitter API
85
- */
86
-
87
- Document.$emitter = new EventEmitter();
88
-
89
- ['on', 'once', 'emit', 'listeners', 'removeListener', 'setMaxListeners',
90
- 'removeAllListeners', 'addListener'].forEach(function(emitterFn) {
91
- Document[emitterFn] = function() {
92
- return Document.$emitter[emitterFn].apply(Document.$emitter, arguments);
93
- };
94
- });
95
-
96
- /*!
97
- * Module exports.
98
- */
99
-
100
- Document.ValidationError = ValidationError;
101
- module.exports = exports = Document;
@@ -1,30 +0,0 @@
1
- 'use strict';
2
-
3
- /* eslint-env browser */
4
-
5
- /*!
6
- * Module dependencies.
7
- */
8
- const Document = require('./document.js');
9
- const BrowserDocument = require('./browserDocument.js');
10
-
11
- let isBrowser = false;
12
-
13
- /**
14
- * Returns the Document constructor for the current context
15
- *
16
- * @api private
17
- */
18
- module.exports = function documentProvider() {
19
- if (isBrowser) {
20
- return BrowserDocument;
21
- }
22
- return Document;
23
- };
24
-
25
- /*!
26
- * ignore
27
- */
28
- module.exports.setBrowser = function(flag) {
29
- isBrowser = flag;
30
- };
@@ -1,14 +0,0 @@
1
-
2
- /*!
3
- * Module dependencies.
4
- */
5
-
6
- 'use strict';
7
-
8
- const Binary = require('bson').Binary;
9
-
10
- /*!
11
- * Module exports.
12
- */
13
-
14
- module.exports = exports = Binary;
@@ -1,7 +0,0 @@
1
- /*!
2
- * ignore
3
- */
4
-
5
- 'use strict';
6
-
7
- module.exports = require('bson').Decimal128;
@@ -1,13 +0,0 @@
1
- /*!
2
- * Module exports.
3
- */
4
-
5
- 'use strict';
6
-
7
- exports.Collection = function() {
8
- throw new Error('Cannot create a collection from browser library');
9
- };
10
- exports.Connection = function() {
11
- throw new Error('Cannot create a connection from browser library');
12
- };
13
- exports.BulkWriteResult = function() {};
@@ -1,29 +0,0 @@
1
-
2
- /*!
3
- * [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) ObjectId
4
- * @constructor NodeMongoDbObjectId
5
- * @see ObjectId
6
- */
7
-
8
- 'use strict';
9
-
10
- const ObjectId = require('bson').ObjectID;
11
-
12
- /**
13
- * Getter for convenience with populate, see gh-6115
14
- * @api private
15
- */
16
-
17
- Object.defineProperty(ObjectId.prototype, '_id', {
18
- enumerable: false,
19
- configurable: true,
20
- get: function() {
21
- return this;
22
- }
23
- });
24
-
25
- /*!
26
- * ignore
27
- */
28
-
29
- module.exports = exports = ObjectId;
@@ -1,54 +0,0 @@
1
- 'use strict';
2
-
3
- const immediate = require('./immediate');
4
-
5
- const emittedSymbol = Symbol('mongoose#emitted');
6
-
7
- module.exports = function promiseOrCallback(callback, fn, ee, Promise) {
8
- if (typeof callback === 'function') {
9
- try {
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);
15
- }
16
- try {
17
- callback(error);
18
- } catch (error) {
19
- return immediate(() => {
20
- throw error;
21
- });
22
- }
23
- return;
24
- }
25
- callback.apply(this, arguments);
26
- });
27
- } catch (error) {
28
- if (ee != null && ee.listeners != null && ee.listeners('error').length > 0 && !error[emittedSymbol]) {
29
- error[emittedSymbol] = true;
30
- ee.emit('error', error);
31
- }
32
-
33
- return callback(error);
34
- }
35
- }
36
-
37
- Promise = Promise || global.Promise;
38
-
39
- return new Promise((resolve, reject) => {
40
- fn(function(error, res) {
41
- if (error != null) {
42
- if (ee != null && ee.listeners != null && ee.listeners('error').length > 0 && !error[emittedSymbol]) {
43
- error[emittedSymbol] = true;
44
- ee.emit('error', error);
45
- }
46
- return reject(error);
47
- }
48
- if (arguments.length > 2) {
49
- return resolve(Array.prototype.slice.call(arguments, 1));
50
- }
51
- resolve(res);
52
- });
53
- });
54
- };