Skip to content

Commit 94e47e3

Browse files
committedJul 31, 2018
Dedicated Model for testing disableDefaultSort
1 parent fe6ae0e commit 94e47e3

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed
 

‎lib/mongodb.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -942,13 +942,15 @@ MongoDB.prototype.buildSort = function(model, order, options) {
942942

943943
var modelClass = this._models[model];
944944

945-
var disableDefaultSort = this.settings.disableDefaultSort;
945+
var disableDefaultSort = false;
946+
if (this.settings.hasOwnProperty('disableDefaultSort')) {
947+
disableDefaultSort = this.settings.disableDefaultSort;
948+
}
949+
if (modelClass.settings.hasOwnProperty('disableDefaultSort')) {
950+
disableDefaultSort = modelClass.settings.disableDefaultSort;
951+
}
946952
if (options && options.hasOwnProperty('disableDefaultSort')) {
947-
disableDefaultSort = options.disableDefaultSort === true;
948-
} else if (disableDefaultSort !== false && modelClass.settings.hasOwnProperty('disableDefaultSort')) {
949-
disableDefaultSort = modelClass.settings.disableDefaultSort === true;
950-
} else if (disableDefaultSort === true) {
951-
disableDefaultSort = true;
953+
disableDefaultSort = options.disableDefaultSort;
952954
}
953955

954956
if (!order && !disableDefaultSort) {

‎test/mongodb.test.js

+22-9
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ var Superhero,
2626
Category,
2727
UserWithRenamedColumns,
2828
PostWithStringIdAndRenamedColumns,
29-
Employee;
29+
Employee,
30+
PostWithDisableDefaultSort;
3031

3132
describe('lazyConnect', function() {
3233
it('should skip connect phase (lazyConnect = true)', function(done) {
@@ -273,6 +274,18 @@ describe('mongodb connector', function() {
273274
}
274275
);
275276

277+
PostWithDisableDefaultSort = db.define(
278+
'PostWithDisableDefaultSort',
279+
{
280+
id: {type: String, id: true},
281+
title: {type: String, length: 255, index: true},
282+
content: {type: String},
283+
},
284+
{
285+
disableDefaultSort: true,
286+
}
287+
);
288+
276289
User.hasMany(Post);
277290
Post.belongsTo(User);
278291
});
@@ -285,7 +298,9 @@ describe('mongodb connector', function() {
285298
PostWithNumberId.destroyAll(function() {
286299
PostWithNumberUnderscoreId.destroyAll(function() {
287300
PostWithStringId.destroyAll(function() {
288-
done();
301+
PostWithDisableDefaultSort.destroyAll(function() {
302+
done();
303+
});
289304
});
290305
});
291306
});
@@ -2492,21 +2507,19 @@ describe('mongodb connector', function() {
24922507

24932508
it('find should not order by id if the order is not set for the query filter and settings.disableDefaultSort is true',
24942509
function(done) {
2495-
PostWithStringId.settings.disableDefaultSort = true;
2496-
2497-
PostWithStringId.create({ id: '2', title: 'c', content: 'CCC' }, function(err, post) {
2498-
PostWithStringId.create({ id: '1', title: 'd', content: 'DDD' }, function(err, post) {
2499-
PostWithStringId.find({}, function(err, posts) {
2510+
PostWithDisableDefaultSort.create({id: '2', title: 'c', content: 'CCC'}, function(err, post) {
2511+
PostWithDisableDefaultSort.create({id: '1', title: 'd', content: 'DDD'}, function(err, post) {
2512+
PostWithDisableDefaultSort.find({}, function(err, posts) {
25002513
should.not.exist(err);
25012514
posts.length.should.be.equal(2);
25022515
posts[0].id.should.be.equal('2');
25032516

2504-
PostWithStringId.find({ limit: 1, offset: 0 }, function(err, posts) {
2517+
PostWithDisableDefaultSort.find({limit: 1, offset: 0}, function(err, posts) {
25052518
should.not.exist(err);
25062519
posts.length.should.be.equal(1);
25072520
posts[0].id.should.be.equal('2');
25082521

2509-
PostWithStringId.find({ limit: 1, offset: 1 }, function(err, posts) {
2522+
PostWithDisableDefaultSort.find({limit: 1, offset: 1}, function(err, posts) {
25102523
should.not.exist(err);
25112524
posts.length.should.be.equal(1);
25122525
posts[0].id.should.be.equal('1');

0 commit comments

Comments
 (0)
Please sign in to comment.