Skip to content

Commit 09e8445

Browse files
committed
Merge branch 'master' into vkarpov15/gh-13755
2 parents 098c644 + 3042ac1 commit 09e8445

File tree

13 files changed

+122
-26
lines changed

13 files changed

+122
-26
lines changed

.mocharc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ reporter: spec # better to identify failing / slow tests than "dot"
22
ui: bdd # explicitly setting, even though it is mocha default
33
require:
44
- test/mocha-fixtures.js
5+
extension:
6+
- test.js
7+
watch-files:
8+
- test/**/*.js

.npmignore

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,13 @@ webpack.base.config.js
4747
notes.md
4848
list.out
4949

50-
eslintrc.json
50+
# config files
51+
lgtm.yml
52+
.mocharc.yml
53+
.eslintrc.js
54+
.markdownlint-cli2.cjs
55+
tsconfig.json
56+
57+
# scripts
58+
scripts/
59+
tools/

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
7.6.1 / 2023-10-09
2+
==================
3+
* fix: bump bson to match mongodb@5.9.0 exactly #13947 [hasezoey](https://github.com/hasezoey)
4+
* fix: raw result deprecation message #13954 [simllll](https://github.com/simllll)
5+
* type: add types for includeResultMetadata #13955 [simllll](https://github.com/simllll)
6+
* perf(npmignore): ignore newer files #13946 [hasezoey](https://github.com/hasezoey)
7+
* perf: move mocha config from package.json to mocharc #13948 [hasezoey](https://github.com/hasezoey)
8+
19
7.6.0 / 2023-10-06
210
==================
311
* feat: upgrade mongodb node driver -> 5.9.0 #13927 #13926 [sanguineti](https://github.com/sanguineti)

lib/model.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3780,6 +3780,7 @@ Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, op
37803780
}
37813781

37823782
setDefaultOptions();
3783+
const discriminatorKey = this.schema.options.discriminatorKey;
37833784

37843785
const writeOperations = documents.reduce((accumulator, document, i) => {
37853786
if (!options.skipValidation) {
@@ -3810,6 +3811,12 @@ Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, op
38103811

38113812
_applyCustomWhere(document, where);
38123813

3814+
// Set the discriminator key, so bulk write casting knows which
3815+
// schema to use re: gh-13907
3816+
if (document[discriminatorKey] != null && !(discriminatorKey in where)) {
3817+
where[discriminatorKey] = document[discriminatorKey];
3818+
}
3819+
38133820
document.$__version(where, delta);
38143821
const writeOperation = { updateOne: { filter: where, update: changes } };
38153822
utils.injectTimestampsOption(writeOperation.updateOne, options.timestamps);

lib/query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1680,7 +1680,7 @@ Query.prototype.setOptions = function(options, overwrite) {
16801680

16811681
const printRawResultDeprecationWarning = util.deprecate(
16821682
function printRawResultDeprecationWarning() {},
1683-
'The `rawResult` option for Mongoose queries is deprecated. Use `includeResultMetadata: false` as a replacement for `rawResult: true`.'
1683+
'The `rawResult` option for Mongoose queries is deprecated. Use `includeResultMetadata: true` as a replacement for `rawResult: true`.'
16841684
);
16851685

16861686
/*!

lib/schema.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -723,19 +723,6 @@ Schema.prototype.add = function add(obj, prefix) {
723723
for (const key in val[0].discriminators) {
724724
schemaType.discriminator(key, val[0].discriminators[key]);
725725
}
726-
} else if (val[0] != null && val[0].instanceOfSchema && val[0]._applyDiscriminators instanceof Map) {
727-
const applyDiscriminators = val[0]._applyDiscriminators;
728-
const schemaType = this.path(prefix + key);
729-
for (const disc of applyDiscriminators.keys()) {
730-
schemaType.discriminator(disc, applyDiscriminators.get(disc));
731-
}
732-
}
733-
else if (val != null && val.instanceOfSchema && val._applyDiscriminators instanceof Map) {
734-
const applyDiscriminators = val._applyDiscriminators;
735-
const schemaType = this.path(prefix + key);
736-
for (const disc of applyDiscriminators.keys()) {
737-
schemaType.discriminator(disc, applyDiscriminators.get(disc));
738-
}
739726
}
740727
} else if (Object.keys(val).length < 1) {
741728
// Special-case: {} always interpreted as Mixed path so leaf at this node

lib/schema/SubdocumentPath.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ function SubdocumentPath(schema, path, options) {
5555
this.$isSingleNested = true;
5656
this.base = schema.base;
5757
SchemaType.call(this, path, options, 'Embedded');
58+
59+
if (schema._applyDiscriminators != null) {
60+
for (const disc of schema._applyDiscriminators.keys()) {
61+
this.discriminator(disc, schema._applyDiscriminators.get(disc));
62+
}
63+
}
5864
}
5965

6066
/*!

lib/schema/documentarray.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ function DocumentArrayPath(key, schema, options, schemaOptions) {
8888

8989
this.$embeddedSchemaType.caster = this.Constructor;
9090
this.$embeddedSchemaType.schema = this.schema;
91+
92+
if (schema._applyDiscriminators != null) {
93+
for (const disc of schema._applyDiscriminators.keys()) {
94+
this.discriminator(disc, schema._applyDiscriminators.get(disc));
95+
}
96+
}
9197
}
9298

9399
/**

package.json

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "mongoose",
33
"description": "Mongoose MongoDB ODM",
4-
"version": "7.6.0",
4+
"version": "7.6.1",
55
"author": "Guillermo Rauch <guillermo@learnboost.com>",
66
"keywords": [
77
"mongodb",
@@ -19,7 +19,7 @@
1919
],
2020
"license": "MIT",
2121
"dependencies": {
22-
"bson": "^5.4.0",
22+
"bson": "^5.5.0",
2323
"kareem": "2.5.1",
2424
"mongodb": "5.9.0",
2525
"mpath": "0.9.0",
@@ -39,7 +39,6 @@
3939
"axios": "1.1.3",
4040
"babel-loader": "8.2.5",
4141
"benchmark": "2.1.4",
42-
"bluebird": "3.7.2",
4342
"broken-link-checker": "^0.7.8",
4443
"buffer": "^5.6.0",
4544
"cheerio": "1.0.0-rc.12",
@@ -130,14 +129,6 @@
130129
},
131130
"homepage": "https://mongoosejs.com",
132131
"browser": "./dist/browser.umd.js",
133-
"mocha": {
134-
"extension": [
135-
"test.js"
136-
],
137-
"watch-files": [
138-
"test/**/*.js"
139-
]
140-
},
141132
"config": {
142133
"mongodbMemoryServer": {
143134
"disablePostinstall": true

test/document.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12520,6 +12520,56 @@ describe('document', function() {
1252012520
await doc.save();
1252112521
assert.strictEqual(attachmentSchemaPreValidateCalls, 1);
1252212522
});
12523+
12524+
it('handles embedded discriminators defined using Schema.prototype.discriminator (gh-13898)', async function() {
12525+
const baseNestedDiscriminated = new Schema({
12526+
type: { type: Number, required: true }
12527+
}, { discriminatorKey: 'type' });
12528+
12529+
class BaseClass {
12530+
whoAmI() {
12531+
return 'I am baseNestedDiscriminated';
12532+
}
12533+
}
12534+
BaseClass.type = 1;
12535+
12536+
baseNestedDiscriminated.loadClass(BaseClass);
12537+
12538+
class NumberTyped extends BaseClass {
12539+
whoAmI() {
12540+
return 'I am NumberTyped';
12541+
}
12542+
}
12543+
NumberTyped.type = 3;
12544+
12545+
class StringTyped extends BaseClass {
12546+
whoAmI() {
12547+
return 'I am StringTyped';
12548+
}
12549+
}
12550+
StringTyped.type = 4;
12551+
12552+
baseNestedDiscriminated.discriminator(1, new Schema({}).loadClass(NumberTyped));
12553+
baseNestedDiscriminated.discriminator('3', new Schema({}).loadClass(StringTyped));
12554+
12555+
const containsNestedSchema = new Schema({
12556+
nestedDiscriminatedTypes: { type: [baseNestedDiscriminated], required: true }
12557+
});
12558+
12559+
class ContainsNested {
12560+
whoAmI() {
12561+
return 'I am ContainsNested';
12562+
}
12563+
}
12564+
containsNestedSchema.loadClass(ContainsNested);
12565+
12566+
const Test = db.model('Test', containsNestedSchema);
12567+
const instance = await Test.create({ type: 1, nestedDiscriminatedTypes: [{ type: 1 }, { type: '3' }] });
12568+
assert.deepStrictEqual(
12569+
instance.nestedDiscriminatedTypes.map(i => i.whoAmI()),
12570+
['I am NumberTyped', 'I am StringTyped']
12571+
);
12572+
});
1252312573
});
1252412574

1252512575
describe('Check if instance function that is supplied in schema option is availabe', function() {

0 commit comments

Comments
 (0)