Skip to content

Commit 9f57dd6

Browse files
committed
checkpoint
1 parent 57ec777 commit 9f57dd6

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

packages/db/src/query/builder/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type {
2424
GroupByCallback,
2525
JoinOnCallback,
2626
MergeContext,
27+
MergeContextForJoinCallback,
2728
MergeContextWithJoinType,
2829
OrderByCallback,
2930
OrderByOptions,
@@ -142,7 +143,7 @@ export class BaseQueryBuilder<TContext extends Context = Context> {
142143
>(
143144
source: TSource,
144145
onCallback: JoinOnCallback<
145-
MergeContext<TContext, SchemaFromSource<TSource>>
146+
MergeContextForJoinCallback<TContext, SchemaFromSource<TSource>>
146147
>,
147148
type: TJoinType = `left` as TJoinType
148149
): QueryBuilder<
@@ -154,7 +155,7 @@ export class BaseQueryBuilder<TContext extends Context = Context> {
154155
const currentAliases = this._getCurrentAliases()
155156
const newAliases = [...currentAliases, alias]
156157
const refProxy = createRefProxy(newAliases) as RefProxyForContext<
157-
MergeContext<TContext, SchemaFromSource<TSource>>
158+
MergeContextForJoinCallback<TContext, SchemaFromSource<TSource>>
158159
>
159160

160161
// Get the join condition expression
@@ -209,7 +210,7 @@ export class BaseQueryBuilder<TContext extends Context = Context> {
209210
leftJoin<TSource extends Source>(
210211
source: TSource,
211212
onCallback: JoinOnCallback<
212-
MergeContext<TContext, SchemaFromSource<TSource>>
213+
MergeContextForJoinCallback<TContext, SchemaFromSource<TSource>>
213214
>
214215
): QueryBuilder<
215216
MergeContextWithJoinType<TContext, SchemaFromSource<TSource>, `left`>
@@ -235,7 +236,7 @@ export class BaseQueryBuilder<TContext extends Context = Context> {
235236
rightJoin<TSource extends Source>(
236237
source: TSource,
237238
onCallback: JoinOnCallback<
238-
MergeContext<TContext, SchemaFromSource<TSource>>
239+
MergeContextForJoinCallback<TContext, SchemaFromSource<TSource>>
239240
>
240241
): QueryBuilder<
241242
MergeContextWithJoinType<TContext, SchemaFromSource<TSource>, `right`>
@@ -261,7 +262,7 @@ export class BaseQueryBuilder<TContext extends Context = Context> {
261262
innerJoin<TSource extends Source>(
262263
source: TSource,
263264
onCallback: JoinOnCallback<
264-
MergeContext<TContext, SchemaFromSource<TSource>>
265+
MergeContextForJoinCallback<TContext, SchemaFromSource<TSource>>
265266
>
266267
): QueryBuilder<
267268
MergeContextWithJoinType<TContext, SchemaFromSource<TSource>, `inner`>
@@ -287,7 +288,7 @@ export class BaseQueryBuilder<TContext extends Context = Context> {
287288
fullJoin<TSource extends Source>(
288289
source: TSource,
289290
onCallback: JoinOnCallback<
290-
MergeContext<TContext, SchemaFromSource<TSource>>
291+
MergeContextForJoinCallback<TContext, SchemaFromSource<TSource>>
291292
>
292293
): QueryBuilder<
293294
MergeContextWithJoinType<TContext, SchemaFromSource<TSource>, `full`>

packages/db/src/query/builder/types.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,22 @@ export type MergeContext<
323323
TNewSchema extends ContextSchema,
324324
> = MergeContextWithJoinType<TContext, TNewSchema, `left`>
325325

326+
// Type for join callbacks that doesn't apply optionality - both tables are available
327+
export type MergeContextForJoinCallback<
328+
TContext extends Context,
329+
TNewSchema extends ContextSchema,
330+
> = {
331+
baseSchema: TContext[`baseSchema`]
332+
// Merge schemas without applying join optionality - both are non-optional in join condition
333+
schema: TContext[`schema`] & TNewSchema
334+
fromSourceName: TContext[`fromSourceName`]
335+
hasJoins: true
336+
joinTypes: TContext[`joinTypes`] extends Record<string, any>
337+
? TContext[`joinTypes`]
338+
: {}
339+
result: TContext[`result`]
340+
}
341+
326342
// Helper type for updating context with result type
327343
export type WithResult<TContext extends Context, TResult> = Prettify<
328344
Omit<TContext, `result`> & {

packages/db/tests/query/builder/functional-variants.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ describe(`QueryBuilder functional variants (fn)`, () => {
221221
({ employees, departments }) =>
222222
eq(employees.department_id, departments.id)
223223
)
224-
.groupBy(({ departments }) => departments.name)
224+
.groupBy(({ departments }) => departments?.name)
225225
.fn.having(
226226
(row) =>
227227
row.employees.salary > 60000 &&
@@ -245,7 +245,7 @@ describe(`QueryBuilder functional variants (fn)`, () => {
245245
)
246246
.fn.where((row) => row.employees.active)
247247
.fn.where((row) => row.employees.salary > 40000)
248-
.groupBy(({ departments }) => departments.name)
248+
.groupBy(({ departments }) => departments?.name)
249249
.fn.having((row) => row.employees.salary > 70000)
250250
.fn.select((row) => ({
251251
departmentName: row.departments?.name || `Unknown`,

packages/db/tests/query/builder/join.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe(`QueryBuilder.join`, () => {
7575
eq(employees.department_id, departments.id)
7676
)
7777
.join({ projects: projectsCollection }, ({ departments, projects }) =>
78-
eq(departments.id, projects.department_id)
78+
eq(departments?.id, projects?.department_id)
7979
)
8080

8181
const builtQuery = getQueryIR(query)
@@ -360,7 +360,7 @@ describe(`QueryBuilder.join`, () => {
360360
.innerJoin(
361361
{ projects: projectsCollection },
362362
({ departments, projects }) =>
363-
eq(departments.id, projects.department_id)
363+
eq(departments?.id, projects.department_id)
364364
)
365365

366366
const builtQuery = getQueryIR(query)

0 commit comments

Comments
 (0)