Skip to content

Commit

Permalink
Merge pull request #6917 from Automattic/gh6880
Browse files Browse the repository at this point in the history
Clean up remaining deprecation warnings
  • Loading branch information
vkarpov15 committed Aug 25, 2018
2 parents 972cbc5 + eda0bb8 commit 2c2dbcd
Show file tree
Hide file tree
Showing 27 changed files with 174 additions and 198 deletions.
6 changes: 5 additions & 1 deletion lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,11 @@ Connection.prototype.openUri = function(uri, options, callback) {
options.promiseLibrary = PromiseProvider.get();
}
if (!('useNewUrlParser' in options)) {
options.useNewUrlParser = false;
if ('useNewUrlParser' in this.base.options) {
options.useNewUrlParser = this.base.options.useNewUrlParser;
} else {
options.useNewUrlParser = false;
}
}

const parsePromise = new Promise((resolve, reject) => {
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Mongoose.prototype.STATES = STATES;
* - 'bufferCommands': enable/disable mongoose's buffering mechanism for all connections and models
* - 'useCreateIndex': false by default. Set to `true` to make Mongoose's default index build use `createIndex()` instead of `ensureIndex()` to avoid deprecation warnings from the MongoDB driver.
* - 'useFindAndModify': true by default. Set to `false` to make `findOneAndUpdate()` and `findOneAndRemove()` use native `findOneAndUpdate()` rather than `findAndModify()`.
* - 'useNewUrlParser': false by default. Set to `true` to make all connections set the `useNewUrlParser` option by default
* - 'cloneSchemas': false by default. Set to `true` to `clone()` all schemas before compiling into a model.
* - 'applyPluginsToDiscriminators': false by default. Set to true to apply global plugins to discriminator schemas. This typically isn't necessary because plugins are applied to the base schema and discriminators copy all middleware, methods, statics, and properties from the base schema.
* - 'objectIdGetter': true by default. Mongoose adds a getter to MongoDB ObjectId's called `_id` that returns `this` for convenience with populate. Set this to false to remove the getter.
Expand Down
2 changes: 1 addition & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@ Model.syncIndexes = function syncIndexes(options, callback) {
if (err != null) {
return cb(err);
}
this.ensureIndexes(options, err => {
this.createIndexes(options, err => {
if (err != null) {
return cb(err);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ function Query(conditions, options, model, collection) {
}

this.options = this.options || {};

// For gh-6880. mquery still needs to support `fields` by default for old
// versions of MongoDB
this.$useProjection = true;

const collation = get(this, 'schema.options.collation', null);
if (collation != null) {
this.options.collation = collation;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"mongodb-core": "3.1.2",
"mongoose-legacy-pluralize": "1.0.2",
"mpath": "0.4.1",
"mquery": "3.1.2",
"mquery": "3.2.0",
"ms": "2.0.0",
"regexp-clone": "0.0.1",
"safe-buffer": "5.1.2",
Expand Down
2 changes: 1 addition & 1 deletion test/collection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('collections:', function() {
});

const uri = 'mongodb://localhost:27017/mongoose_test';
db.openUri(process.env.MONGOOSE_TEST_URI || uri, function(err) {
db.openUri(process.env.MONGOOSE_TEST_URI || uri, { useNewUrlParser: true }, function(err) {
connected = !err;
finish();
});
Expand Down
8 changes: 8 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ if (process.env.D === '1') {
mongoose.set('debug', true);
}

// For 3.1.3 deprecations
mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true);
mongoose.set('useNewUrlParser', true);

/**
* Override all Collection related queries to keep count
*/
Expand Down Expand Up @@ -82,6 +87,9 @@ module.exports = function(options) {
var noErrorListener = !!options.noErrorListener;
delete options.noErrorListener;

// For 3.1.3 deprecations
options.useNewUrlParser = true;

var conn = mongoose.createConnection(uri, options);

if (noErrorListener) {
Expand Down
40 changes: 18 additions & 22 deletions test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ describe('connections:', function() {

it('with autoIndex (gh-5423)', function(done) {
var promise = mongoose.createConnection('mongodb://localhost:27017/mongoosetest', {
autoIndex: false
autoIndex: false,
useNewUrlParser: true
});

promise.then(function(conn) {
Expand All @@ -67,15 +68,16 @@ describe('connections:', function() {

it('throws helpful error with undefined uri (gh-6763)', function(done) {
assert.throws(function() {
mongoose.createConnection(void 0);
mongoose.createConnection(void 0, { useNewUrlParser: true });
}, /string.*createConnection/);
done();
});

it('resolving with q (gh-5714)', function(done) {
var bootMongo = Q.defer();

var conn = mongoose.createConnection('mongodb://localhost:27017/mongoosetest');
var conn = mongoose.createConnection('mongodb://localhost:27017/mongoosetest',
{ useNewUrlParser: true });

conn.on('connected', function() {
bootMongo.resolve(this);
Expand Down Expand Up @@ -110,7 +112,9 @@ describe('connections:', function() {
var numReconnected = 0;
var numReconnect = 0;
var numClose = 0;
conn = mongoose.createConnection('mongodb://localhost:27000/mongoosetest');
conn = mongoose.createConnection('mongodb://localhost:27000/mongoosetest', {
useNewUrlParser: true
});

conn.on('connected', function() {
++numConnected;
Expand Down Expand Up @@ -177,7 +181,8 @@ describe('connections:', function() {
var numReconnected = 0;
conn = mongoose.createConnection('mongodb://localhost:27000/mongoosetest', {
reconnectTries: 3,
reconnectInterval: 100
reconnectInterval: 100,
useNewUrlParser: true
});

conn.on('connected', function() {
Expand Down Expand Up @@ -245,7 +250,8 @@ describe('connections:', function() {
var numDisconnected = 0;
conn = mongoose.createConnection('mongodb://localhost:27000/mongoosetest', {
socketTimeoutMS: 100,
poolSize: 1
poolSize: 1,
useNewUrlParser: true
});

conn.on('timeout', function() {
Expand Down Expand Up @@ -351,18 +357,8 @@ describe('connections:', function() {
db.close(done);
});

it('should accept mongodb://localhost/fake', function(done) {
const db = mongoose.createConnection('mongodb://localhost/fake', () => {
db.close(done);
});
assert.ok(db instanceof mongoose.Connection);
assert.equal(db.name, 'fake');
assert.equal(db.host, 'localhost');
assert.equal(db.port, 27017);
});

it('should accept mongodb://aaron:psw@localhost:27000/fake', function(done) {
var db = mongoose.createConnection('mongodb://aaron:psw@localhost:27000/fake', () => {
var db = mongoose.createConnection('mongodb://aaron:psw@localhost:27000/fake', { useNewUrlParser: true }, () => {
db.close(done);
});
assert.equal(db.pass, 'psw');
Expand All @@ -374,7 +370,7 @@ describe('connections:', function() {

it('should accept unix domain sockets', function(done) {
const host = encodeURIComponent('/tmp/mongodb-27017.sock');
var db = mongoose.createConnection(`mongodb://aaron:psw@${host}/fake`);
var db = mongoose.createConnection(`mongodb://aaron:psw@${host}/fake`, { useNewUrlParser: true });
db.catch(() => {});
assert.equal(db.name, 'fake');
assert.equal(db.host, '/tmp/mongodb-27017.sock');
Expand Down Expand Up @@ -414,7 +410,7 @@ describe('connections:', function() {

describe('connect callbacks', function() {
it('execute with user:pwd connection strings', function(done) {
var db = mongoose.createConnection('mongodb://aaron:psw@localhost:27000/fake', function() {
var db = mongoose.createConnection('mongodb://aaron:psw@localhost:27000/fake', { useNewUrlParser: true }, function() {
done();
});
db.catch(() => {});
Expand All @@ -424,7 +420,7 @@ describe('connections:', function() {
db.close();
});
it('execute without user:pwd connection strings', function(done) {
var db = mongoose.createConnection('mongodb://localhost/fake', function() {
var db = mongoose.createConnection('mongodb://localhost/fake', { useNewUrlParser: true }, function() {
});
db.on('error', function(err) {
assert.ok(err);
Expand All @@ -439,15 +435,15 @@ describe('connections:', function() {
});

it('should return an error if malformed uri passed', function(done) {
var db = mongoose.createConnection('mongodb:///fake', function(err) {
var db = mongoose.createConnection('mongodb:///fake', { useNewUrlParser: true }, function(err) {
assert.ok(/hostname/.test(err.message));
done();
});
db.close();
assert.ok(!db.options);
});
it('should use admin db if not specified and user/pass specified', function(done) {
var db = mongoose.createConnection('mongodb://u:p@localhost/admin', function() {
var db = mongoose.createConnection('mongodb://u:p@localhost/admin', { useNewUrlParser: true }, function() {
done();
});
assert.equal(typeof db.options, 'object');
Expand Down
6 changes: 3 additions & 3 deletions test/docs/discriminators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ describe('discriminator docs', function () {
db.close(done);
});

beforeEach(function (done) {
Event.remove({}, done);
beforeEach(function(done) {
Event.deleteMany({}, done);
});

/**
Expand Down Expand Up @@ -95,7 +95,7 @@ describe('discriminator docs', function () {
assert.ifError(error);
// acquit:ignore:end

Event.count({}, function (error, count) {
Event.countDocuments({}, function (error, count) {
// acquit:ignore:start
assert.ifError(error);
// acquit:ignore:end
Expand Down
2 changes: 1 addition & 1 deletion test/docs/promises.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('promises docs', function () {
});

beforeEach(function (done) {
Band.remove({}, done);
Band.deleteMany({}, done);
});

after(function (done) {
Expand Down
16 changes: 8 additions & 8 deletions test/docs/validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ describe('validation docs', function() {
}, 'Invalid color');

var opts = { runValidators: true };
Toy.update({}, { color: 'bacon' }, opts, function (err) {
Toy.updateOne({}, { color: 'bacon' }, opts, function (err) {
assert.equal(err.errors.color.message,
'Invalid color');
// acquit:ignore:start
Expand Down Expand Up @@ -442,7 +442,7 @@ describe('validation docs', function() {
var update = { color: 'red', name: 'Red Power Ranger' };
var opts = { runValidators: true };

Toy.update({}, update, opts, function(error) {
Toy.updateOne({}, update, opts, function(error) {
// The update validator throws an error:
// "TypeError: Cannot read property 'toLowerCase' of undefined",
// because `this` is **not** the document being updated when using
Expand Down Expand Up @@ -481,7 +481,7 @@ describe('validation docs', function() {
// Note the context option
var opts = { runValidators: true, context: 'query' };

Toy.update({}, update, opts, function(error) {
Toy.updateOne({}, update, opts, function(error) {
assert.ok(error.errors['color']);
// acquit:ignore:start
done();
Expand Down Expand Up @@ -512,15 +512,15 @@ describe('validation docs', function() {

var update = { color: 'blue' };
var opts = { runValidators: true };
Kitten.update({}, update, opts, function(err) {
Kitten.updateOne({}, update, opts, function(err) {
// Operation succeeds despite the fact that 'name' is not specified
// acquit:ignore:start
--outstanding || done();
// acquit:ignore:end
});

var unset = { $unset: { name: 1 } };
Kitten.update({}, unset, opts, function(err) {
Kitten.updateOne({}, unset, opts, function(err) {
// Operation fails because 'name' is required
assert.ok(err);
assert.ok(err.errors['name']);
Expand Down Expand Up @@ -565,10 +565,10 @@ describe('validation docs', function() {

var update = { $inc: { number: 1 } };
var opts = { runValidators: true };
Test.update({}, update, opts, function(error) {
Test.updateOne({}, update, opts, function(error) {
// There will never be a validation error here
update = { $push: [{ message: 'hello' }, { message: 'world' }] };
Test.update({}, update, opts, function(error) {
Test.updateOne({}, update, opts, function(error) {
// This will never error either even though the array will have at
// least 2 elements.
// acquit:ignore:start
Expand Down Expand Up @@ -600,7 +600,7 @@ describe('validation docs', function() {
}
};
var opts = { runValidators: true };
Test.update({}, update, opts, function(error) {
Test.updateOne({}, update, opts, function(error) {
assert.ok(error.errors['numbers']);
assert.ok(error.errors['docs']);
// acquit:ignore:start
Expand Down
8 changes: 4 additions & 4 deletions test/document.strict.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ describe('document: strict mode:', function() {
var doc = s.toObject();
doc.notInSchema = true;

Strict.collection.insert(doc, {w: 1}, function(err) {
Strict.collection.insertOne(doc, {w: 1}, function(err) {
assert.ifError(err);
Strict.findById(doc._id, function(err, doc) {
assert.ifError(err);
Expand Down Expand Up @@ -300,15 +300,15 @@ describe('document: strict mode:', function() {
var doc = s.toObject();
doc.notInSchema = true;

Strict.collection.insert(doc, function(err) {
Strict.collection.insertOne(doc, function(err) {
assert.ifError(err);

Strict.findById(doc._id, function(err, doc) {
assert.ifError(err);
assert.equal(doc._doc.bool, true);
assert.equal(doc._doc.notInSchema, true);

Strict.update({_id: doc._id}, {$unset: {bool: 1, notInSchema: 1}}, {strict: false},
Strict.updateOne({_id: doc._id}, {$unset: {bool: 1, notInSchema: 1}}, {strict: false},
function(err) {
assert.ifError(err);

Expand Down Expand Up @@ -338,7 +338,7 @@ describe('document: strict mode:', function() {
var doc = s.toObject();
doc.notInSchema = true;

Strict.collection.insert(doc, {w: 1}, function(err) {
Strict.collection.insertOne(doc, {w: 1}, function(err) {
assert.ifError(err);

Strict.findById(doc._id, function(err, doc) {
Expand Down
12 changes: 6 additions & 6 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ describe('document', function() {
var Task = db.model('gh4001', taskSchema);

var doc = { name: 'task1', title: 'task999' };
Task.collection.insert(doc, function(error) {
Task.collection.insertOne(doc, function(error) {
assert.ifError(error);
Task.findById(doc._id, function(error, doc) {
assert.ifError(error);
Expand Down Expand Up @@ -920,7 +920,7 @@ describe('document', function() {
});
});

describe('#update', function() {
describe.skip('#update', function() {
it('returns a Query', function(done) {
var mg = new mongoose.Mongoose;
var M = mg.model('doc#update', {s: String});
Expand Down Expand Up @@ -2138,13 +2138,13 @@ describe('document', function() {

var badUpdate = {$set: {'user.email': 'a'}};
var options = {runValidators: true};
Event.update({}, badUpdate, options, function(error) {
Event.updateOne({}, badUpdate, options, function(error) {
assert.ok(error);
assert.equal(error.errors['user.email'].kind, 'regexp');

var nestedUpdate = {name: 'test'};
var options = {upsert: true, setDefaultsOnInsert: true};
Event.update({}, nestedUpdate, options, function(error) {
Event.updateOne({}, nestedUpdate, options, function(error) {
assert.ifError(error);
Event.findOne({name: 'test'}, function(error, ev) {
assert.ifError(error);
Expand Down Expand Up @@ -5252,7 +5252,7 @@ describe('document', function() {

return co(function* () {
// use native driver directly to insert an empty doc
yield Test.collection.insert({});
yield Test.collection.insertOne({});

// udate the doc with the expectation that default booleans will be saved.
let found = yield Test.findOne({});
Expand Down Expand Up @@ -5283,7 +5283,7 @@ describe('document', function() {

return co(function* () {
// use native driver directly to kill the fields
yield Test.collection.insert({});
yield Test.collection.insertOne({});

// udate the doc with the expectation that default booleans will be saved.
let found = yield Test.findOne({});
Expand Down

0 comments on commit 2c2dbcd

Please sign in to comment.