Skip to content

Commit 9f456b9

Browse files
committed
apply executable @ select.
1 parent 299ccd3 commit 9f456b9

3 files changed

Lines changed: 51 additions & 47 deletions

File tree

src/query-builder/select-query-builder.ts

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import {
3030
Nullable,
3131
ShallowRecord,
3232
Simplify,
33-
SimplifySingleResult,
3433
SqlBool,
3534
} from '../util/type-utils.js'
3635
import {
@@ -83,9 +82,11 @@ import { TopModifier } from '../operation-node/top-node.js'
8382
import { parseTop } from '../parser/top-parser.js'
8483
import { JoinType } from '../operation-node/join-node.js'
8584
import { OrderByInterface } from './order-by-interface.js'
85+
import { Executable, ExecuteOptions } from '../util/executable.js'
8686

8787
export interface SelectQueryBuilder<DB, TB extends keyof DB, O>
88-
extends WhereInterface<DB, TB>,
88+
extends Executable<Simplify<O>>,
89+
WhereInterface<DB, TB>,
8990
HavingInterface<DB, TB>,
9091
OrderByInterface<DB, TB, O>,
9192
SelectQueryBuilderExpression<O>,
@@ -2117,31 +2118,20 @@ export interface SelectQueryBuilder<DB, TB extends keyof DB, O>
21172118

21182119
compile(): CompiledQuery<Simplify<O>>
21192120

2120-
/**
2121-
* Executes the query and returns an array of rows.
2122-
*
2123-
* Also see the {@link executeTakeFirst} and {@link executeTakeFirstOrThrow} methods.
2124-
*/
21252121
execute(): Promise<Simplify<O>[]>
21262122

2127-
/**
2128-
* Executes the query and returns the first result or undefined if
2129-
* the query returned no result.
2130-
*/
2131-
executeTakeFirst(): Promise<SimplifySingleResult<O>>
2123+
executeTakeFirst(): Promise<Simplify<O> | undefined>
21322124

2133-
/**
2134-
* Executes the query and returns the first result or throws if
2135-
* the query returned no result.
2136-
*
2137-
* By default an instance of {@link NoResultError} is thrown, but you can
2138-
* provide a custom error class, or callback to throw a different
2139-
* error.
2140-
*/
21412125
executeTakeFirstOrThrow(
21422126
errorConstructor?: NoResultErrorConstructor | ((node: QueryNode) => Error),
21432127
): Promise<Simplify<O>>
21442128

2129+
executeTakeFirstOrThrow(
2130+
options?: ExecuteOptions & {
2131+
errorConstructor?: NoResultErrorConstructor | ((node: QueryNode) => Error)
2132+
},
2133+
): Promise<O>
2134+
21452135
stream(chunkSize?: number): AsyncIterableIterator<O>
21462136

21472137
explain<ER extends Record<string, any> = Record<string, any>>(
@@ -2647,35 +2637,43 @@ class SelectQueryBuilderImpl<DB, TB extends keyof DB, O>
26472637
)
26482638
}
26492639

2650-
async execute(): Promise<Simplify<O>[]> {
2640+
async execute(options?: any): Promise<any> {
26512641
const compiledQuery = this.compile()
26522642

2653-
const result = await this.#props.executor.executeQuery<O>(compiledQuery)
2643+
const result = await this.#props.executor.executeQuery<O>(
2644+
compiledQuery,
2645+
options,
2646+
)
26542647

26552648
return result.rows
26562649
}
26572650

2658-
async executeTakeFirst(): Promise<SimplifySingleResult<O>> {
2659-
const [result] = await this.execute()
2660-
return result as SimplifySingleResult<O>
2651+
async executeTakeFirst(options?: any): Promise<any> {
2652+
const [result] = await this.execute(options)
2653+
return result
26612654
}
26622655

2663-
async executeTakeFirstOrThrow(
2664-
errorConstructor:
2665-
| NoResultErrorConstructor
2666-
| ((node: QueryNode) => Error) = NoResultError,
2667-
): Promise<Simplify<O>> {
2668-
const result = await this.executeTakeFirst()
2656+
async executeTakeFirstOrThrow(errorConstructorOrOptions?: any): Promise<any> {
2657+
if (typeof errorConstructorOrOptions === 'function') {
2658+
errorConstructorOrOptions = {
2659+
errorConstructor: errorConstructorOrOptions,
2660+
}
2661+
}
2662+
2663+
const result = await this.executeTakeFirst(errorConstructorOrOptions)
26692664

26702665
if (result === undefined) {
2666+
const errorConstructor =
2667+
errorConstructorOrOptions?.errorConstructor ?? NoResultError
2668+
26712669
const error = isNoResultErrorConstructor(errorConstructor)
26722670
? new errorConstructor(this.toOperationNode())
26732671
: errorConstructor(this.toOperationNode())
26742672

26752673
throw error
26762674
}
26772675

2678-
return result as O
2676+
return result
26792677
}
26802678

26812679
async *stream(chunkSize: number = 100): AsyncIterableIterator<O> {

src/query-executor/query-executor.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ export interface QueryExecutor extends ConnectionProvider {
4444
* Executes a compiled query and runs the result through all plugins'
4545
* `transformResult` method.
4646
*/
47-
executeQuery<R>(compiledQuery: CompiledQuery<R>): Promise<QueryResult<R>>
47+
executeQuery<R>(
48+
compiledQuery: CompiledQuery<R>,
49+
options?: ExecuteQueryOptions,
50+
): Promise<QueryResult<R>>
4851

4952
/**
5053
* Executes a compiled query and runs the result through all plugins'
@@ -88,3 +91,16 @@ export interface QueryExecutor extends ConnectionProvider {
8891
*/
8992
withoutPlugins(): QueryExecutor
9093
}
94+
95+
export interface ExecuteQueryOptions {
96+
/**
97+
* An optional signal that can be used to abort the execution of the query.
98+
*
99+
* This is useful for cancelling long-running queries, for example when
100+
* the user navigates away from the page or closes the browser tab.
101+
*
102+
* Writes (insert, update, delete) are not cancellable in most database engines,
103+
* so this signal is mostly useful for read queries.
104+
*/
105+
abortSignal?: AbortSignal
106+
}

src/util/executable.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { QueryNode } from '../operation-node/query-node'
2-
import { NoResultErrorConstructor } from '../query-builder/no-result-error'
1+
import { QueryNode } from '../operation-node/query-node.js'
2+
import { NoResultErrorConstructor } from '../query-builder/no-result-error.js'
3+
import { ExecuteQueryOptions } from '../query-executor/query-executor.js'
34

45
export interface Executable<O> {
56
/**
@@ -34,15 +35,4 @@ export interface Executable<O> {
3435
): Promise<O>
3536
}
3637

37-
export interface ExecuteOptions {
38-
/**
39-
* An optional signal that can be used to abort the execution of the query.
40-
*
41-
* This is useful for cancelling long-running queries, for example when
42-
* the user navigates away from the page or closes the browser tab.
43-
*
44-
* Writes (insert, update, delete) are not cancellable in most database engines,
45-
* so this signal is mostly useful for read queries.
46-
*/
47-
abortSignal?: AbortSignal
48-
}
38+
export interface ExecuteOptions extends ExecuteQueryOptions {}

0 commit comments

Comments
 (0)