Skip to content

Commit eced2c7

Browse files
committed
Merge branch 'master' into netlify-functions-example
2 parents 2e6b064 + 92cb6fb commit eced2c7

13 files changed

+115
-30
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
6.4.5 / 2022-07-18
2+
==================
3+
* fix(model+timestamps): set timestamps on subdocuments in insertMany() #12060
4+
* fix: correct isAtlas check #12110 [skrtheboss](https://github.com/skrtheboss)
5+
* fix(types): fix various issues with auto typed schemas #12042 [mohammad0-0ahmad](https://github.com/mohammad0-0ahmad)
6+
* fix(types): allow any value for AddFields #12096
7+
* fix(types): allow arbitrary expressions for ConcatArrays #12058
8+
* fix(types): make $addToSet fields mutable to allow programatically constructing $addToSet #12091
9+
* fix(types): add $let as a possible expression to $addFields #12087 [AbdelrahmanHafez](https://github.com/AbdelrahmanHafez)
10+
* fix(types): fix $switch expression type #12088 [AbdelrahmanHafez](https://github.com/AbdelrahmanHafez)
11+
* fix(types): correct options type for syncIndexes() #12101 [lpizzinidev](https://github.com/lpizzinidev)
12+
* fix(types): avoid treating | undefined types as any in `Require_id` to better support `_id: String` with auto-typed schemas #12070
13+
* docs: fix up various jsdoc issues #12086 [hasezoey](https://github.com/hasezoey)
14+
* docs: add sanitizeFilter to mongoose.set() options #12112 [pathei-kosmos](https://github.com/pathei-kosmos)
15+
116
6.4.4 / 2022-07-08
217
==================
318
* fix(types): allow using an object to configure timestamps #12061 [lantw44](https://github.com/lantw44)

docs/migrating_to_6.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ mongoose.isValidObjectId(new User({ name: 'test' })); // true
194194
// character hex strings.
195195
mongoose.isObjectIdOrHexString(new mongoose.Types.ObjectId()); // true
196196
mongoose.isObjectIdOrHexString('62261a65d66c6be0a63c051f'); // true
197-
mongoose.isValidObjectId('0123456789ab'); // false
198-
mongoose.isValidObjectId(6); // false
197+
mongoose.isObjectIdOrHexString('0123456789ab'); // false
198+
mongoose.isObjectIdOrHexString(6); // false
199199
```
200200

201201
<h3 id="schema-defined-document-key-order"><a href="#schema-defined-document-key-order">Schema Defined Document Key Order</a></h3>

lib/document.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,8 +1149,8 @@ Document.prototype.$set = function $set(path, val, type, options) {
11491149
}
11501150

11511151
if (utils.isNonBuiltinObject(valForKey) && pathtype === 'nested') {
1152-
$applyDefaultsToNested(path[key], prefix + key, this);
11531152
this.$set(prefix + key, path[key], constructing, Object.assign({}, options, { _skipMarkModified: true }));
1153+
$applyDefaultsToNested(this.$get(prefix + key), prefix + key, this);
11541154
continue;
11551155
} else if (strict) {
11561156
// Don't overwrite defaults with undefined keys (gh-3981) (gh-9039)

lib/query.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4019,7 +4019,9 @@ Query.prototype._findAndModify = function(type, callback) {
40194019
*/
40204020

40214021
function _completeOneLean(schema, doc, path, res, opts, callback) {
4022-
if (opts.lean && opts.lean.transform) {
4022+
if (opts.lean && typeof opts.lean.transform === 'function') {
4023+
opts.lean.transform(doc);
4024+
40234025
for (let i = 0; i < schema.childSchemas.length; i++) {
40244026
const childPath = path ? path + '.' + schema.childSchemas[i].model.path : schema.childSchemas[i].model.path;
40254027
const _schema = schema.childSchemas[i].schema;
@@ -4053,7 +4055,11 @@ function _completeOneLean(schema, doc, path, res, opts, callback) {
40534055
*/
40544056

40554057
function _completeManyLean(schema, docs, path, opts, callback) {
4056-
if (opts.lean && opts.lean.transform) {
4058+
if (opts.lean && typeof opts.lean.transform === 'function') {
4059+
for (const doc of docs) {
4060+
opts.lean.transform(doc);
4061+
}
4062+
40574063
for (let i = 0; i < schema.childSchemas.length; i++) {
40584064
const childPath = path ? path + '.' + schema.childSchemas[i].model.path : schema.childSchemas[i].model.path;
40594065
const _schema = schema.childSchemas[i].schema;

lib/schema.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,10 @@ Schema.prototype.add = function add(obj, prefix) {
554554
const keys = Object.keys(obj);
555555
const typeKey = this.options.typeKey;
556556
for (const key of keys) {
557+
if (utils.specialProperties.has(key)) {
558+
continue;
559+
}
560+
557561
const fullPath = prefix + key;
558562
const val = obj[key];
559563

@@ -854,6 +858,9 @@ Schema.prototype.path = function(path, obj) {
854858
let fullPath = '';
855859

856860
for (const sub of subpaths) {
861+
if (utils.specialProperties.has(sub)) {
862+
throw new Error('Cannot set special property `' + sub + '` on a schema');
863+
}
857864
fullPath = fullPath += (fullPath.length > 0 ? '.' : '') + sub;
858865
if (!branch[sub]) {
859866
this.nested[fullPath] = true;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "mongoose",
33
"description": "Mongoose MongoDB ODM",
4-
"version": "6.4.4",
4+
"version": "6.4.5",
55
"author": "Guillermo Rauch <guillermo@learnboost.com>",
66
"keywords": [
77
"mongodb",
@@ -21,7 +21,7 @@
2121
"dependencies": {
2222
"bson": "^4.6.2",
2323
"kareem": "2.4.1",
24-
"mongodb": "4.8.0",
24+
"mongodb": "4.7.0",
2525
"mpath": "0.9.0",
2626
"mquery": "4.0.3",
2727
"ms": "2.1.3",

test/document.test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8831,7 +8831,7 @@ describe('document', function() {
88318831
assert.ok(!user.updatedAt);
88328832
});
88338833

8834-
it('Sets default when passing undefined as value for a key in a nested subdoc (gh-9039)', async function() {
8834+
it('Sets default when passing undefined as value for a key in a nested subdoc (gh-12102) (gh-9039)', async function() {
88358835
const Test = db.model('Test', {
88368836
nested: {
88378837
prop: {
@@ -8841,9 +8841,11 @@ describe('document', function() {
88418841
}
88428842
});
88438843

8844-
8845-
const doc = await Test.create({ nested: { prop: undefined } });
8844+
const obj = { nested: { prop: undefined } };
8845+
const doc = await Test.create(obj);
88468846
assert.equal(doc.nested.prop, 'some default value');
8847+
8848+
assert.deepStrictEqual(obj, { nested: { prop: undefined } });
88478849
});
88488850

88498851
it('allows accessing $locals when initializing (gh-9098)', function() {

test/query.test.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4006,22 +4006,28 @@ describe('Query', function() {
40064006
});
40074007
const Test = db.model('gh10423', testSchema);
40084008
await Test.create({ name: 'foo', foo: [{ sub: 'Test' }, { sub: 'Testerson' }], otherName: { nickName: 'Bar' } });
4009-
const result = await Test.find().lean({ transform: (doc) => {
4010-
delete doc._id;
4011-
return doc;
4012-
} });
4013-
assert(result[0]._id);
4014-
assert.equal(result[0].otherName._id, undefined);
4015-
assert.equal(result[0].foo[0]._id, undefined);
4016-
assert.equal(result[0].foo[1]._id, undefined);
4017-
const single = await Test.findOne().lean({ transform: (doc) => {
4018-
delete doc._id;
4019-
return doc;
4020-
} });
4021-
assert(single._id);
4022-
assert.equal(single.otherName._id, undefined);
4023-
assert.equal(single.foo[0]._id, undefined);
4024-
assert.equal(single.foo[0]._id, undefined);
4009+
4010+
const result = await Test.find().lean({
4011+
transform: (doc) => {
4012+
delete doc._id;
4013+
return doc;
4014+
}
4015+
});
4016+
assert.strictEqual(result[0]._id, undefined);
4017+
assert.strictEqual(result[0].otherName._id, undefined);
4018+
assert.strictEqual(result[0].foo[0]._id, undefined);
4019+
assert.strictEqual(result[0].foo[1]._id, undefined);
4020+
4021+
const single = await Test.findOne().lean({
4022+
transform: (doc) => {
4023+
delete doc._id;
4024+
return doc;
4025+
}
4026+
});
4027+
assert.strictEqual(single._id, undefined);
4028+
assert.strictEqual(single.otherName._id, undefined);
4029+
assert.strictEqual(single.foo[0]._id, undefined);
4030+
assert.strictEqual(single.foo[0]._id, undefined);
40254031
});
40264032

40274033
it('skips applying default projections over slice projections (gh-11940)', async function() {

test/schema.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,19 @@ describe('schema', function() {
924924

925925
assert.equal(called, true);
926926
});
927+
928+
it('options param (gh-12077)', function() {
929+
const Tobi = new Schema();
930+
let called = false;
931+
932+
Tobi.plugin(function(schema, opts) {
933+
assert.equal(schema, Tobi);
934+
assert.deepStrictEqual(opts, { answer: 42 });
935+
called = true;
936+
}, { answer: 42 });
937+
938+
assert.equal(called, true);
939+
});
927940
});
928941

929942
describe('options', function() {
@@ -2792,4 +2805,14 @@ describe('schema', function() {
27922805
});
27932806
}, /Cannot use schema-level projections.*subdocument_mapping.not_selected/);
27942807
});
2808+
2809+
it('disallows setting special properties with `add()` or constructor (gh-12085)', async function() {
2810+
const maliciousPayload = '{"__proto__.toString": "Number"}';
2811+
2812+
assert.throws(() => {
2813+
mongoose.Schema(JSON.parse(maliciousPayload));
2814+
}, /__proto__/);
2815+
2816+
assert.ok({}.toString());
2817+
});
27952818
});

test/types/PipelineStage.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,11 @@ const stages4: PipelineStage[] = [
415415
}
416416
}
417417
];
418+
419+
(function gh12096() {
420+
const data: PipelineStage.AddFields = {
421+
$addFields: {
422+
name: { $meta: 'Bill' }
423+
}
424+
};
425+
})();

test/types/queries.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,5 +325,19 @@ function gh11964() {
325325
/* ... */
326326
}
327327
}
328+
}
328329

330+
function gh12091() {
331+
interface IUser{
332+
friendsNames: string[];
333+
}
334+
const userSchema = new Schema<IUser>({
335+
friendsNames: [String]
336+
});
337+
338+
const update: UpdateQuery<IUser> = { $addToSet: { friendsNames: 'John Doe' } };
339+
if (!update?.$addToSet) {
340+
return;
341+
}
342+
update.$addToSet.friendsNames = 'Jane Doe';
329343
}

types/expressions.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2449,7 +2449,7 @@ declare module 'mongoose' {
24492449
FunctionExpression |
24502450
ObjectIdExpression |
24512451
ConditionalExpressionOperator |
2452-
Expression.Let;
2452+
any;
24532453

24542454
export type ObjectIdExpression =
24552455
TypeExpressionOperatorReturningObjectId;

types/index.d.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,10 @@ declare module 'mongoose' {
437437

438438
export type SortOrder = -1 | 1 | 'asc' | 'ascending' | 'desc' | 'descending';
439439

440+
type Mutable<T> = {
441+
-readonly [K in keyof T]: T[K];
442+
};
443+
440444
type _UpdateQuery<TSchema> = {
441445
/** @see https://docs.mongodb.com/manual/reference/operator/update-field/ */
442446
$currentDate?: AnyKeys<TSchema> & AnyObject;
@@ -450,10 +454,10 @@ declare module 'mongoose' {
450454
$unset?: AnyKeys<TSchema> & AnyObject;
451455

452456
/** @see https://docs.mongodb.com/manual/reference/operator/update-array/ */
453-
$addToSet?: mongodb.SetFields<TSchema>;
457+
$addToSet?: Mutable<mongodb.SetFields<TSchema>>;
454458
$pop?: AnyKeys<TSchema> & AnyObject;
455-
$pull?: mongodb.PullOperator<TSchema>;
456-
$push?: mongodb.PushOperator<TSchema>;
459+
$pull?: Mutable<mongodb.PullOperator<TSchema>>;
460+
$push?: Mutable<mongodb.PushOperator<TSchema>>;
457461
$pullAll?: mongodb.PullAllOperator<TSchema>;
458462

459463
/** @see https://docs.mongodb.com/manual/reference/operator/update-bitwise/ */

0 commit comments

Comments
 (0)