Skip to content

Commit

Permalink
remove ReadPreference from driver layer and use bson Binary, Decimal1…
Browse files Browse the repository at this point in the history
…28, ObjectId re: #12638
  • Loading branch information
vkarpov15 committed Dec 26, 2022
1 parent 884139d commit 1f7b6fe
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 120 deletions.
7 changes: 0 additions & 7 deletions lib/drivers/browser/ReadPreference.js

This file was deleted.

1 change: 0 additions & 1 deletion lib/drivers/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ exports.getConnection = () => function() {
};
exports.Decimal128 = require('./decimal128');
exports.ObjectId = require('./objectid');
exports.ReadPreference = require('./ReadPreference');
47 changes: 0 additions & 47 deletions lib/drivers/node-mongodb-native/ReadPreference.js

This file was deleted.

1 change: 0 additions & 1 deletion lib/drivers/node-mongodb-native/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ exports.Binary = require('./binary');
exports.Collection = require('./collection');
exports.Decimal128 = require('./decimal128');
exports.ObjectId = require('./objectid');
exports.ReadPreference = require('./ReadPreference');
exports.getConnection = () => require('./connection');
23 changes: 23 additions & 0 deletions lib/helpers/query/handleReadPreferenceAliases.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

module.exports = function handleReadPreferenceAliases(pref) {
switch (pref) {
case 'p':
pref = 'primary';
break;
case 'pp':
pref = 'primaryPreferred';
break;
case 's':
pref = 'secondary';
break;
case 'sp':
pref = 'secondaryPreferred';
break;
case 'n':
pref = 'nearest';
break;
}

return pref;
};
16 changes: 9 additions & 7 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const Kareem = require('kareem');
const MongooseError = require('./error/mongooseError');
const ObjectParameterError = require('./error/objectParameter');
const QueryCursor = require('./cursor/QueryCursor');
const ReadPreference = require('./driver').get().ReadPreference;
const ValidationError = require('./error/validation');
const { applyGlobalMaxTimeMS, applyGlobalDiskUse } = require('./helpers/query/applyGlobalOption');
const handleReadPreferenceAliases = require('./helpers/query/handleReadPreferenceAliases');
const applyWriteConcern = require('./helpers/schema/applyWriteConcern');
const cast = require('./cast');
const castArrayFilters = require('./helpers/update/castArrayFilters');
Expand Down Expand Up @@ -117,7 +117,6 @@ function Query(conditions, options, model, collection) {
this.schema = model.schema;
}


// this is needed because map reduce returns a model that can be queried, but
// all of the queries on said model should be lean
if (this.model && this.model._mapreduce) {
Expand Down Expand Up @@ -1212,17 +1211,20 @@ Query.prototype.select = function select() {
* @method read
* @memberOf Query
* @instance
* @param {String} pref one of the listed preference options or aliases
* @param {String} mode one of the listed preference options or aliases
* @param {Array} [tags] optional tags for this query
* @see mongodb https://docs.mongodb.org/manual/applications/replication/#read-preference
* @return {Query} this
* @api public
*/

Query.prototype.read = function read(pref, tags) {
// first cast into a ReadPreference object to support tags
const read = new ReadPreference(pref, tags);
this.options.readPreference = read;
Query.prototype.read = function read(mode, tags) {
if (typeof mode === 'string') {
mode = handleReadPreferenceAliases(mode);
this.options.readPreference = { mode, tags };
} else {
this.options.readPreference = mode;
}
return this;
};

Expand Down
13 changes: 6 additions & 7 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const addAutoId = require('./helpers/schema/addAutoId');
const get = require('./helpers/get');
const getConstructorName = require('./helpers/getConstructorName');
const getIndexes = require('./helpers/schema/getIndexes');
const handleReadPreferenceAliases = require('./helpers/query/handleReadPreferenceAliases');
const idGetter = require('./helpers/schema/idGetter');
const merge = require('./helpers/schema/merge');
const mpath = require('mpath');
const readPref = require('./driver').get().ReadPreference;
const setupTimestamps = require('./helpers/timestamps/setupTimestamps');
const utils = require('./utils');
const validateRef = require('./helpers/populate/validateRef');
Expand Down Expand Up @@ -513,10 +513,6 @@ Schema.prototype.defaultOptions = function(options) {
typeKey: 'type'
}, utils.clone(options));

if (options.read) {
options.read = readPref(options.read);
}

if (options.versionKey && typeof options.versionKey !== 'string') {
throw new MongooseError('`versionKey` must be falsy or string, got `' + (typeof options.versionKey) + '`');
}
Expand Down Expand Up @@ -1941,18 +1937,21 @@ Schema.prototype.index = function(fields, options) {
*
* @param {String} key The name of the option to set the value to
* @param {Object} [value] The value to set the option to, if not passed, the option will be reset to default
* @param {Array<string>} [tags] tags to add to read preference if key === 'read'
* @see Schema #schema_Schema
* @api public
*/

Schema.prototype.set = function(key, value, _tags) {
Schema.prototype.set = function(key, value, tags) {
if (arguments.length === 1) {
return this.options[key];
}

switch (key) {
case 'read':
this.options[key] = readPref(value, _tags);
this.options[key] = typeof value === 'string' ?
{ mode: handleReadPreferenceAliases(value), tags } :
value;
this._userProvidedOptions[key] = this.options[key];
break;
case 'timestamps':
Expand Down
2 changes: 1 addition & 1 deletion lib/types/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

'use strict';

const Binary = require('../driver').get().Binary;
const Binary = require('bson').Binary;
const utils = require('../utils');

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/types/decimal128.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@

'use strict';

module.exports = require('../driver').get().Decimal128;
module.exports = require('bson').Decimal128;
2 changes: 1 addition & 1 deletion lib/types/objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

'use strict';

const ObjectId = require('../driver').get().ObjectId;
const ObjectId = require('bson').ObjectId;
const objectIdSymbol = require('../helpers/symbols').objectIdSymbol;

/**
Expand Down
2 changes: 1 addition & 1 deletion test/query.cursor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ describe('QueryCursor', function() {
const User = db.model('User', Schema({ name: String }, { read }));
const cursor = User.find().cursor();

assert.equal(cursor.options.readPreference.mode, read);
assert.equal(cursor.options.readPreference, read);
});

it('eachAsync() with parallel > numDocs (gh-8422)', async function() {
Expand Down
51 changes: 6 additions & 45 deletions test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1410,53 +1410,18 @@ describe('Query', function() {
it('works', function(done) {
const query = new Query({});
query.read('primary');
assert.ok(query.options.readPreference instanceof P);
assert.ok(query.options.readPreference.isValid());
assert.equal(query.options.readPreference.mode, 'primary');

query.read('p');
assert.ok(query.options.readPreference instanceof P);
assert.ok(query.options.readPreference.isValid());
assert.equal(query.options.readPreference.mode, 'primary');

query.read('primaryPreferred');
assert.ok(query.options.readPreference instanceof P);
assert.ok(query.options.readPreference.isValid());
assert.equal(query.options.readPreference.mode, 'primaryPreferred');

query.read('pp');
assert.ok(query.options.readPreference instanceof P);
assert.ok(query.options.readPreference.isValid());
assert.equal(query.options.readPreference.mode, 'primaryPreferred');

query.read('secondary');
assert.ok(query.options.readPreference instanceof P);
assert.ok(query.options.readPreference.isValid());
assert.equal(query.options.readPreference.mode, 'secondary');

query.read('s');
assert.ok(query.options.readPreference instanceof P);
assert.ok(query.options.readPreference.isValid());
assert.equal(query.options.readPreference.mode, 'secondary');

query.read('secondaryPreferred');
assert.ok(query.options.readPreference instanceof P);
assert.ok(query.options.readPreference.isValid());
assert.equal(query.options.readPreference.mode, 'secondaryPreferred');

query.read('sp');
assert.ok(query.options.readPreference instanceof P);
assert.ok(query.options.readPreference.isValid());
assert.equal(query.options.readPreference.mode, 'secondaryPreferred');

query.read('nearest');
assert.ok(query.options.readPreference instanceof P);
assert.ok(query.options.readPreference.isValid());
assert.equal(query.options.readPreference.mode, 'nearest');

query.read('n');
assert.ok(query.options.readPreference instanceof P);
assert.ok(query.options.readPreference.isValid());
assert.equal(query.options.readPreference.mode, 'nearest');

done();
Expand All @@ -1468,9 +1433,7 @@ describe('Query', function() {
const query = new Query({});
const tags = [{ dc: 'sf', s: 1 }, { dc: 'jp', s: 2 }];

query.read('pp', tags);
assert.ok(query.options.readPreference instanceof P);
assert.ok(query.options.readPreference.isValid());
query.read('primaryPreferred', tags);
assert.equal(query.options.readPreference.mode, 'primaryPreferred');
assert.ok(Array.isArray(query.options.readPreference.tags));
assert.equal(query.options.readPreference.tags[0].dc, 'sf');
Expand All @@ -1484,20 +1447,18 @@ describe('Query', function() {
describe('inherits its models schema read option', function() {
let schema, M, called;
before(function() {
schema = new Schema({}, { read: 'p' });
schema = new Schema({}, { read: 'primary' });
M = mongoose.model('schemaOptionReadPrefWithQuery', schema);
});

it('if not set in query', function(done) {
const options = M.where()._optionsForExec(M);
assert.ok(options.readPreference instanceof P);
assert.equal(options.readPreference.mode, 'primary');
assert.equal(options.readPreference, 'primary');
done();
});

it('if set in query', function(done) {
const options = M.where().read('s')._optionsForExec(M);
assert.ok(options.readPreference instanceof P);
const options = M.where().read('secondary')._optionsForExec(M);
assert.equal(options.readPreference.mode, 'secondary');
done();
});
Expand All @@ -1516,7 +1477,7 @@ describe('Query', function() {
const ret = getopts.call(this, model);

assert.ok(ret.readPreference);
assert.equal(ret.readPreference.mode, 'secondary');
assert.equal(ret.readPreference, 'secondary');
assert.deepEqual({ w: 'majority' }, ret.writeConcern);
called = true;

Expand Down Expand Up @@ -1546,7 +1507,7 @@ describe('Query', function() {
q.setOptions({ sort: '-blah' });
q.setOptions({ sort: { woot: -1 } });
q.setOptions({ hint: { index1: 1, index2: -1 } });
q.setOptions({ read: ['s', [{ dc: 'eu' }]] });
q.setOptions({ read: ['secondary', [{ dc: 'eu' }]] });

assert.equal(q.options.thing, 'cat');
assert.deepEqual(q._mongooseOptions.populate.fans, { path: 'fans', _docs: {}, _childDocs: [] });
Expand Down
2 changes: 1 addition & 1 deletion types/query.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ declare module 'mongoose' {
projection(): ProjectionFields<DocType> | null;

/** Determines the MongoDB nodes from which to read. */
read(pref: string | mongodb.ReadPreferenceMode, tags?: any[]): this;
read(mode: string | mongodb.ReadPreferenceMode, tags?: any[]): this;

/** Sets the readConcern option for the query. */
readConcern(level: string): this;
Expand Down

0 comments on commit 1f7b6fe

Please sign in to comment.