Skip to content

Commit

Permalink
fix: move stream and async iterator implementations back to SetBase…
Browse files Browse the repository at this point in the history
… class
  • Loading branch information
aleclarson committed Nov 9, 2022
1 parent 1d20005 commit 43388c2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
25 changes: 25 additions & 0 deletions src/postgres/query/base/set.ts
@@ -1,6 +1,8 @@
import { renderQuery } from '../../internal/query'
import { SetProps } from '../../props/set'
import { Query } from '../../query'
import type { Selectable, SelectionSource } from '../../selection'
import type { QueryStreamConfig } from '../../stream'
import { orderBy, SortSelection, SortSelector } from '../orderBy'

const kSelectFrom = Symbol()
Expand Down Expand Up @@ -39,4 +41,27 @@ export abstract class SetBase<
self.props.orderBy = orderBy(self.sources, selector)
return self
}

stream(config?: QueryStreamConfig) {
const ctx: Query.Context = {
query: this as any,
values: [],
resolvers: [],
mutators: [],
}

// TODO: apply mutators to stream
const query = renderQuery(ctx)
const client = this.db['getClient'](ctx)
if (client.stream) {
return client.stream(query, ctx.values, config)
}
throw Error('Streaming not supported by your Postgres client')
}

[Symbol.asyncIterator]() {
const stream = this.stream()
stream.resume()
return stream[Symbol.asyncIterator]()
}
}
29 changes: 4 additions & 25 deletions src/postgres/query/select.ts
@@ -1,5 +1,3 @@
import { renderQuery } from '../internal/query'
import { Query } from '../query'
import { Selectable, SelectResult, SelectResults } from '../selection'
import { QueryStream, QueryStreamConfig } from '../stream'
import { SetType } from '../type'
Expand All @@ -17,29 +15,6 @@ export class Select<From extends Selectable[] = any> //
query: new Union(this.db),
})
}

stream(config?: QueryStreamConfig): QueryStream<SelectResult<From>> {
const ctx: Query.Context = {
query: this as any,
values: [],
resolvers: [],
mutators: [],
}

// TODO: apply mutators to stream
const query = renderQuery(ctx)
const client = this.db['getClient'](ctx)
if (client.stream) {
return client.stream(query, ctx.values, config)
}
throw Error('Streaming not supported by your Postgres client')
}

[Symbol.asyncIterator](): AsyncIterableIterator<SelectResult<From>> {
const stream = this.stream()
stream.resume()
return stream[Symbol.asyncIterator]()
}
}

export interface Select<From>
Expand All @@ -49,4 +24,8 @@ export interface Select<From>
from: Joined,
on: Where<[...From, Joined]>
): Select<[...From, Joined]>

stream(config?: QueryStreamConfig): QueryStream<SelectResult<From>>

[Symbol.asyncIterator](): AsyncIterableIterator<SelectResult<From>>
}

0 comments on commit 43388c2

Please sign in to comment.