Skip to content

Commit 338efc2

Browse files
authored
make select optional on a query (#148)
1 parent ee5d026 commit 338efc2

5 files changed

Lines changed: 363 additions & 10 deletions

File tree

.changeset/lemon-deer-tell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tanstack/db": patch
3+
---
4+
5+
the select operator is not optional on a query, it will default to returning the whole row for a basic query, and a namespaced object when there are joins

packages/db/src/query/pipeline-compiler.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import { processOrderBy } from "./order-by.js"
66
import { processSelect } from "./select.js"
77
import type { Query } from "./schema.js"
88
import type { IStreamBuilder } from "@electric-sql/d2ts"
9-
import type { KeyedStream, NamespacedAndKeyedStream } from "../types.js"
9+
import type {
10+
InputRow,
11+
KeyedStream,
12+
NamespacedAndKeyedStream,
13+
} from "../types.js"
1014

1115
/**
1216
* Compiles a query into a D2 pipeline
@@ -136,7 +140,10 @@ export function compileQueryPipeline<T extends IStreamBuilder<unknown>>(
136140
// Process the SELECT clause - this is where we flatten the structure
137141
const resultPipeline: KeyedStream | NamespacedAndKeyedStream = query.select
138142
? processSelect(pipeline, query, mainTableAlias, allInputs)
139-
: pipeline
140-
143+
: !query.join && !query.groupBy
144+
? pipeline.pipe(
145+
map(([key, row]) => [key, row[mainTableAlias]] as InputRow)
146+
)
147+
: pipeline
141148
return resultPipeline as T
142149
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ export class BaseQueryBuilder<TContext extends Context<Schema>> {
438438
Input
439439
>
440440
}
441+
hasJoin: true
441442
}
442443
>
443444
>
@@ -474,6 +475,7 @@ export class BaseQueryBuilder<TContext extends Context<Schema>> {
474475
schema: TContext[`schema`] & {
475476
[K in T]: RemoveIndexSignature<TContext[`baseSchema`][T]>
476477
}
478+
hasJoin: true
477479
}
478480
>
479481
>
@@ -513,6 +515,7 @@ export class BaseQueryBuilder<TContext extends Context<Schema>> {
513515
schema: TContext[`schema`] & {
514516
[K in TAs]: RemoveIndexSignature<TContext[`baseSchema`][TFrom]>
515517
}
518+
hasJoin: true
516519
}
517520
>
518521
>
@@ -864,10 +867,12 @@ export function queryBuilder<TBaseSchema extends Schema = {}>() {
864867

865868
export type ResultsFromContext<TContext extends Context<Schema>> = Flatten<
866869
TContext[`result`] extends object
867-
? TContext[`result`]
868-
: TContext[`result`] extends undefined
869-
? TContext[`schema`]
870-
: object
870+
? TContext[`result`] // If there is a select we will have a result type
871+
: TContext[`hasJoin`] extends true
872+
? TContext[`schema`] // If there is a join, the query returns the namespaced schema
873+
: TContext[`default`] extends keyof TContext[`schema`]
874+
? TContext[`schema`][TContext[`default`]] // If there is no join we return the flat default schema
875+
: never // Should never happen
871876
>
872877

873878
export type ResultFromQueryBuilder<TQueryBuilder> = Flatten<

packages/db/src/query/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export type Context<
2020
schema: TSchema
2121
default?: keyof TSchema
2222
result?: Record<string, unknown>
23+
hasJoin?: boolean
2324
}
2425

2526
// Helper types

0 commit comments

Comments
 (0)