Skip to content

Commit 55de0f0

Browse files
committed
feat: add ManyToManyJoin interface and update QueryRowsPayload to support it
1 parent 0806695 commit 55de0f0

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/types/query.ts

+11
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,20 @@ export interface Join<T, F> {
4545
columns?: Array<keyof F>;
4646
}
4747

48+
export interface ManyToManyJoin<Pivot,Foreign> {
49+
table: string;
50+
as?: string;
51+
on: {
52+
localField: keyof Pivot;
53+
foreignField: keyof Pivot;
54+
};
55+
columns?: Array<keyof Foreign>;
56+
}
57+
4858
export interface QueryRowsPayload<T> {
4959
where?: WhereCondition<T>;
5060
joins?: Join<T, any>[];
61+
with?: ManyToManyJoin<any, any>[];
5162
orderBy?: Array<OrderBy<T>>;
5263
columns?: Array<keyof T>;
5364
limit?: number;

src/utils/clauses.ts

+43-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Join, OrderBy, SimpleWhere, WhereCondition } from "../types";
1+
import {Join, ManyToManyJoin, OrderBy, SimpleWhere, WhereCondition} from "../types";
22
import { toSnakeCase } from "./formatting";
33

44
/**
@@ -181,6 +181,48 @@ export const buildJoinClause = <T>(
181181
};
182182
};
183183

184+
185+
/**
186+
* Builds JOIN clauses for SQL queries
187+
*/
188+
// export const buildJoinManyToManyClause = <T>(
189+
// joins?: Array<ManyToManyJoin<any, any>>,
190+
// baseTableName?: string
191+
// ): { joinConditionClause: string; joinSelectClause: string[] } => {
192+
// if (!joins || joins.length === 0) {
193+
// return {
194+
// joinConditionClause: "",
195+
// joinSelectClause: []
196+
// };
197+
// }
198+
199+
// const joinConditions = joins.map((join) => {
200+
// const joinType = join.type.toUpperCase();
201+
// const alias = join.as || join.table;
202+
// const foreignField = join.on.foreignField as any;
203+
// const localField = join.on.localField as any;
204+
// return `${joinType} JOIN "${join.table}" AS "${alias}" ON "${alias}"."${foreignField}" = "${baseTableName}"."${localField}"`;
205+
// });
206+
//
207+
// const joinSelectClause = joins
208+
// .map((join) => {
209+
// if (!join.columns || join.columns.length === 0) return "";
210+
//
211+
// const alias = join.as || join.table;
212+
// const jsonColumns = join.columns
213+
// .map((col) => `'${col.toString()}', "${alias}"."${col.toString()}"`)
214+
// .join(", ");
215+
//
216+
// return `json_build_object(${jsonColumns}) AS ${alias}`;
217+
// })
218+
// .filter(Boolean);
219+
//
220+
// return {
221+
// joinConditionClause: joinConditions.join(" "),
222+
// joinSelectClause
223+
// };
224+
// };
225+
184226
/**
185227
* Builds a SET clause for UPDATE SQL statements with proper type handling
186228
*/

0 commit comments

Comments
 (0)