Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add HAVING clause #6

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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