Skip to content

Commit

Permalink
Add cross join type
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubknejzlik committed Dec 16, 2024
1 parent 421b22d commit 94bf4a8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
12 changes: 12 additions & 0 deletions src/Query-serialization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ describe("Query builder JSON Serialization/Deserialization", () => {
.leftJoin(
Q.table("anotherTable", "AAA"),
Conditions.columnEqual("table.foo", "anotherTable.bar")
)
.rightJoin(
Q.table("anotherTable", "AAA"),
Conditions.columnEqual("table.foo", "anotherTable.bar")
)
.fullJoin(
Q.table("anotherTable", "AAA"),
Conditions.columnEqual("table.foo", "anotherTable.bar")
)
.crossJoin(
Q.table("anotherTable", "AAA"),
Conditions.columnEqual("table.foo", "anotherTable.bar")
);
const jsonStr = JSON.stringify(originalQuery.toJSON());
const deserializedQuery = Q.deserialize(jsonStr);
Expand Down
26 changes: 26 additions & 0 deletions src/Query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,32 @@ describe("Query builder SQL", () => {
"SELECT * FROM `table` LEFT JOIN `otherTable` AS `aliasOtherTable` ON `table`.`foo` = `aliasOtherTable`.`bar`"
);
});
it("should handle FULL JOIN", () => {
expect(
Query.select()
.from("table")
.fullJoin(
Q.table("otherTable", "aliasOtherTable"),
Cond.columnEqual("table.foo", "aliasOtherTable.bar")
)
.toSQL(flavor)
).toEqual(
"SELECT * FROM `table` FULL JOIN `otherTable` AS `aliasOtherTable` ON `table`.`foo` = `aliasOtherTable`.`bar`"
);
});
it("should handle CROSS JOIN", () => {
expect(
Query.select()
.from("table")
.crossJoin(
Q.table("otherTable", "aliasOtherTable"),
Cond.columnEqual("table.foo", "aliasOtherTable.bar")
)
.toSQL(flavor)
).toEqual(
"SELECT * FROM `table` CROSS JOIN `otherTable` AS `aliasOtherTable` ON `table`.`foo` = `aliasOtherTable`.`bar`"
);
});
it("should handle multiple JOINS", () => {
expect(
Query.select()
Expand Down
19 changes: 8 additions & 11 deletions src/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,7 @@ export class QueryBase implements ISequelizable, IMetadata {
/**
* join function to join tables with all join types
*/
join(
table: Table,
condition?: Condition,
type: "INNER" | "LEFT" | "RIGHT" | "FULL" = "INNER"
): this {
join(table: Table, condition?: Condition, type: JoinType = "INNER"): this {
const clone = this.clone();
clone._joins.push(new Join(table, condition, type));
return clone;
Expand All @@ -141,6 +137,9 @@ export class QueryBase implements ISequelizable, IMetadata {
fullJoin(table: Table, condition: Condition): this {
return this.join(table, condition, "FULL");
}
crossJoin(table: Table, condition: Condition): this {
return this.join(table, condition, "CROSS");
}

public clone(): this {
const clone = new (this.constructor as any)();
Expand All @@ -156,16 +155,14 @@ export class QueryBase implements ISequelizable, IMetadata {
}
}

type JoinType = "INNER" | "LEFT" | "RIGHT" | "FULL" | "CROSS";

class Join {
protected _type: "INNER" | "LEFT" | "RIGHT" | "FULL";
protected _type: JoinType;
protected _table: Table;
protected _condition?: Condition;

constructor(
table: Table,
condition?: Condition,
type: "INNER" | "LEFT" | "RIGHT" | "FULL" = "INNER"
) {
constructor(table: Table, condition?: Condition, type: JoinType = "INNER") {
this._table = table;
this._condition = condition;
this._type = type;
Expand Down
11 changes: 11 additions & 0 deletions src/stories/1_Query.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ Q.select()
);
`}
/>
### Cross Join

A cross join returns the Cartesian product of the two tables, combining each row from the first table with every row from the second table:

<QueryPreview
code={`
Q.select()
.from('users')
.crossJoin(Q.table('orders', 'o'));
`}
/>

### Join Conditions

Expand Down

0 comments on commit 94bf4a8

Please sign in to comment.