Skip to content

Commit d2d4e1b

Browse files
committed
allow multiple joins - closes brianc#15
1 parent d535c67 commit d2d4e1b

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

lib/node/join.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
module.exports = require(__dirname).define({
1+
var JoinNode = module.exports = require(__dirname).define({
22
type: 'JOIN',
33
constructor: function(subType, from, to) {
44
this.subType = subType;
5-
this.from = from;
6-
this.to = to;
5+
this.from = from.toNode();
6+
this.to = to.toNode();
77
},
88
on: function(node) {
99
this.on = node;
1010
return this;
11+
},
12+
join: function(other) {
13+
return new JoinNode('INNER', this, other);
1114
}
1215
});

lib/table.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Table.prototype.toNode = function() {
7777
}
7878

7979
Table.prototype.join = function(other) {
80-
return new JoinNode('INNER', this.toNode(), other.toNode());
80+
return new JoinNode('INNER', this.toNode(), other.toNode(), other);
8181
}
8282

8383
Table.prototype.as = function(alias) {

test/dialect-tests.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,23 @@ test({
9292
pg : 'SELECT "user"."name", "post"."content" FROM "user" INNER JOIN "post" ON ("user"."id" = "post"."userId")'
9393
});
9494

95+
var comment = Table.define({
96+
name: 'comment',
97+
columns: ['postId', 'text']
98+
});
99+
100+
test({
101+
query : user
102+
.select(user.name, post.content, comment.text)
103+
.from(
104+
user
105+
.join(post).on(user.id.equals(post.userId))
106+
.join(comment).on(post.id.equals(comment.postId))
107+
),
108+
pg : 'SELECT "user"."name", "post"."content", "comment"."text" FROM "user" INNER JOIN "post" ON ("user"."id" = "post"."userId")' +
109+
' INNER JOIN "comment" ON ("post"."id" = "comment"."postId")'
110+
});
111+
95112
test({
96113
query : user.select(user.name, post.content).from(user.join(post).on(user.id.equals(post.userId))),
97114
pg : 'SELECT "user"."name", "post"."content" FROM "user" INNER JOIN "post" ON ("user"."id" = "post"."userId")'

0 commit comments

Comments
 (0)