@@ -30,7 +30,6 @@ import {
3030 Nullable ,
3131 ShallowRecord ,
3232 Simplify ,
33- SimplifySingleResult ,
3433 SqlBool ,
3534} from '../util/type-utils.js'
3635import {
@@ -83,9 +82,11 @@ import { TopModifier } from '../operation-node/top-node.js'
8382import { parseTop } from '../parser/top-parser.js'
8483import { JoinType } from '../operation-node/join-node.js'
8584import { OrderByInterface } from './order-by-interface.js'
85+ import { Executable , ExecuteOptions } from '../util/executable.js'
8686
8787export 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 > {
0 commit comments