Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add sorting on nested associations

  • Loading branch information...
commit 914e345db72b28b6468b1b53048450e76d095910 1 parent de290c3
@ben-ng ben-ng authored
View
1  lib/adapters/transformers/sql.js
@@ -36,7 +36,6 @@ var sql = utils.mixin(new BaseTransformer(), new (function () {
, rootKey = Object.keys(meta.dependencyTree)[0];
for (var p in sort) {
- // If there are dots, it might be nested
if (p.indexOf('.') > -1) {
names = p.split('.');
propertyName = this._columnizePropertyName(names.pop());
View
63 lib/query/query.js
@@ -58,39 +58,46 @@ Query.prototype = new (function () {
, updatedAt: true
};
return function (k) {
- var key = k
+ var keyName = k
+ , keyNameArr
, modelName
- , assumedModelName
- , propertyName
- , assn
- , reg;
+ , reg
+ , assnTokens = [];
// Users.loginId, Teams.name
- // Sort on association property
- if (key.indexOf('.') > -1) {
- key = key.split('.');
- modelName = key[0];
- assumedModelName = key[0];
- propertyName = key[1];
- // Make sure it's an association
- if (modelName != self.model.modelName) {
- assn = model.getAssociation(self.model.modelName, modelName);
- modelName = assn.model;
+ // Sort on nested association property
+ modelName = self.model.modelName
+
+ // Walks through the associations and updates modelName
+ // until we reach the property name
+ if (keyName.indexOf('.') > -1) {
+ keyNameArr = keyName.split('.');
+
+ while(keyNameArr.length > 1) {
+ var tempAssnName = keyNameArr.shift()
+ , assn = model.getAssociation(modelName, tempAssnName)
+
+ assnTokens.push(tempAssnName)
+
+ if(!assn) {
+ throw new Error('The association "' + tempAssnName + '" is not a valid ' +
+ 'property on the ' + modelName + ' model.');
+ }
+ else {
+ modelName = assn.model
+ }
}
+
+ keyName = keyNameArr[0]
}
- // loginId, name
- // Sort on a prop on the main model for this query
- else {
- modelName = self.model.modelName;
- assumedModelName = modelName
- propertyName = key;
- }
+
reg = model.descriptionRegistry[modelName].properties;
- if (baseProps[propertyName] || reg[propertyName]) {
+ if (baseProps[keyName] || reg[keyName]) {
return {
- modelName: assumedModelName
- , propertyName: propertyName
+ modelName: modelName
+ , propertyName: keyName
+ , assnTokens: assnTokens
};
}
else {
@@ -329,9 +336,9 @@ Query.prototype = new (function () {
' field on ' + validatedField.modelName + ' must be ' +
'either "asc" or "desc"');
}
- if (p.indexOf('.') > -1) {
- validated[validatedField.modelName + '.' +
- validatedField.propertyName] = val;
+ if (validatedField.assnTokens.length) {
+ var temp = validatedField.assnTokens.concat(validatedField.propertyName).join('.');
+ validated[temp] = val;
}
else {
validated[p] = val;
View
17 test/integration/adapters/sql/nested_eager_assn.js
@@ -46,10 +46,7 @@ tests = {
, c = data[2]
, d = data[3]
, e = data[4]
- , f = data[5]
- , byTitleComparator = function (l, r) {
- return l.title < r.title ? -1 : 1;
- };
+ , f = data[5];
// a -> b, c
a.addFriend(b);
@@ -70,6 +67,7 @@ tests = {
sort: {
title: 'asc'
, 'friends.title': 'asc'
+ , 'friends.friends.title': 'asc'
}
, includes: {friends: 'friends'}}
, function (err, data) {
@@ -85,8 +83,7 @@ tests = {
// a -> c -> e, f
assert.equal(data[0].friends[1].friends.length, 2);
- // FIXME: Need to be able to sort on nested assocs
- data[0].friends[1].friends.sort(byTitleComparator);
+
assert.equal(data[0].friends[1].friends[0].id, e.id);
assert.equal(data[0].friends[1].friends[1].id, f.id);
@@ -158,10 +155,7 @@ tests = {
, c = data[2]
, d = data[3]
, e = data[4]
- , f = data[5]
- , byTitleComparator = function (l, r) {
- return l.title < r.title ? -1 : 1;
- };
+ , f = data[5];
// a -> b, c
a.addFriend(b);
@@ -178,6 +172,7 @@ tests = {
sort: {
title: 'asc'
, 'friends.title': 'asc'
+ , 'friends.friends.title': 'asc'
}
// "Get friends of friends"
, includes: {friends: 'friends'}}
@@ -194,8 +189,6 @@ tests = {
// a -> c -> e, f
assert.equal(data[0].friends[1].friends.length, 2);
- // FIXME: Need to be able to sort on nested assocs
- data[0].friends[1].friends.sort(byTitleComparator);
assert.equal(data[0].friends[1].friends[0].id, e.id);
assert.equal(data[0].friends[1].friends[1].id, f.id);
Please sign in to comment.
Something went wrong with that request. Please try again.