Skip to content

Commit

Permalink
refactor: move stream and async iterator support from SetBase to Se…
Browse files Browse the repository at this point in the history
…lect class

…and ensure a proper result type.
  • Loading branch information
aleclarson committed Nov 9, 2022
1 parent f4af806 commit a313ae5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
25 changes: 0 additions & 25 deletions src/postgres/query/base/set.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
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 @@ -41,27 +39,4 @@ 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]()
}
}
26 changes: 26 additions & 0 deletions src/postgres/query/select.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { renderQuery } from '../internal/query'
import { Query } from '../query'
import { Selectable, SelectResult, SelectResults } from '../selection'
import { QueryStream, QueryStreamConfig } from '../stream'
import { SetType } from '../type'
import { SelectBase } from './base/select'
import { Union } from './union'
Expand All @@ -14,6 +17,29 @@ 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 Down

0 comments on commit a313ae5

Please sign in to comment.