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

readConcern #14532

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/helpers/schema/applyReadConcern.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const get = require('../get');

module.exports = function applyReadConcern(schema, options) {
if (options.readConcern != null) {
return;
}
// Don't apply default write concern to operations in transactions,
// because setting write concern on an operation in a transaction is an error
// See: https://www.mongodb.com/docs/manual/reference/write-concern/
if (options && options.session && options.session.transaction) {
return;
}
const readConcern = get(schema, 'options.readConcern', {});
if (Object.keys(readConcern).length != 0) {
options.readConcern = {};
if (!('majority' in options) && readConcern.level != null) {
options.readConcern.level = readConcern.level;
}
}
else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This else {} statement does the exact same thing as the if {} branch above, please get rid of it

if (!('majority' in options) && readConcern.level != null) {
options.readConcern.level = readConcern.level;
}
}
};
11 changes: 0 additions & 11 deletions lib/helpers/schema/applyWriteConcern.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,4 @@ module.exports = function applyWriteConcern(schema, options) {
options.writeConcern.wtimeout = writeConcern.wtimeout;
}
}
else {
if (!('w' in options) && writeConcern.w != null) {
options.w = writeConcern.w;
}
if (!('j' in options) && writeConcern.j != null) {
options.j = writeConcern.j;
}
if (!('wtimeout' in options) && writeConcern.wtimeout != null) {
options.wtimeout = writeConcern.wtimeout;
}
}
};
2 changes: 2 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const util = require('util');
const utils = require('./utils');
const MongooseBulkWriteError = require('./error/bulkWriteError');
const minimize = require('./helpers/minimize');
const applyReadConcern = require('./helpers/schema/applyReadConcern');

const VERSION_WHERE = 1;
const VERSION_INC = 2;
Expand Down Expand Up @@ -1982,6 +1983,7 @@ function _ensureIndexes(model, options, callback) {
delete indexOptions._autoIndex;
decorateDiscriminatorIndexOptions(model.schema, indexOptions);
applyWriteConcern(model.schema, indexOptions);
applyReadConcern(model.schema, indexOptions);
applySchemaCollation(indexFields, indexOptions, model.schema.options);

indexSingleStart(indexFields, options);
Expand Down
3 changes: 2 additions & 1 deletion lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const specialProperties = require('./helpers/specialProperties');
const updateValidators = require('./helpers/updateValidators');
const util = require('util');
const utils = require('./utils');
const applyReadConcern = require('./helpers/schema/applyReadConcern');
const queryMiddlewareFunctions = require('./constants').queryMiddlewareFunctions;

const queryOptionMethods = new Set([
Expand Down Expand Up @@ -1946,7 +1947,7 @@ Query.prototype._optionsForExec = function(model) {
}
// Apply schema-level `writeConcern` option
applyWriteConcern(model.schema, options);

applyReadConcern(model.schema, options);
const readPreference = model &&
model.schema &&
model.schema.options &&
Expand Down
22 changes: 22 additions & 0 deletions test/helpers/applyReadConcern.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const assert = require('assert');
const applyReadConcern = require('../../lib/helpers/schema/applyReadConcern');
const start = require('../common');
const mongoose = start.mongoose;

describe('applyReadConcern', function() {
IslandRhythms marked this conversation as resolved.
Show resolved Hide resolved
let db;
before(function() {
db = start();
});
after(async function() {
await db.close();
});
it('should not overwrite user specified read options (gh-14511)', async function() {
const options = { readConcern: { level: 'majority' } };
const testSchema = new mongoose.Schema({ name: String }, { readConcern: { level: 'majority ' } });
applyReadConcern(testSchema, options);
assert.deepStrictEqual({ readConcern: { level: 'majority' } }, options);
});
});
29 changes: 29 additions & 0 deletions test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,23 @@ describe('Query', function() {
});
});

it('applies schema-level readConcern option', function() {
const q = new Query();

const options = q._optionsForExec({
schema: {
options: {
readConcern: { level: 'majority' }
}
}
});
assert.deepEqual(options, {
readConcern: {
level: 'majority'
}
});
});

it('session() (gh-6663)', function() {
const q = new Query();

Expand Down Expand Up @@ -3412,6 +3429,18 @@ describe('Query', function() {

assert.deepEqual(q.options.writeConcern, { w: 'majority', wtimeout: 1000 });
});

it('sets `readConcer` option correctly (gh-14511)', function() {
const testSchema = new mongoose.Schema({
name: String
});
const Test = db.model('Test', testSchema);

const q = Test.find();
q.readConcern({ level: 'majority' });

assert.deepEqual(q.options.readConcern, { level: 'majority' });
});
it('no longer has the deprecation warning message with writeConcern gh-10083', async function() {
const MySchema = new mongoose.Schema(
{
Expand Down