node-entity
Advanced tools
Comparing version 0.2.9 to 0.2.10
@@ -280,2 +280,3 @@ /** | ||
findMethod = this._checkEagerLoad(findMethod); | ||
findMethod = this._checkSorting(findMethod); | ||
@@ -335,2 +336,3 @@ return this._buildQuery(findMethod, fullQuery.selectors); | ||
}); | ||
return findMethod; | ||
@@ -356,1 +358,26 @@ }; | ||
}; | ||
/** | ||
* Checks for sorting and applies it. | ||
* | ||
* @param {mongoose.Query} query The query object. | ||
* @return {mongoose.Query} The query object. | ||
* @private | ||
*/ | ||
MongooseAdapter.prototype._checkSorting = function(query) { | ||
if (!this._hasSortBy) { | ||
return query; | ||
} | ||
this._sortBy.forEach(function(sort) { | ||
var obj = {}; | ||
var test = 1; | ||
if (sort[1] === 'DESC') { | ||
test = -1; | ||
} | ||
obj[sort[0]] = test; | ||
query.sort(obj); | ||
}); | ||
return query; | ||
}; |
@@ -93,2 +93,3 @@ /** | ||
findOpts = this._checkEagerLoad(findOpts); | ||
findOpts = this._checkSorting(findOpts); | ||
@@ -118,2 +119,3 @@ return Promise.cast(this.Model.findAll(findOpts)); | ||
findOpts = this._checkEagerLoad(findOpts); | ||
findOpts = this._checkSorting(findOpts); | ||
@@ -265,1 +267,18 @@ if (query) { | ||
}; | ||
/** | ||
* Checks for sorting and applies it. | ||
* | ||
* @param {sequelize.Query} query The query object. | ||
* @return {sequelize.Query} The query object. | ||
* @private | ||
*/ | ||
SequelizeAdaptor.prototype._checkSorting = function(query) { | ||
if (!this._hasSortBy) { | ||
return query; | ||
} | ||
query.order = this._sortBy; | ||
return query; | ||
}; |
@@ -32,2 +32,8 @@ /*jshint unused:false */ | ||
/** @type {Array.<Object>} Order by clauses */ | ||
this._sortBy = []; | ||
/** @type {boolean} Indicates if an order by exists */ | ||
this._hasSortBy = false; | ||
// Create primitive methods | ||
@@ -55,2 +61,3 @@ this.method('create', this._create.bind(this)); | ||
* @param {string|Array|Object} values stuff to eager load | ||
* @return {this} Chainable. | ||
*/ | ||
@@ -65,5 +72,24 @@ EntityCrud.prototype.eagerLoad = function(values) { | ||
} | ||
return this; | ||
}; | ||
/** | ||
* Apply sortening rules to fetching records. | ||
* | ||
* @param {string} attribute The attribute to sort by. | ||
* @param {string=} optDirection One of: 'DESC', 'ASC'. | ||
* @return {this} Chainable. | ||
*/ | ||
EntityCrud.prototype.sort = function(attribute, optDirection) { | ||
this._hasSortBy = true; | ||
var sort = [attribute]; | ||
if (optDirection) { | ||
sort.push(optDirection); | ||
} | ||
this._sortBy.push(sort); | ||
return this; | ||
}; | ||
/** | ||
* Create an entity item. | ||
@@ -70,0 +96,0 @@ * |
{ | ||
"name": "node-entity", | ||
"description": "The MVCe implementation for Node.js", | ||
"version": "0.2.9", | ||
"version": "0.2.10", | ||
"homepage": "https://github.com/thanpolas/entity", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -475,2 +475,4 @@ # Entity | ||
- **v0.2.10**, *04 Jul 2014* | ||
- Created sorting mechanism. | ||
- **v0.2.9**, *03 Jul 2014* | ||
@@ -477,0 +479,0 @@ - optimizations and fixes on eager loading for mongoose. |
@@ -9,2 +9,3 @@ /** | ||
name: 'Oh Long Johnson', | ||
sortby: 1, | ||
_isActive: true, | ||
@@ -15,3 +16,4 @@ }; | ||
name: 'once upon a time', | ||
sortby: 2, | ||
_isActive: false, | ||
}; |
@@ -17,2 +17,3 @@ /** | ||
name: {type: String, trim: true, required: true}, | ||
sortby: {type: Number}, | ||
_isActive: {type: Boolean, required: true, default: true}, | ||
@@ -19,0 +20,0 @@ }; |
@@ -18,2 +18,3 @@ /** | ||
name: {type: Sequelize.STRING, allowNull: false}, | ||
sortby: {type: Sequelize.INTEGER}, | ||
_isActive: {type: Sequelize.BOOLEAN, allowNull: false, defaultValue: true}, | ||
@@ -20,0 +21,0 @@ }; |
@@ -23,11 +23,12 @@ /** | ||
suite(majNum + '.5 Read records', function() { | ||
var ent, id; | ||
setup(function(done) { | ||
ent = adaptor.factory(); | ||
ent.create(fix.one).then(function(obj) { | ||
id = obj.id; | ||
ent.create(fix.two).then(done.bind(null, null), done); | ||
}).then(null, done); | ||
}); | ||
suite(majNum + '.5.2 using ', function() { | ||
var ent, id; | ||
setup(function(done) { | ||
ent = adaptor.factory(); | ||
ent.create(fix.one).then(function(obj) { | ||
id = obj.id; | ||
ent.create(fix.two).then(done.bind(null, null), done); | ||
}).then(null, done); | ||
}); | ||
test(majNum + '.5.2.1 Read one record using the id', function(done) { | ||
@@ -56,3 +57,37 @@ ent.readOne(id).then(function(res) { | ||
}); | ||
suite(majNum + '.5.3 sorting ', function() { | ||
setup(function(done) { | ||
this.ent = adaptor.factory(); | ||
var self = this; | ||
this.ent.create(fix.one).then(function(obj) { | ||
self.id = obj.id; | ||
self.ent.create(fix.two).then(done.bind(null, null), done); | ||
}).then(null, done); | ||
}); | ||
test(majNum + '.5.3.1 Default sorting is ASC', function(done) { | ||
this.ent.read().then(function(res) { | ||
assert.equal(res[0].sortby, 1, 'Default sorting is ASC'); | ||
}).then(done, done); | ||
}); | ||
test(majNum + '.5.3.2 Defining sort, default is ASC', function(done) { | ||
this.ent.sort('sortby'); | ||
this.ent.read().then(function(res) { | ||
assert.equal(res[0].sortby, 1, 'Default sorting is ASC'); | ||
}).then(done, done); | ||
}); | ||
test(majNum + '.5.3.3 Defining ASC sort works', function(done) { | ||
this.ent.sort('sortby', 'ASC'); | ||
this.ent.read().then(function(res) { | ||
assert.equal(res[0].sortby, 1, 'ASC sorting works'); | ||
}).then(done, done); | ||
}); | ||
test(majNum + '.5.3.4 Defining DESC sort works', function(done) { | ||
this.ent.sort('sortby', 'DESC'); | ||
this.ent.read().then(function(res) { | ||
assert.equal(res[0].sortby, 2, 'DESC sorting works'); | ||
}).then(done, done); | ||
}); | ||
}); | ||
}); | ||
}; |
@@ -50,3 +50,3 @@ /** | ||
test(majNum + '.9.10.1 Expect specific number of keys', function() { | ||
assert.lengthOf(Object.keys(ent.getSchema()), 4, 'Mongoose schema items'); | ||
assert.lengthOf(Object.keys(ent.getSchema()), 5, 'Mongoose schema items'); | ||
}); | ||
@@ -63,3 +63,3 @@ test(majNum + '.9.10.2 Expect special keys to cannot show', function() { | ||
test(majNum + '.9.11.1 Expect specific number of keys', function() { | ||
assert.lengthOf(Object.keys(ent.getSchema()), 5); | ||
assert.lengthOf(Object.keys(ent.getSchema()), 6); | ||
}); | ||
@@ -66,0 +66,0 @@ test(majNum + '.9.11.2 Expect special keys to cannot show', function() { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
84550
2019
520