Skip to content

Commit

Permalink
feat: add HAVING clause
Browse files Browse the repository at this point in the history
  • Loading branch information
megha-tnt authored Jul 5, 2024
1 parent aac5b8d commit 6ab76f6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/lib/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function buildSelectQuery(
select: "",
where: query.where,
groupBy: query.groupBy,
having: query.having,
orderBy: query.orderBy,
limit: query.limit,
offset: query.offset,
Expand Down Expand Up @@ -213,6 +214,9 @@ export function sql(
if (params.groupBy && params.groupBy.length > 0) {
sql += ` GROUP BY ${group(params.groupBy)}`;
}
if (params.having && Object.keys(params.having).length > 0) {
sql += ` HAVING ${where(params.having, "AND", params.relations || {})}`;
}
if (params.orderBy && Object.keys(params.orderBy).length > 0) {
sql += ` ORDER BY ${order(params.orderBy)}`;
}
Expand Down
7 changes: 7 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export type IncludeQuery = {
offset?: number;
include?: Include;
groupBy?: string[];
having?: QueryHavingCondition;
orderBy?: OrderBy;
leftJoin?: Join;
rightJoin?: Join;
Expand All @@ -95,6 +96,7 @@ export type SelectQuery = {
offset?: number;
include?: Include;
groupBy?: string[];
having?: QueryHavingCondition;
orderBy?: OrderBy;
leftJoin?: Join;
rightJoin?: Join;
Expand Down Expand Up @@ -135,6 +137,10 @@ export type NestedWhereCondition = {

export type QueryWhereCondition = WhereCondition | NestedWhereCondition;

export type QueryHavingCondition =
| WhereCondition
| Omit<NestedWhereCondition, "exists">;

export type SelectType = "simple" | "aggregated" | "object";

export type SqlParams = {
Expand All @@ -146,6 +152,7 @@ export type SqlParams = {
limit?: number;
offset?: number;
groupBy?: GroupBy;
having?: QueryHavingCondition;
orderBy?: OrderBy;
relations?: Relations;
returning?: Array<string>;
Expand Down
5 changes: 4 additions & 1 deletion src/test/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ describe("sql", () => {
join: " JOIN other_table ON table.id = other_table.table_id",
where: { id: 1 },
groupBy: ["name"],
having: {
'COUNT("name")': 1,
},
orderBy: { name: "ASC" },
limit: 10,
offset: 5,
Expand All @@ -92,7 +95,7 @@ describe("sql", () => {
};
const result = sql(params, append);
expect(result).toBe(
'SELECT * FROM table JOIN other_table ON table.id = other_table.table_id JOIN another_table ON table.id = another_table.table_id WHERE "id" = 1 AND other_table.name = "test" GROUP BY "name" ORDER BY "name" ASC LIMIT 10 OFFSET 5 RETURNING "id", "name"',
`SELECT * FROM table JOIN other_table ON table.id = other_table.table_id JOIN another_table ON table.id = another_table.table_id WHERE "id" = 1 AND other_table.name = "test" GROUP BY "name" HAVING COUNT("name") = 1 ORDER BY "name" ASC LIMIT 10 OFFSET 5 RETURNING "id", "name"`,
);
});
});
Expand Down

0 comments on commit 6ab76f6

Please sign in to comment.