Skip to content

Commit 641d836

Browse files
committed
Merge pull request #30 from js-data/develop
Allow qualified local columns names if joined relation has same name
2 parents df50955 + 0eca2c7 commit 641d836

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,18 @@ function filterQuery (resourceConfig, params) {
6464

6565
if (!joinedTables.some(t => t === relationPath.join('.'))) {
6666
let [relation] = localResourceConfig.relationList.filter(r => r.relation === relationName)
67-
let table = getTable(localResourceConfig)
68-
let localId = `${table}.${relation.localKey}`
67+
if (relation) {
68+
let table = getTable(localResourceConfig)
69+
let localId = `${table}.${relation.localKey}`
6970

70-
let relationTable = getTable(relationResourceConfig)
71-
let foreignId = `${relationTable}.${relationResourceConfig.idAttribute}`
71+
let relationTable = getTable(relationResourceConfig)
72+
let foreignId = `${relationTable}.${relationResourceConfig.idAttribute}`
7273

73-
query = query.join(relationTable, localId, foreignId)
74-
joinedTables.push(relationPath.join('.'))
74+
query = query.join(relationTable, localId, foreignId)
75+
joinedTables.push(relationPath.join('.'))
76+
} else {
77+
// local column
78+
}
7579
}
7680
localResourceConfig = relationResourceConfig
7781
}

test/findAll.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,14 @@ describe('DSSqlAdapter#findAll', function () {
182182
assert.isUndefined(users[0].email);
183183
});
184184

185+
it('should filter when relations have same column if column is qualified', function* () {
186+
var profile1 = yield adapter.create(Profile, { email: '[email protected]' });
187+
var user1 = yield adapter.create(User, {name: 'John', profileId: profile1.id});
188+
189+
// `id` column must be qualified with `user.`
190+
var users = yield adapter.findAll(User, {'user.id': user1.id, 'profile.email': '[email protected]'});
191+
assert.equal(users.length, 1);
192+
assert.equal(users[0].profileId, profile1.id);
193+
});
194+
185195
});

0 commit comments

Comments
 (0)