Skip to content

Commit

Permalink
Merge branch 'master' into vkarpov15/gh-13755
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Oct 10, 2023
2 parents 098c644 + 3042ac1 commit 09e8445
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 26 deletions.
4 changes: 4 additions & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ reporter: spec # better to identify failing / slow tests than "dot"
ui: bdd # explicitly setting, even though it is mocha default
require:
- test/mocha-fixtures.js
extension:
- test.js
watch-files:
- test/**/*.js
11 changes: 10 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,13 @@ webpack.base.config.js
notes.md
list.out

eslintrc.json
# config files
lgtm.yml
.mocharc.yml
.eslintrc.js
.markdownlint-cli2.cjs
tsconfig.json

# scripts
scripts/
tools/
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
7.6.1 / 2023-10-09
==================
* fix: bump bson to match mongodb@5.9.0 exactly #13947 [hasezoey](https://github.com/hasezoey)
* fix: raw result deprecation message #13954 [simllll](https://github.com/simllll)
* type: add types for includeResultMetadata #13955 [simllll](https://github.com/simllll)
* perf(npmignore): ignore newer files #13946 [hasezoey](https://github.com/hasezoey)
* perf: move mocha config from package.json to mocharc #13948 [hasezoey](https://github.com/hasezoey)

7.6.0 / 2023-10-06
==================
* feat: upgrade mongodb node driver -> 5.9.0 #13927 #13926 [sanguineti](https://github.com/sanguineti)
Expand Down
7 changes: 7 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3780,6 +3780,7 @@ Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, op
}

setDefaultOptions();
const discriminatorKey = this.schema.options.discriminatorKey;

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

_applyCustomWhere(document, where);

// Set the discriminator key, so bulk write casting knows which
// schema to use re: gh-13907
if (document[discriminatorKey] != null && !(discriminatorKey in where)) {
where[discriminatorKey] = document[discriminatorKey];
}

document.$__version(where, delta);
const writeOperation = { updateOne: { filter: where, update: changes } };
utils.injectTimestampsOption(writeOperation.updateOne, options.timestamps);
Expand Down
2 changes: 1 addition & 1 deletion lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -1680,7 +1680,7 @@ Query.prototype.setOptions = function(options, overwrite) {

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

/*!
Expand Down
13 changes: 0 additions & 13 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,19 +723,6 @@ Schema.prototype.add = function add(obj, prefix) {
for (const key in val[0].discriminators) {
schemaType.discriminator(key, val[0].discriminators[key]);
}
} else if (val[0] != null && val[0].instanceOfSchema && val[0]._applyDiscriminators instanceof Map) {
const applyDiscriminators = val[0]._applyDiscriminators;
const schemaType = this.path(prefix + key);
for (const disc of applyDiscriminators.keys()) {
schemaType.discriminator(disc, applyDiscriminators.get(disc));
}
}
else if (val != null && val.instanceOfSchema && val._applyDiscriminators instanceof Map) {
const applyDiscriminators = val._applyDiscriminators;
const schemaType = this.path(prefix + key);
for (const disc of applyDiscriminators.keys()) {
schemaType.discriminator(disc, applyDiscriminators.get(disc));
}
}
} else if (Object.keys(val).length < 1) {
// Special-case: {} always interpreted as Mixed path so leaf at this node
Expand Down
6 changes: 6 additions & 0 deletions lib/schema/SubdocumentPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ function SubdocumentPath(schema, path, options) {
this.$isSingleNested = true;
this.base = schema.base;
SchemaType.call(this, path, options, 'Embedded');

if (schema._applyDiscriminators != null) {
for (const disc of schema._applyDiscriminators.keys()) {
this.discriminator(disc, schema._applyDiscriminators.get(disc));
}
}
}

/*!
Expand Down
6 changes: 6 additions & 0 deletions lib/schema/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ function DocumentArrayPath(key, schema, options, schemaOptions) {

this.$embeddedSchemaType.caster = this.Constructor;
this.$embeddedSchemaType.schema = this.schema;

if (schema._applyDiscriminators != null) {
for (const disc of schema._applyDiscriminators.keys()) {
this.discriminator(disc, schema._applyDiscriminators.get(disc));
}
}
}

/**
Expand Down
13 changes: 2 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mongoose",
"description": "Mongoose MongoDB ODM",
"version": "7.6.0",
"version": "7.6.1",
"author": "Guillermo Rauch <guillermo@learnboost.com>",
"keywords": [
"mongodb",
Expand All @@ -19,7 +19,7 @@
],
"license": "MIT",
"dependencies": {
"bson": "^5.4.0",
"bson": "^5.5.0",
"kareem": "2.5.1",
"mongodb": "5.9.0",
"mpath": "0.9.0",
Expand All @@ -39,7 +39,6 @@
"axios": "1.1.3",
"babel-loader": "8.2.5",
"benchmark": "2.1.4",
"bluebird": "3.7.2",
"broken-link-checker": "^0.7.8",
"buffer": "^5.6.0",
"cheerio": "1.0.0-rc.12",
Expand Down Expand Up @@ -130,14 +129,6 @@
},
"homepage": "https://mongoosejs.com",
"browser": "./dist/browser.umd.js",
"mocha": {
"extension": [
"test.js"
],
"watch-files": [
"test/**/*.js"
]
},
"config": {
"mongodbMemoryServer": {
"disablePostinstall": true
Expand Down
50 changes: 50 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12520,6 +12520,56 @@ describe('document', function() {
await doc.save();
assert.strictEqual(attachmentSchemaPreValidateCalls, 1);
});

it('handles embedded discriminators defined using Schema.prototype.discriminator (gh-13898)', async function() {
const baseNestedDiscriminated = new Schema({
type: { type: Number, required: true }
}, { discriminatorKey: 'type' });

class BaseClass {
whoAmI() {
return 'I am baseNestedDiscriminated';
}
}
BaseClass.type = 1;

baseNestedDiscriminated.loadClass(BaseClass);

class NumberTyped extends BaseClass {
whoAmI() {
return 'I am NumberTyped';
}
}
NumberTyped.type = 3;

class StringTyped extends BaseClass {
whoAmI() {
return 'I am StringTyped';
}
}
StringTyped.type = 4;

baseNestedDiscriminated.discriminator(1, new Schema({}).loadClass(NumberTyped));
baseNestedDiscriminated.discriminator('3', new Schema({}).loadClass(StringTyped));

const containsNestedSchema = new Schema({
nestedDiscriminatedTypes: { type: [baseNestedDiscriminated], required: true }
});

class ContainsNested {
whoAmI() {
return 'I am ContainsNested';
}
}
containsNestedSchema.loadClass(ContainsNested);

const Test = db.model('Test', containsNestedSchema);
const instance = await Test.create({ type: 1, nestedDiscriminatedTypes: [{ type: 1 }, { type: '3' }] });
assert.deepStrictEqual(
instance.nestedDiscriminatedTypes.map(i => i.whoAmI()),
['I am NumberTyped', 'I am StringTyped']
);
});
});

describe('Check if instance function that is supplied in schema option is availabe', function() {
Expand Down
21 changes: 21 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6239,6 +6239,27 @@ describe('Model', function() {
assert.equal(writeOperations.length, 3);
});

it('saves changes in discriminators if calling `bulkSave()` on base model (gh-13907)', async() => {
const schema = new mongoose.Schema(
{ value: String },
{ discriminatorKey: 'type' }
);
const typeASchema = new mongoose.Schema({ aValue: String });
schema.discriminator('A', typeASchema);

const TestModel = db.model('Test', schema);
const testData = { value: 'initValue', type: 'A', aValue: 'initAValue' };
const doc = await TestModel.create(testData);

doc.value = 'updatedValue1';
doc.aValue = 'updatedValue2';
await TestModel.bulkSave([doc]);

const findDoc = await TestModel.findById(doc._id);
assert.strictEqual(findDoc.value, 'updatedValue1');
assert.strictEqual(findDoc.aValue, 'updatedValue2');
});

it('accepts `timestamps: false` (gh-12059)', async() => {
// Arrange
const userSchema = new Schema({
Expand Down
2 changes: 2 additions & 0 deletions types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ declare module 'mongoose' {
PopulateOption,
SessionOption {
limit?: number;
// @deprecated, use includeResultMetadata instead
rawResult?: boolean;
includeResultMetadata?: boolean;
ordered?: boolean;
lean?: boolean;
throwOnValidationError?: boolean;
Expand Down
5 changes: 5 additions & 0 deletions types/query.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,14 @@ declare module 'mongoose' {
overwriteDiscriminatorKey?: boolean;
projection?: ProjectionType<DocType>;
/**
* @deprecated use includeResultMetadata instead.
* if true, returns the raw result from the MongoDB driver
*/
rawResult?: boolean;
/**
* if ture, includes meta data for the result from the MongoDB driver
*/
includeResultMetadata?: boolean;
readPreference?: string | mongodb.ReadPreferenceMode;
/**
* An alias for the `new` option. `returnOriginal: false` is equivalent to `new: true`.
Expand Down

0 comments on commit 09e8445

Please sign in to comment.