Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6.7 #12587

Merged
merged 63 commits into from Oct 24, 2022
Merged

6.7 #12587

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
93c2f84
feat: WIP add UUIDv1
hasezoey Aug 12, 2022
4c50837
feat: rename "SchemaUUIDv1" to "SchemaUUID"
hasezoey Aug 13, 2022
7240f18
Merge branch 'master' into addUUID
hasezoey Aug 23, 2022
a0459b3
fix(uuid): change "schemaName" to be without prefix "Schema"
hasezoey Aug 23, 2022
db1076c
test(schema.uuid): use actual opened connection instead of default
hasezoey Aug 23, 2022
d7b71cf
test(schema.uuid): remove debug logging
hasezoey Aug 23, 2022
5e7ceba
test(schema.uuid): fix tests to pass
hasezoey Aug 23, 2022
b8432d0
style(uuid): update jsdoc to be up-to-date comments
hasezoey Aug 23, 2022
34c0591
style(uuid): remove unused parameters
hasezoey Aug 23, 2022
6ae2800
style(uuid): remove old (commented-out) buffer code
hasezoey Aug 23, 2022
c53e076
test(schema.uuid): remove another debug logging
hasezoey Aug 23, 2022
47a0d07
fix(uuid): remove "$options" and "$regex" operators
hasezoey Aug 24, 2022
c76d097
fix(uuid): change "_checkRequired" function to just check against "no…
hasezoey Aug 24, 2022
a187b01
test(schema.uuid): check that the type in the database is actually a …
hasezoey Aug 24, 2022
7a43d4a
fix(uuid): change "binaryToString" to handle input strings
hasezoey Aug 24, 2022
6038b72
style(uuid): add jsdoc to helper function
hasezoey Aug 24, 2022
fd17d9b
test(schema.uuid): add tests for "$in" and "$nin"
hasezoey Aug 24, 2022
3abfff7
test(schema.uuid): extend test to also test $all
hasezoey Aug 24, 2022
a8c1380
fix(uuid): add "bits*" operators to "$conditionalHandlers"
hasezoey Aug 24, 2022
78eb098
feat(types+schema): allow defining schema paths using mongoose.Types.…
vkarpov15 Aug 29, 2022
4494877
test(schema.uuid): add tests that still need to be done
hasezoey Sep 15, 2022
7fee4f2
Merge branch 'master' into addUUID
hasezoey Sep 15, 2022
b5692c2
Update package.json
AbdelrahmanHafez Sep 20, 2022
d9a525e
Merge pull request #12446 from Automattic/AbdelrahmanHafez-patch-1
AbdelrahmanHafez Sep 20, 2022
008134b
feat(index): add ability to set multiple options at once in ".set"
hasezoey Sep 22, 2022
37115d8
fix(types): add types for "mongoose.set" as object
hasezoey Sep 22, 2022
72bd56c
fix(index): re-add validating the key against options when getting fo…
hasezoey Sep 22, 2022
28b12e2
style(index): rename loop variables in ".set"
hasezoey Sep 22, 2022
0461326
feat(setOptionError): add error "SetOptionError" and "SetOptionInnerE…
hasezoey Sep 22, 2022
4ab2e44
fix(index): update all errors in ".set" to use "SetOptionError" and "…
hasezoey Sep 22, 2022
6596b2c
fix(error): move common function to "helpers/error/combinePathErrors"
hasezoey Sep 25, 2022
9951ac7
Merge branch 'master' into 6.7
vkarpov15 Sep 27, 2022
c91dd0c
Merge branch 'master' into 6.7
vkarpov15 Sep 30, 2022
8287161
feat(schema): add `alias()` method that makes it easier to define mul…
vkarpov15 Sep 30, 2022
421cd93
Merge branch 'master' into 6.7
vkarpov15 Sep 30, 2022
25652c2
test(types): add test case from #12450 to #12352
vkarpov15 Oct 2, 2022
2e19ae2
Merge branch 'master' into 6.7
vkarpov15 Oct 2, 2022
d290d9a
Merge branch '6.7' into vkarpov15/gh-12205
vkarpov15 Oct 2, 2022
24a768b
Merge pull request #12352 from Automattic/vkarpov15/gh-12205
vkarpov15 Oct 2, 2022
4d84a38
Merge pull request #12462 from hasezoey/setMultipleOptionsAtOnce
vkarpov15 Oct 2, 2022
9c17de0
Update lib/schema.js
vkarpov15 Oct 3, 2022
03ff83f
Update lib/schema.js
vkarpov15 Oct 3, 2022
ba81950
Merge branch '6.7' into vkarpov15/gh-12368
vkarpov15 Oct 3, 2022
964912f
Merge branch 'master' into 6.7
vkarpov15 Oct 3, 2022
bfa835f
Merge branch '6.7' into vkarpov15/gh-12368
vkarpov15 Oct 3, 2022
0e8e9c4
feat(schema): allow defining array of aliases in schema as well
vkarpov15 Oct 3, 2022
136bd1d
Merge branch 'master' into 6.7
vkarpov15 Oct 5, 2022
2da76c3
Merge branch '6.7' into vkarpov15/gh-12368
vkarpov15 Oct 5, 2022
fd2449c
refactor(schema): remove unnecessary conditional re: @hasezoey code r…
vkarpov15 Oct 5, 2022
b2261ca
Merge branch 'master' into 6.7
vkarpov15 Oct 5, 2022
b8f4cb4
Merge pull request #12491 from Automattic/vkarpov15/gh-12368
vkarpov15 Oct 5, 2022
2481079
feat(document): add `$timestamps()` method to set timestamps for `sav…
vkarpov15 Oct 8, 2022
3f2c3cb
feat(model): add `mergeHooks` option to `Model.discriminator()` to av…
vkarpov15 Oct 8, 2022
7b8a432
Update lib/document.js
vkarpov15 Oct 8, 2022
cb81723
Merge pull request #12540 from Automattic/vkarpov15/gh-12117
vkarpov15 Oct 8, 2022
2a9208b
feat(types): allow calling `Model.discriminator()` with options
vkarpov15 Oct 9, 2022
76eaadd
add $fill pipeline stage
Oct 11, 2022
c35f99e
Merge branch 'master' into 6.7
vkarpov15 Oct 17, 2022
bed6d81
Merge pull request #12545 from raphael-papazikas/feature/12536-fill-s…
vkarpov15 Oct 17, 2022
8c84754
Merge pull request #12542 from Automattic/vkarpov15/gh-12472
vkarpov15 Oct 17, 2022
dc42696
Merge branch 'master' into 6.7
vkarpov15 Oct 24, 2022
49cca93
feat: upgrade to mongodb driver 4.11.0
vkarpov15 Oct 24, 2022
a20f7bf
Merge pull request #12268 from hasezoey/addUUID
vkarpov15 Oct 24, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 28 additions & 8 deletions lib/aggregate.js
Expand Up @@ -111,11 +111,11 @@ Aggregate.prototype.model = function(model) {
this._model = model;
if (model.schema != null) {
if (this.options.readPreference == null &&
model.schema.options.read != null) {
model.schema.options.read != null) {
this.options.readPreference = model.schema.options.read;
}
if (this.options.collation == null &&
model.schema.options.collation != null) {
model.schema.options.collation != null) {
this.options.collation = model.schema.options.collation;
}
}
Expand Down Expand Up @@ -158,7 +158,7 @@ Aggregate.prototype.append = function() {
* Requires MongoDB v3.4+ to work
*
* #### Example:
*
*
* // adding new fields based on existing fields
* aggregate.addFields({
* newField: '$b.nested'
Expand Down Expand Up @@ -328,6 +328,28 @@ Aggregate.prototype.project = function(arg) {
* @api public
*/

/**
* Appends a new $fill operator to this aggregate pipeline.
*
* #### Example:
*
* aggregate.fill({
* output: {
* bootsSold: { value: 0 },
* sandalsSold: { value: 0 },
* sneakersSold: { value: 0 }
* }
* });
*
* @see $fill https://www.mongodb.com/docs/manual/reference/operator/aggregation/fill/
* @method fill
* @memberOf Aggregate
* @instance
* @param {Object} arg $fill operator contents
* @return {Aggregate}
* @api public
*/

/**
* Appends a new $geoNear operator to this aggregate pipeline.
*
Expand Down Expand Up @@ -366,7 +388,7 @@ Aggregate.prototype.near = function(arg) {
* define methods
*/

'group match skip limit out densify'.split(' ').forEach(function($operator) {
'group match skip limit out densify fill'.split(' ').forEach(function($operator) {
Aggregate.prototype[$operator] = function(arg) {
const op = {};
op['$' + $operator] = arg;
Expand Down Expand Up @@ -702,7 +724,7 @@ Aggregate.prototype.readConcern = function(level) {
Aggregate.prototype.redact = function(expression, thenExpr, elseExpr) {
if (arguments.length === 3) {
if ((typeof thenExpr === 'string' && !validRedactStringValues.has(thenExpr)) ||
(typeof elseExpr === 'string' && !validRedactStringValues.has(elseExpr))) {
(typeof elseExpr === 'string' && !validRedactStringValues.has(elseExpr))) {
throw new Error('If thenExpr or elseExpr is string, it must be either $$DESCEND, $$PRUNE or $$KEEP');
}

Expand Down Expand Up @@ -1099,9 +1121,7 @@ Aggregate.prototype.catch = function(reject) {

if (Symbol.asyncIterator != null) {
Aggregate.prototype[Symbol.asyncIterator] = function() {
return this.cursor({ useMongooseAggCursor: true }).
transformNull().
_transformForAsyncIterator();
return this.cursor({ useMongooseAggCursor: true }).transformNull()._transformForAsyncIterator();
};
}

Expand Down
42 changes: 42 additions & 0 deletions lib/document.js
Expand Up @@ -948,6 +948,48 @@ Document.prototype.$session = function $session(session) {
return session;
};

/**
* Getter/setter around whether this document will apply timestamps by
* default when using `save()` and `bulkSave()`.
*
* #### Example:
*
* const TestModel = mongoose.model('Test', new Schema({ name: String }, { timestamps: true }));
* const doc = new TestModel({ name: 'John Smith' });
*
* doc.$timestamps(); // true
*
* doc.$timestamps(false);
* await doc.save(); // Does **not** apply timestamps
*
* @param {Boolean} [value] overwrite the current session
* @return {Document|boolean|undefined} When used as a getter (no argument), a boolean will be returned indicating the timestamps option state or if unset "undefined" will be used, otherwise will return "this"
* @method $timestamps
* @api public
* @memberOf Document
*/

Document.prototype.$timestamps = function $timestamps(value) {
if (arguments.length === 0) {
if (this.$__.timestamps != null) {
return this.$__.timestamps;
}

if (this.$__schema) {
return this.$__schema.options.timestamps;
}

return undefined;
}

const currentValue = this.$timestamps();
if (value !== currentValue) {
this.$__.timestamps = value;
}

return this;
};

/**
* Overwrite all values in this document with the values of `obj`, except
* for immutable properties. Behaves similarly to `set()`, except for it
Expand Down
101 changes: 101 additions & 0 deletions lib/error/setOptionError.js
@@ -0,0 +1,101 @@
/*!
* Module requirements
*/

'use strict';

const MongooseError = require('./mongooseError');
const util = require('util');
const combinePathErrors = require('../helpers/error/combinePathErrors');

class SetOptionError extends MongooseError {
/**
* Mongoose.set Error
*
* @api private
* @inherits MongooseError
*/
constructor() {
super('');

this.errors = {};
}

/**
* Console.log helper
*/
toString() {
return combinePathErrors(this);
}

/**
* inspect helper
* @api private
*/
inspect() {
return Object.assign(new Error(this.message), this);
}

/**
* add message
* @param {String} key
* @param {String|Error} error
* @api private
*/
addError(key, error) {
if (error instanceof SetOptionError) {
const { errors } = error;
for (const optionKey of Object.keys(errors)) {
this.addError(optionKey, errors[optionKey]);
}

return;
}

this.errors[key] = error;
this.message = combinePathErrors(this);
}
}


if (util.inspect.custom) {
// Avoid Node deprecation warning DEP0079
SetOptionError.prototype[util.inspect.custom] = SetOptionError.prototype.inspect;
}

/**
* Helper for JSON.stringify
* Ensure `name` and `message` show up in toJSON output re: gh-9847
* @api private
*/
Object.defineProperty(SetOptionError.prototype, 'toJSON', {
enumerable: false,
writable: false,
configurable: true,
value: function() {
return Object.assign({}, this, { name: this.name, message: this.message });
}
});


Object.defineProperty(SetOptionError.prototype, 'name', {
value: 'SetOptionError'
});

class SetOptionInnerError extends MongooseError {
/**
* Error for the "errors" array in "SetOptionError" with consistent message
* @param {String} key
*/
constructor(key) {
super(`"${key}" is not a valid option to set`);
}
}

SetOptionError.SetOptionInnerError = SetOptionInnerError;

/*!
* Module exports
*/

module.exports = SetOptionError;
26 changes: 3 additions & 23 deletions lib/error/validation.js
Expand Up @@ -7,6 +7,7 @@
const MongooseError = require('./mongooseError');
const getConstructorName = require('../helpers/getConstructorName');
const util = require('util');
const combinePathErrors = require('../helpers/error/combinePathErrors');

class ValidationError extends MongooseError {
/**
Expand Down Expand Up @@ -38,7 +39,7 @@ class ValidationError extends MongooseError {
* Console.log helper
*/
toString() {
return this.name + ': ' + _generateMessage(this);
return this.name + ': ' + combinePathErrors(this);
}

/**
Expand Down Expand Up @@ -66,7 +67,7 @@ class ValidationError extends MongooseError {
}

this.errors[path] = error;
this.message = this._message + ': ' + _generateMessage(this);
this.message = this._message + ': ' + combinePathErrors(this);
}
}

Expand Down Expand Up @@ -95,27 +96,6 @@ Object.defineProperty(ValidationError.prototype, 'name', {
value: 'ValidationError'
});

/*!
* ignore
*/

function _generateMessage(err) {
const keys = Object.keys(err.errors || {});
const len = keys.length;
const msgs = [];
let key;

for (let i = 0; i < len; ++i) {
key = keys[i];
if (err === err.errors[key]) {
continue;
}
msgs.push(key + ': ' + err.errors[key].message);
}

return msgs.join(', ');
}

/*!
* Module exports
*/
Expand Down
22 changes: 22 additions & 0 deletions lib/helpers/error/combinePathErrors.js
@@ -0,0 +1,22 @@
'use strict';

/*!
* ignore
*/

module.exports = function combinePathErrors(err) {
const keys = Object.keys(err.errors || {});
const len = keys.length;
const msgs = [];
let key;

for (let i = 0; i < len; ++i) {
key = keys[i];
if (err === err.errors[key]) {
continue;
}
msgs.push(key + ': ' + err.errors[key].message);
}

return msgs.join(', ');
};
10 changes: 7 additions & 3 deletions lib/helpers/model/discriminator.js
Expand Up @@ -19,11 +19,13 @@ const CUSTOMIZABLE_DISCRIMINATOR_OPTIONS = {
* ignore
*/

module.exports = function discriminator(model, name, schema, tiedValue, applyPlugins) {
module.exports = function discriminator(model, name, schema, tiedValue, applyPlugins, mergeHooks) {
if (!(schema && schema.instanceOfSchema)) {
throw new Error('You must pass a valid discriminator Schema');
}

mergeHooks = mergeHooks == null ? true : mergeHooks;

if (model.schema.discriminatorMapping &&
!model.schema.discriminatorMapping.isRoot) {
throw new Error('Discriminator "' + name +
Expand All @@ -32,7 +34,7 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu

if (applyPlugins) {
const applyPluginsToDiscriminators = get(model.base,
'options.applyPluginsToDiscriminators', false);
'options.applyPluginsToDiscriminators', false) || !mergeHooks;
// Even if `applyPluginsToDiscriminators` isn't set, we should still apply
// global plugins to schemas embedded in the discriminator schema (gh-7370)
model.base._applyPlugins(schema, {
Expand Down Expand Up @@ -179,7 +181,9 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
schema.options._id = _id;
}
schema.options.id = id;
schema.s.hooks = model.schema.s.hooks.merge(schema.s.hooks);
if (mergeHooks) {
schema.s.hooks = model.schema.s.hooks.merge(schema.s.hooks);
}

schema.plugins = Array.prototype.slice.call(baseSchema.plugins);
schema.callQueue = baseSchema.callQueue.concat(schema.callQueue);
Expand Down