Skip to content

Commit

Permalink
Merge branch 'master' into gh9885
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 authored Feb 8, 2021
2 parents f79e9a7 + fe8b97b commit 6f1af75
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 20 deletions.
13 changes: 13 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
5.11.15 / 2021-02-03
====================
* fix(document): fix issues with `isSelected` as an path in a nested schema #9884 #9873 [IslandRhythms](https://github.com/IslandRhythms)
* fix(index.d.ts): better support for `SchemaDefinition` generics when creating schema #9863 #9862 #9789
* fix(index.d.ts): allow required function in array definition #9888 [Ugzuzg](https://github.com/Ugzuzg)
* fix(index.d.ts): reorder create typings to allow array desctructuring #9890 [Ugzuzg](https://github.com/Ugzuzg)
* fix(index.d.ts): add missing overload to Model.validate #9878 #9877 [jonamat](https://github.com/jonamat)
* fix(index.d.ts): throw compiler error if schema says path is a String, but interface says path is a number #9857
* fix(index.d.ts): make `Query` a class, allow calling `Query#where()` with object argument and with no arguments #9856
* fix(index.d.ts): support calling `Schema#pre()` and `Schema#post()` with options and array of hooked function names #9844
* docs(faq): mention other debug options than console #9887 [dandv](https://github.com/dandv)
* docs(connections): clarify that Mongoose can emit 'error' both during initial connection and after initial connection #9853

5.11.14 / 2021-01-28
====================
* fix(populate): avoid inferring `justOne` from parent when populating a POJO with a manually populated path #9833 [IslandRhythms](https://github.com/IslandRhythms)
Expand Down
6 changes: 3 additions & 3 deletions docs/faq.pug
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,10 @@ block content

<a href="#enable_debugging">**Q**</a>. How can I enable debugging?

**A**. Set the `debug` option to `true`:
**A**. Set the `debug` option:

```javascript
// all executed methods log output to console
mongoose.set('debug', true)

// disable colors in debug mode
Expand All @@ -305,8 +306,7 @@ block content
mongoose.set('debug', { shell: true })
```

All executed collection methods will log output of their arguments to your
console.
For more debugging options (streams, callbacks), see the ['debug' option under `.set()`](./api.html#mongoose_Mongoose-set).


<hr id="callback_never_executes" />
Expand Down
6 changes: 3 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,11 +630,11 @@ declare module 'mongoose' {
countDocuments(filter: FilterQuery<T>, callback?: (err: any, count: number) => void): Query<number, T>;

/** Creates a new document or documents */
create<DocContents = T | DocumentDefinition<T>>(doc: DocContents): Promise<T>;
create<DocContents = T | DocumentDefinition<T>>(docs: DocContents[], options?: SaveOptions): Promise<T[]>;
create<DocContents = T | DocumentDefinition<T>>(doc: DocContents): Promise<T>;
create<DocContents = T | DocumentDefinition<T>>(...docs: DocContents[]): Promise<T[]>;
create<DocContents = T | DocumentDefinition<T>>(doc: DocContents, callback: (err: CallbackError, doc: T) => void): void;
create<DocContents = T | DocumentDefinition<T>>(docs: DocContents[], callback: (err: CallbackError, docs: T[]) => void): void;
create<DocContents = T | DocumentDefinition<T>>(doc: DocContents, callback: (err: CallbackError, doc: T) => void): void;

/**
* Create the collection for this model. By default, if no indexes are specified,
Expand Down Expand Up @@ -1382,7 +1382,7 @@ declare module 'mongoose' {
* path cannot be set to a nullish value. If a function, Mongoose calls the
* function and only checks for nullish values if the function returns a truthy value.
*/
required?: boolean | (() => boolean) | [boolean, string];
required?: boolean | (() => boolean) | [boolean, string] | [() => boolean, string];

/**
* The default value for this path. If a function, Mongoose executes the function
Expand Down
8 changes: 6 additions & 2 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ function Document(obj, fields, skipId, options) {
});
}
}

if (obj) {
// Skip set hooks
if (this.$__original_set) {
Expand Down Expand Up @@ -899,6 +898,7 @@ Document.prototype.overwrite = function overwrite(obj) {
*/

Document.prototype.$set = function $set(path, val, type, options) {

if (utils.isPOJO(type)) {
options = type;
type = undefined;
Expand Down Expand Up @@ -937,9 +937,13 @@ Document.prototype.$set = function $set(path, val, type, options) {
path = path._doc;
}
}
if (path == null) {
const _ = path;
path = val;
val = _;
}

prefix = val ? val + '.' : '';

keys = Object.keys(path);
const len = keys.length;

Expand Down
6 changes: 4 additions & 2 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -1397,9 +1397,11 @@ Query.prototype.setOptions = function(options, overwrite) {
Query.prototype.explain = function(verbose) {
if (arguments.length === 0) {
this.options.explain = true;
return this;
} else if (verbose === false) {
delete this.options.explain;
} else {
this.options.explain = verbose;
}
this.options.explain = verbose;
return this;
};

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mongoose",
"description": "Mongoose MongoDB ODM",
"version": "5.11.14",
"version": "5.11.15",
"author": "Guillermo Rauch <[email protected]>",
"keywords": [
"mongodb",
Expand All @@ -22,7 +22,7 @@
"@types/mongodb": "^3.5.27",
"bson": "^1.1.4",
"kareem": "2.3.2",
"mongodb": "3.6.3",
"mongodb": "3.6.4",
"mongoose-legacy-pluralize": "1.0.2",
"mpath": "0.8.3",
"mquery": "3.2.3",
Expand Down
43 changes: 43 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9931,6 +9931,7 @@ describe('document', function() {
assert.ok(doc);
});
});

it('Makes sure pre remove hook is executed gh-9885', function() {
const SubSchema = new Schema({
myValue: {
Expand Down Expand Up @@ -9968,5 +9969,47 @@ describe('document', function() {
});
assert.equal(count, 1);
});
});

it('gh9880', function(done) {
const testSchema = new Schema({
prop: String,
nestedProp: {
prop: String
}
});
const Test = db.model('Test', testSchema);

new Test({
prop: 'Test',
nestedProp: null
}).save((err, doc) => {
doc.id;
doc.nestedProp;

// let's clone this document:
new Test({
prop: 'Test 2',
nestedProp: doc.nestedProp
});

Test.updateOne({
_id: doc._id
}, {
nestedProp: null
}, (err) => {
assert.ifError(err);
Test.findOne({
_id: doc._id
}, (err, updatedDoc) => {
assert.ifError(err);
new Test({
prop: 'Test 3',
nestedProp: updatedDoc.nestedProp
});
done();
});
});
});
});
});
5 changes: 3 additions & 2 deletions test/errors.validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,16 +243,17 @@ describe('ValidationError', function() {
});

describe('when user code defines a r/o Error#toJSON', function() {
it('shoud not fail', function() {
it('shoud not fail', function(done) {
const err = [];
const child = require('child_process')
.fork('./test/isolated/project-has-error.toJSON.js', { silent: true });
.fork('./test/isolated/project-has-error.toJSON.js', ['--no-warnings'], { silent: true });

child.stderr.on('data', function(buf) { err.push(buf); });
child.on('exit', function(code) {
const stderr = err.join('');
assert.equal(stderr, '');
assert.equal(code, 0);
done();
});
});
});
Expand Down
4 changes: 0 additions & 4 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9870,7 +9870,6 @@ describe('model: populate:', function() {
});
});
it('gh-9833', function() {
const util = require('util');
const Books = db.model('books', new Schema({ name: String, tags: [{ type: Schema.Types.ObjectId, ref: 'tags' }] }));
const Tags = db.model('tags', new Schema({ author: Schema.Types.ObjectId }));
const Authors = db.model('authors', new Schema({ name: String }));
Expand Down Expand Up @@ -9898,16 +9897,13 @@ describe('model: populate:', function() {
];
const books = yield Books.aggregate(aggregateOptions).exec();

console.log('books = ' + util.inspect(books, false, null, true));

const populateOptions = [{
path: 'tags.author',
model: 'authors',
select: '_id name'
}];

const populatedBooks = yield Books.populate(books, populateOptions);
console.log('populatedBooks = ' + util.inspect(populatedBooks, false, null, true));
assert.ok(!Array.isArray(populatedBooks[0].tags[0].author));
});
});
Expand Down
9 changes: 8 additions & 1 deletion test/typescript/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ Test.create({ name: 'test' }, { name: 'test2' }).then((docs: ITest[]) => console
Test.insertMany({ name: 'test' }).then((doc: ITest) => console.log(doc.name));
Test.insertMany([{ name: 'test' }], { session: null }).then((docs: ITest[]) => console.log(docs[0].name));

Test.create([{ name: 'test' }], { validateBeforeSave: true }).then((docs: ITest[]) => console.log(docs[0].name));
Test.create([{ name: 'test' }], { validateBeforeSave: true }).then((docs: ITest[]) => console.log(docs[0].name));

(async() => {
const [t1] = await Test.create([{ name: 'test' }]);
const [t2, t3, t4] = await Test.create({ name: 'test' }, { name: 'test' }, { name: 'test' });
(await Test.create([{ name: 'test' }]))[0];
(await Test.create({ name: 'test' }))._id;
})();
11 changes: 10 additions & 1 deletion test/typescript/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ const movieSchema: Schema = new Schema({
type: String,
enum: Genre,
required: true
},
actionIntensity: {
type: Number,
required: [
function(this: { genre: Genre }) {
return this.genre === Genre.Action;
},
'Action intensity required for action genre'
]
}
});

Expand Down Expand Up @@ -68,4 +77,4 @@ async function gh9857() {
};

const schema = new Schema<UserDocument, UserModel, User>(schemaDefinition);
}
}

0 comments on commit 6f1af75

Please sign in to comment.