Skip to content

Commit

Permalink
Merge branch 'master' into 6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jan 11, 2021
2 parents f797ddc + eca8374 commit 5c85b04
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 31 deletions.
13 changes: 12 additions & 1 deletion History.md
@@ -1,4 +1,15 @@
5.11.10 / 2020-01-04
5.11.11 / 2021-01-08
====================
* fix(model): support calling `create()` with `undefined` as first argument and no callback #9765
* fix(index.d.ts): ensure TypeScript knows that `this` refers to `DocType` in schema methods with strict mode #9755
* fix(index.d.ts): make SchemaDefinition accept a model generic #9761 [mroohian](https://github.com/mroohian)
* fix(index.d.ts): add `Aggregate#addFields()` #9774
* fix(index.d.ts): allow setting `min` and `max` to [number, string] and [Date, string] #9762
* fix(index.d.ts): improve context and type bindings for `Schema#methods` and `Schema#statics` #9717
* docs: add recommended connection option #9768 [Fernando-Lozano](https://github.com/Fernando-Lozano)
* chore: correct improper date in History.md #9783 [botv](https://github.com/botv)

5.11.10 / 2021-01-04
====================
* fix(model): support `populate` option for `insertMany()` as a workaround for mongoose-autopopulate #9720
* perf(schema): avoid creating extra array when initializing array of arrays #9588
Expand Down
35 changes: 35 additions & 0 deletions docs/images/mongoose.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 12 additions & 6 deletions index.d.ts
Expand Up @@ -1086,11 +1086,11 @@ declare module 'mongoose' {

/** Adds an instance method to documents constructed from Models compiled from this schema. */
// eslint-disable-next-line @typescript-eslint/ban-types
method(name: string, fn: Function, opts?: any): this;
method(obj: { [name: string]: Function }): this;
method(name: string, fn: (this: DocType, ...args: any[]) => any, opts?: any): this;
method(obj: { [name: string]: (this: DocType, ...args: any[]) => any }): this;

/** Object of currently defined methods on this schema. */
methods: { [name: string]: (this: DocType, ...args: any[]) => void };
methods: { [F in keyof DocType]: DocType[F] } & { [name: string]: (this: DocType, ...args: any[]) => any };

/** The original object passed to the schema constructor */
obj: any;
Expand Down Expand Up @@ -1144,12 +1144,12 @@ declare module 'mongoose' {

/** Adds static "class" methods to Models compiled from this schema. */
// eslint-disable-next-line @typescript-eslint/ban-types
static(name: string, fn: Function): this;
static(name: string, fn: (this: M, ...args: any[]) => any): this;
// eslint-disable-next-line @typescript-eslint/ban-types
static(obj: { [name: string]: Function }): this;
static(obj: { [name: string]: (this: M, ...args: any[]) => any }): this;

/** Object of currently defined statics on this schema. */
statics: { [name: string]: Function };
statics: { [F in keyof M]: M[F] } & { [name: string]: (this: M, ...args: any[]) => any };

/** Creates a virtual type with the given name. */
virtual(name: string, options?: any): VirtualType;
Expand Down Expand Up @@ -2240,6 +2240,12 @@ declare module 'mongoose' {
* future release. */
addCursorFlag(flag: string, value: boolean): this;

/**
* Appends a new $addFields operator to this aggregate pipeline.
* Requires MongoDB v3.4+ to work
*/
addFields(arg: any): this;

/** Sets the allowDiskUse option for the aggregation query (ignored for < 2.6.0) */
allowDiskUse(value: boolean): this;

Expand Down
3 changes: 3 additions & 0 deletions index.pug
Expand Up @@ -346,6 +346,9 @@ html(lang='en')
<a rel="sponsored" href="https://njsportsjournal.com/">
<img class="sponsor" src="https://images.opencollective.com/njsj1/9bf603f/logo/256.png" style="height: 100px">
</a>
<a rel="sponsored" href="https://www.nettikasinot.media/">
<img class="sponsor" src="https://images.opencollective.com/nettikasinot-media/2dba7da/logo/256.png" style="height: 100px">
</a>
</div>
</div>

Expand Down
4 changes: 4 additions & 0 deletions lib/document.js
Expand Up @@ -1229,6 +1229,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
let didPopulate = false;
if (refMatches && val instanceof Document) {
this.populated(path, val._id, { [populateModelSymbol]: val.constructor });
val.$__.wasPopulated = true;
didPopulate = true;
}

Expand All @@ -1246,6 +1247,9 @@ Document.prototype.$set = function $set(path, val, type, options) {
popOpts = { [populateModelSymbol]: val[0].constructor };
this.populated(path, val.map(function(v) { return v._id; }), popOpts);
}
for (const doc of val) {
doc.$__.wasPopulated = true;
}
didPopulate = true;
}

Expand Down
21 changes: 1 addition & 20 deletions lib/schematype.js
Expand Up @@ -1063,33 +1063,14 @@ SchemaType.prototype.getDefault = function(scope, init) {
* @api private
*/

SchemaType.prototype._applySetters = function(value, scope, init, priorVal) {
SchemaType.prototype._applySetters = function(value, scope) {
let v = value;
const setters = this.setters;
const caster = this.caster;

for (const setter of utils.clone(setters).reverse()) {
v = setter.call(scope, v, priorVal, this);
}

if (caster && !caster.$isMongooseArray && Array.isArray(v) && caster.setters) {
const newVal = [];

for (let i = 0; i < v.length; ++i) {
const value = v[i];
try {
newVal.push(caster.applySetters(value, scope, init, priorVal));
} catch (err) {
if (err instanceof MongooseError.CastError) {
err.$originalErrorPath = err.path;
err.path = err.path + '.' + i;
}
throw err;
}
}
v = newVal;
}

return v;
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "mongoose",
"description": "Mongoose MongoDB ODM",
"version": "5.11.10",
"version": "5.11.11",
"author": "Guillermo Rauch <guillermo@learnboost.com>",
"keywords": [
"mongodb",
Expand Down
1 change: 0 additions & 1 deletion test/document.test.js
Expand Up @@ -8954,7 +8954,6 @@ describe('document', function() {
const err = t.validateSync();
assert.ok(err);
assert.ok(err.errors);
assert.ok(err.errors['test']);
assert.ok(err.errors['test.1']);
});

Expand Down
2 changes: 1 addition & 1 deletion test/schema.test.js
Expand Up @@ -442,7 +442,7 @@ describe('schema', function() {
threw = true;
assert.equal(error.name, 'CastError');
assert.equal(error.message,
'Cast to [Number] failed for value "[["abcd"]]" at path "nums.0"');
'Cast to [[Number]] failed for value "[["abcd"]]" at path "nums.0"');
}
assert.ok(threw);

Expand Down
6 changes: 5 additions & 1 deletion test/typescript/queries.ts
@@ -1,11 +1,12 @@
import { Schema, model, Document, Types } from 'mongoose';

const schema: Schema = new Schema({ name: { type: 'String' } });
const schema: Schema = new Schema({ name: { type: 'String' }, tags: [String] });

interface ITest extends Document {
name?: string;
age?: number;
parent?: Types.ObjectId;
tags?: string[];
}

const Test = model<ITest>('Test', schema);
Expand Down Expand Up @@ -39,3 +40,6 @@ Test.findOneAndUpdate({ name: 'test' }, { $inc: { age: 2 } }).then((res: ITest |
Test.findOneAndUpdate({ name: 'test' }, { name: 'test3' }, { upsert: true }).then((res: ITest) => { res.name = 'test4'; });

Test.findOneAndReplace({ name: 'test' }, { _id: new Types.ObjectId(), name: 'test2' }).exec().then((res: ITest | null) => console.log(res));

Test.findOneAndUpdate({ name: 'test' }, { $addToSet: { tags: 'each' } });
Test.findOneAndUpdate({ name: 'test' }, { $push: { tags: 'each' } });

0 comments on commit 5c85b04

Please sign in to comment.