Skip to content

Commit 0c90cb3

Browse files
authored
Merge pull request #12406 from Automattic/6.6
6.6
2 parents fbf543c + 4d0392b commit 0c90cb3

26 files changed

+271
-30
lines changed

.github/dependabot.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ updates:
66
interval: "monthly"
77
open-pull-requests-limit: 100
88
target-branch: master
9-
ignore:
109
- package-ecosystem: github-actions
1110
directory: "/"
1211
schedule:

index.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/**
32
* Export lib/mongoose
43
*
@@ -11,3 +10,54 @@ const mongoose = require('./lib/');
1110
module.exports = mongoose;
1211
module.exports.default = mongoose;
1312
module.exports.mongoose = mongoose;
13+
14+
// Re-export for ESM support
15+
module.exports.cast = mongoose.cast;
16+
module.exports.STATES = mongoose.STATES;
17+
module.exports.setDriver = mongoose.setDriver;
18+
module.exports.set = mongoose.set;
19+
module.exports.get = mongoose.get;
20+
module.exports.createConnection = mongoose.createConnection;
21+
module.exports.connect = mongoose.connect;
22+
module.exports.disconnect = mongoose.disconnect;
23+
module.exports.startSession = mongoose.startSession;
24+
module.exports.pluralize = mongoose.pluralize;
25+
module.exports.model = mongoose.model;
26+
module.exports.deleteModel = mongoose.deleteModel;
27+
module.exports.modelNames = mongoose.modelNames;
28+
module.exports.plugin = mongoose.plugin;
29+
module.exports.connections = mongoose.connections;
30+
module.exports.version = mongoose.version;
31+
module.exports.Mongoose = mongoose.Mongoose;
32+
module.exports.Schema = mongoose.Schema;
33+
module.exports.SchemaType = mongoose.SchemaType;
34+
module.exports.SchemaTypes = mongoose.SchemaTypes;
35+
module.exports.VirtualType = mongoose.VirtualType;
36+
module.exports.Types = mongoose.Types;
37+
module.exports.Query = mongoose.Query;
38+
module.exports.Promise = mongoose.Promise;
39+
module.exports.Model = mongoose.Model;
40+
module.exports.Document = mongoose.Document;
41+
module.exports.ObjectId = mongoose.ObjectId;
42+
module.exports.isValidObjectId = mongoose.isValidObjectId;
43+
module.exports.isObjectIdOrHexString = mongoose.isObjectIdOrHexString;
44+
module.exports.syncIndexes = mongoose.syncIndexes;
45+
module.exports.Decimal128 = mongoose.Decimal128;
46+
module.exports.Mixed = mongoose.Mixed;
47+
module.exports.Date = mongoose.Date;
48+
module.exports.Number = mongoose.Number;
49+
module.exports.Error = mongoose.Error;
50+
module.exports.now = mongoose.now;
51+
module.exports.CastError = mongoose.CastError;
52+
module.exports.SchemaTypeOptions = mongoose.SchemaTypeOptions;
53+
module.exports.mongo = mongoose.mongo;
54+
module.exports.mquery = mongoose.mquery;
55+
module.exports.sanitizeFilter = mongoose.sanitizeFilter;
56+
module.exports.trusted = mongoose.trusted;
57+
module.exports.skipMiddlewareFunction = mongoose.skipMiddlewareFunction;
58+
module.exports.overwriteMiddlewareResult = mongoose.overwriteMiddlewareResult;
59+
60+
// The following properties are not exported using ESM because `setDriver()` can mutate these
61+
// module.exports.connection = mongoose.connection;
62+
// module.exports.Collection = mongoose.Collection;
63+
// module.exports.Connection = mongoose.Connection;

lib/aggregate.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,30 @@ Aggregate.prototype.project = function(arg) {
304304
* @api public
305305
*/
306306

307+
308+
/**
309+
* Appends a new $densify operator to this aggregate pipeline.
310+
*
311+
* #### Examples:
312+
*
313+
* aggregate.densify({
314+
* field: 'timestamp',
315+
* range: {
316+
* step: 1,
317+
* unit: 'hour',
318+
* bounds: [new Date('2021-05-18T00:00:00.000Z'), new Date('2021-05-18T08:00:00.000Z')]
319+
* }
320+
* });
321+
*
322+
* @see $densify https://www.mongodb.com/docs/manual/reference/operator/aggregation/densify/
323+
* @method densify
324+
* @memberOf Aggregate
325+
* @instance
326+
* @param {Object} arg $densify operator contents
327+
* @return {Aggregate}
328+
* @api public
329+
*/
330+
307331
/**
308332
* Appends a new $geoNear operator to this aggregate pipeline.
309333
*
@@ -342,7 +366,7 @@ Aggregate.prototype.near = function(arg) {
342366
* define methods
343367
*/
344368

345-
'group match skip limit out'.split(' ').forEach(function($operator) {
369+
'group match skip limit out densify'.split(' ').forEach(function($operator) {
346370
Aggregate.prototype[$operator] = function(arg) {
347371
const op = {};
348372
op['$' + $operator] = arg;

lib/connection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function Connection(base) {
8181
* Inherit from EventEmitter
8282
*/
8383

84-
Connection.prototype.__proto__ = EventEmitter.prototype;
84+
Object.setPrototypeOf(Connection.prototype, EventEmitter.prototype);
8585

8686
/**
8787
* Connection ready state

lib/drivers/node-mongodb-native/collection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function NativeCollection(name, conn, options) {
3434
* Inherit from abstract Collection.
3535
*/
3636

37-
NativeCollection.prototype.__proto__ = MongooseCollection.prototype;
37+
Object.setPrototypeOf(NativeCollection.prototype, MongooseCollection.prototype);
3838

3939
/**
4040
* Called when the connection opens.

lib/drivers/node-mongodb-native/connection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ NativeConnection.STATES = STATES;
3232
* Inherits from Connection.
3333
*/
3434

35-
NativeConnection.prototype.__proto__ = MongooseConnection.prototype;
35+
Object.setPrototypeOf(NativeConnection.prototype, MongooseConnection.prototype);
3636

3737
/**
3838
* Switches to a different database using the same connection pool.

lib/helpers/cursor/eachAsync.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const promiseOrCallback = require('../promiseOrCallback');
1616
* @param {Function} next the thunk to call to get the next document
1717
* @param {Function} fn
1818
* @param {Object} options
19+
* @param {Number} [options.batchSize=null] if set, Mongoose will call `fn` with an array of at most `batchSize` documents, instead of a single document
20+
* @param {Number} [options.parallel=1] maximum number of `fn` calls that Mongoose will run in parallel
21+
* @param {AbortSignal} [options.signal] allow cancelling this eachAsync(). Once the abort signal is fired, `eachAsync()` will immediately fulfill the returned promise (or call the callback) and not fetch any more documents.
1922
* @param {Function} [callback] executed when all docs have been processed
2023
* @return {Promise}
2124
* @api public
@@ -25,11 +28,25 @@ const promiseOrCallback = require('../promiseOrCallback');
2528
module.exports = function eachAsync(next, fn, options, callback) {
2629
const parallel = options.parallel || 1;
2730
const batchSize = options.batchSize;
31+
const signal = options.signal;
2832
const continueOnError = options.continueOnError;
2933
const aggregatedErrors = [];
3034
const enqueue = asyncQueue();
3135

36+
let drained = false;
37+
3238
return promiseOrCallback(callback, cb => {
39+
if (signal != null) {
40+
if (signal.aborted) {
41+
return cb(null);
42+
}
43+
44+
signal.addEventListener('abort', () => {
45+
drained = true;
46+
return cb(null);
47+
}, { once: true });
48+
}
49+
3350
if (batchSize != null) {
3451
if (typeof batchSize !== 'number') {
3552
throw new TypeError('batchSize must be a number');
@@ -44,7 +61,6 @@ module.exports = function eachAsync(next, fn, options, callback) {
4461
});
4562

4663
function iterate(finalCallback) {
47-
let drained = false;
4864
let handleResultsInProgress = 0;
4965
let currentDocumentIndex = 0;
5066
let documentsBatch = [];

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ function Mongoose(options) {
115115
]
116116
});
117117
}
118+
118119
Mongoose.prototype.cast = cast;
119120
/**
120121
* Expose connection states for user-land

lib/model.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ function Model(doc, fields, skipId) {
127127
* @api private
128128
*/
129129

130-
Model.prototype.__proto__ = Document.prototype;
130+
Object.setPrototypeOf(Model.prototype, Document.prototype);
131131
Model.prototype.$isMongooseModelPrototype = true;
132132

133133
/**
@@ -1230,7 +1230,7 @@ Model.discriminator = function(name, schema, options) {
12301230
model = this.db.model(model || name, schema, this.$__collection.name);
12311231
this.discriminators[name] = model;
12321232
const d = this.discriminators[name];
1233-
d.prototype.__proto__ = this.prototype;
1233+
Object.setPrototypeOf(d.prototype, this.prototype);
12341234
Object.defineProperty(d, 'baseModelName', {
12351235
value: this.modelName,
12361236
configurable: true,
@@ -3771,12 +3771,15 @@ Model.applyDefaults = function applyDefaults(doc) {
37713771
* Test.castObject({ num: 'not a number' }); // Throws a ValidationError
37723772
*
37733773
* @param {Object} obj object or document to cast
3774+
* @param {Object} options options passed to castObject
3775+
* @param {Boolean} options.ignoreCastErrors If set to `true` will not throw a ValidationError and only return values that were successfully cast.
37743776
* @returns {Object} POJO casted to the model's schema
37753777
* @throws {ValidationError} if casting failed for at least one path
37763778
* @api public
37773779
*/
37783780

3779-
Model.castObject = function castObject(obj) {
3781+
Model.castObject = function castObject(obj, options) {
3782+
options = options || {};
37803783
const ret = {};
37813784

37823785
const schema = this.schema;
@@ -3822,8 +3825,10 @@ Model.castObject = function castObject(obj) {
38223825
try {
38233826
val = Model.castObject.call(schemaType.caster, val);
38243827
} catch (err) {
3825-
error = error || new ValidationError();
3826-
error.addError(path, err);
3828+
if (!options.ignoreCastErrors) {
3829+
error = error || new ValidationError();
3830+
error.addError(path, err);
3831+
}
38273832
continue;
38283833
}
38293834

@@ -3835,8 +3840,10 @@ Model.castObject = function castObject(obj) {
38353840
val = schemaType.cast(val);
38363841
cur[pieces[pieces.length - 1]] = val;
38373842
} catch (err) {
3838-
error = error || new ValidationError();
3839-
error.addError(path, err);
3843+
if (!options.ignoreCastErrors) {
3844+
error = error || new ValidationError();
3845+
error.addError(path, err);
3846+
}
38403847

38413848
continue;
38423849
}
@@ -5010,8 +5017,8 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
50105017
model.modelName = name;
50115018

50125019
if (!(model.prototype instanceof Model)) {
5013-
model.__proto__ = Model;
5014-
model.prototype.__proto__ = Model.prototype;
5020+
Object.setPrototypeOf(model, Model);
5021+
Object.setPrototypeOf(model.prototype, Model.prototype);
50155022
}
50165023
model.model = function model(name) {
50175024
return this.db.model(name);
@@ -5108,8 +5115,8 @@ Model.__subclass = function subclass(conn, schema, collection) {
51085115
_this.call(this, doc, fields, skipId);
51095116
};
51105117

5111-
Model.__proto__ = _this;
5112-
Model.prototype.__proto__ = _this.prototype;
5118+
Object.setPrototypeOf(Model, _this);
5119+
Object.setPrototypeOf(Model.prototype, _this.prototype);
51135120
Model.db = conn;
51145121
Model.prototype.db = conn;
51155122
Model.prototype[modelDbSymbol] = conn;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"dependencies": {
2222
"bson": "^4.6.5",
2323
"kareem": "2.4.1",
24-
"mongodb": "4.8.1",
24+
"mongodb": "4.9.1",
2525
"mpath": "0.9.0",
2626
"mquery": "4.0.3",
2727
"ms": "2.1.3",

0 commit comments

Comments
 (0)