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 3 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 (!('majoriy' in options) && readConcern.level != null) {
IslandRhythms marked this conversation as resolved.
Show resolved Hide resolved
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 (!('majoriy' in options) && readConcern.level != null) {
IslandRhythms marked this conversation as resolved.
Show resolved Hide resolved
options.readConcern.level = readConcern.level;
}
}
};
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
16 changes: 15 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 @@ -1311,6 +1312,19 @@ Query.prototype.writeConcern = function writeConcern(val) {
return this;
};

/*
Query.prototype.readConcern = function readConcern(val) {
IslandRhythms marked this conversation as resolved.
Show resolved Hide resolved
console.log('what is val', val)
if (val == null) {
delete this.options.readConcern;
return this;
}

this.options.readConcern = val;
return this;
}
*/

/**
* Sets the specified number of `mongod` servers, or tag set of `mongod` servers,
* that must acknowledge this write before this write is considered successful.
Expand Down Expand Up @@ -1946,7 +1960,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);
});
});