Skip to content
Merged
5 changes: 5 additions & 0 deletions .changeset/lemon-deer-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tanstack/db": patch
---

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
13 changes: 10 additions & 3 deletions packages/db/src/query/pipeline-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { processOrderBy } from "./order-by.js"
import { processSelect } from "./select.js"
import type { Query } from "./schema.js"
import type { IStreamBuilder } from "@electric-sql/d2ts"
import type { KeyedStream, NamespacedAndKeyedStream } from "../types.js"
import type {
InputRow,
KeyedStream,
NamespacedAndKeyedStream,
} from "../types.js"

/**
* Compiles a query into a D2 pipeline
Expand Down Expand Up @@ -136,7 +140,10 @@ export function compileQueryPipeline<T extends IStreamBuilder<unknown>>(
// Process the SELECT clause - this is where we flatten the structure
const resultPipeline: KeyedStream | NamespacedAndKeyedStream = query.select
? processSelect(pipeline, query, mainTableAlias, allInputs)
: pipeline

: !query.join && !query.groupBy
? pipeline.pipe(
map(([key, row]) => [key, row[mainTableAlias]] as InputRow)
)
: pipeline
return resultPipeline as T
}
13 changes: 9 additions & 4 deletions packages/db/src/query/query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ export class BaseQueryBuilder<TContext extends Context<Schema>> {
Input
>
}
hasJoin: true
}
>
>
Expand Down Expand Up @@ -474,6 +475,7 @@ export class BaseQueryBuilder<TContext extends Context<Schema>> {
schema: TContext[`schema`] & {
[K in T]: RemoveIndexSignature<TContext[`baseSchema`][T]>
}
hasJoin: true
}
>
>
Expand Down Expand Up @@ -513,6 +515,7 @@ export class BaseQueryBuilder<TContext extends Context<Schema>> {
schema: TContext[`schema`] & {
[K in TAs]: RemoveIndexSignature<TContext[`baseSchema`][TFrom]>
}
hasJoin: true
}
>
>
Expand Down Expand Up @@ -864,10 +867,12 @@ export function queryBuilder<TBaseSchema extends Schema = {}>() {

export type ResultsFromContext<TContext extends Context<Schema>> = Flatten<
TContext[`result`] extends object
? TContext[`result`]
: TContext[`result`] extends undefined
? TContext[`schema`]
: object
? TContext[`result`] // If there is a select we will have a result type
: TContext[`hasJoin`] extends true
? TContext[`schema`] // If there is a join, the query returns the namespaced schema
: TContext[`default`] extends keyof TContext[`schema`]
? TContext[`schema`][TContext[`default`]] // If there is no join we return the flat default schema
: never // Should never happen
>

export type ResultFromQueryBuilder<TQueryBuilder> = Flatten<
Expand Down
1 change: 1 addition & 0 deletions packages/db/src/query/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type Context<
schema: TSchema
default?: keyof TSchema
result?: Record<string, unknown>
hasJoin?: boolean
}

// Helper types
Expand Down
Loading