Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Automattic/mongoose
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Mar 21, 2022
2 parents 041be89 + 57834e2 commit 17a5d5a
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 33 deletions.
1 change: 0 additions & 1 deletion benchmarks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ function run(label, fn) {
res.heapUsed = used.heapUsed - started.heapUsed;
log('change: ', res);
a = res = used = time = started = start = total = i = null;
// console.error(((used.vsize - started.vsize) / 1048576)+' MB');
}

run('string', function() {
Expand Down
21 changes: 1 addition & 20 deletions benchmarks/validate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// require('nodetime').profile();

'use strict';

const mongoose = require('../../mongoose');
Expand All @@ -25,8 +23,7 @@ const breakfastSchema = new Schema({
}
});
const Breakfast = mongoose.model('Breakfast', breakfastSchema);
// const time1 = (new Date - start1);
// console.error('reading from disk and parsing JSON took %d ms', time1);

const badBreakfast = new Breakfast({
eggs: 2,
bacon: 0,
Expand All @@ -38,23 +35,7 @@ const goodBreakfast = new Breakfast({
bacon: 1,
drink: 'Tea'
})
// const start = new Date;
// const total = 10000000;
// let i = total;
// let len;

// for (i = 0, len = total; i < len; ++i) {

// const goodBreakfast = new Breakfast({
// eggs: 6,
// bacon: 1,
// drink: 'Tea'
// })
// goodBreakfast.validateSync();
// }

// const time = (new Date - start) / 1000;
// console.error('took %d seconds for %d docs (%d dps)', time, total, total / time);
const suite = new Benchmark.Suite()
suite
.add('invalid async', {
Expand Down
2 changes: 1 addition & 1 deletion examples/statics/statics.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function run() {
const result = await Person.findPersonByName('bill');

console.log(result);
cleanup();
await cleanup();
}

async function cleanup() {
Expand Down
1 change: 1 addition & 0 deletions lib/browserDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const isObject = require('./helpers/isObject');
* Document constructor.
*
* @param {Object} obj the values to set
* @param {Object} schema
* @param {Object} [fields] optional object containing the fields which were selected in the query returning this document and any populated paths data
* @param {Boolean} [skipId] bool, should we auto create an ObjectId _id
* @inherits NodeJS EventEmitter https://nodejs.org/api/events.html#events_class_events_eventemitter
Expand Down
1 change: 0 additions & 1 deletion lib/cursor/AggregationCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const util = require('util');
* Use [`Aggregate#cursor()`](/docs/api.html#aggregate_Aggregate-cursor) instead.
*
* @param {Aggregate} agg
* @param {Object} options
* @inherits Readable
* @event `cursor`: Emitted when the cursor is created
* @event `error`: Emitted when an error occurred
Expand Down
1 change: 0 additions & 1 deletion lib/helpers/query/castFilterPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ module.exports = function castFilterPath(query, schematype, val) {
}
continue;
}
// cast(schematype.caster ? schematype.caster.schema : schema, nested, options, context);
} else {
val[$cond] = schematype.castForQueryWrapper({
$conditional: $cond,
Expand Down
2 changes: 1 addition & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1885,7 +1885,7 @@ Model.discriminators;
* ####Note:
* Only translate arguments of object type anything else is returned raw
*
* @param {Object} raw fields/conditions that may contain aliased keys
* @param {Object} fields fields/conditions that may contain aliased keys
* @return {Object} the translated 'pure' fields/conditions
*/
Model.translateAliases = function translateAliases(fields) {
Expand Down
2 changes: 1 addition & 1 deletion lib/schema/SubdocumentPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = SubdocumentPath;
* Single nested subdocument SchemaType constructor.
*
* @param {Schema} schema
* @param {String} key
* @param {String} path
* @param {Object} options
* @inherits SchemaType
* @api public
Expand Down
1 change: 1 addition & 0 deletions lib/schema/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const emptyOpts = Object.freeze({});
* @param {String} key
* @param {SchemaType} cast
* @param {Object} options
* @param {Object} schemaOptions
* @inherits SchemaType
* @api public
*/
Expand Down
1 change: 1 addition & 0 deletions lib/schema/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ let Subdocument;
* @param {String} key
* @param {Schema} schema
* @param {Object} options
* @param {Object} schemaOptions
* @inherits SchemaArray
* @api public
*/
Expand Down
2 changes: 2 additions & 0 deletions lib/types/array/methods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ const methods = {
* Return whether or not the `obj` is included in the array.
*
* @param {Object} obj the item to check
* @param {Number} fromIndex
* @return {Boolean}
* @api public
* @method includes
Expand All @@ -460,6 +461,7 @@ const methods = {
* Return the index of `obj` or `-1` if not found.
*
* @param {Object} obj the item to look for
* @param {Number} fromIndex
* @return {Number}
* @api public
* @method indexOf
Expand Down
1 change: 1 addition & 0 deletions lib/virtualtype.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const utils = require('./utils');
* @param {Number} [options.skip=null] add a default `skip` to the `populate()` query
* @param {Number} [options.perDocumentLimit=null] For legacy reasons, `limit` with `populate()` may give incorrect results because it only executes a single query for every document being populated. If you set `perDocumentLimit`, Mongoose will ensure correct `limit` per document by executing a separate query for each document to `populate()`. For example, `.find().populate({ path: 'test', perDocumentLimit: 2 })` will execute 2 additional queries if `.find()` returns 2 documents.
* @param {Object} [options.options=null] Additional options like `limit` and `lean`.
* @param {string} name
* @api public
*/

Expand Down
6 changes: 3 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ describe('mongoose module:', function() {

const movie = await Movie.create({ name: 'The Empire Strikes Back' });
await Person.create({ name: 'Test1', favoriteMovie: movie._id });
assert.rejects(async() => {
await assert.rejects(async() => {
await Person.findOne().populate({ path: 'favoriteGame' });
}, { message: 'Cannot populate path `favoriteGame` because it is not in your schema. Set the `strictPopulate` option to false to override.' });
});
Expand All @@ -788,7 +788,7 @@ describe('mongoose module:', function() {
}));
const movie = await Movie.create({ name: 'The Empire Strikes Back' });
await Person.create({ name: 'Test1', favoriteMovie: movie._id });
assert.rejects(async() => {
await assert.rejects(async() => {
await Person.findOne().populate({ path: 'favoriteGame', strictPopulate: true });
}, { message: 'Cannot populate path `favoriteGame` because it is not in your schema. Set the `strictPopulate` option to false to override.' });
});
Expand Down Expand Up @@ -912,4 +912,4 @@ describe('mongoose module:', function() {
assert.deepEqual(await m.syncIndexes(), {});
});
});
});
});
24 changes: 20 additions & 4 deletions test/types/populate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,37 @@ function gh11503() {
interface User {
friends: Types.ObjectId[];
}
const UserSchema = new Schema<User>({
const userSchema = new Schema<User>({
friends: [{ type: Schema.Types.ObjectId, ref: 'friends' }]
});
const Users = model<User>('friends', UserSchema);
const User = model<User>('friends', userSchema);

Users.findOne({}).populate('friends').then(user => {
User.findOne({}).populate('friends').then(user => {
expectType<Types.ObjectId | undefined>(user?.friends[0]);
expectError(user?.friends[0].blocked);
expectError(user?.friends.map(friend => friend.blocked));
});

Users.findOne({}).populate<{friends: Friend[]}>('friends').then(user => {
User.findOne({}).populate<{friends: Friend[]}>('friends').then(user => {
expectAssignable<Friend>(user?.friends[0]);
expectType<boolean>(user?.friends[0].blocked);
const firstFriendBlockedValue = user?.friends.map(friend => friend)[0];
expectType<boolean>(firstFriendBlockedValue?.blocked);
});
}


function gh11544() {

interface User {
friends: Types.ObjectId[];
}
const userSchema = new Schema<User>({
friends: [{ type: Schema.Types.ObjectId, ref: 'friends' }]
});
const User = model<User>('friends', userSchema);

User.findOne({}).populate({ path: 'friends', strictPopulate: false });
User.findOne({}).populate({ path: 'friends', strictPopulate: true });
User.findOne({}).populate({ path: 'friends', populate: { path: 'someNestedPath', strictPopulate: false } });
}
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,8 @@ declare module 'mongoose' {
model?: string | Model<any>;
/** optional query options like sort, limit, etc */
options?: any;
/** optional boolean, set to `false` to allow populating paths that aren't in the schema */
strictPopulate?: boolean;
/** deep populate */
populate?: string | PopulateOptions | (string | PopulateOptions)[];
/**
Expand Down

0 comments on commit 17a5d5a

Please sign in to comment.