Skip to content

Commit

Permalink
fix: simplify execute result type
Browse files Browse the repository at this point in the history
  • Loading branch information
zoubingwu committed Oct 23, 2024
1 parent aa5a98c commit 95de050
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,7 @@ export class Tx<T extends Config> {
args: ExecuteArgs = null,
options: E = defaultExecuteOptions as E,
txOptions: TxOptions = {}
): Promise<
E extends { fullResult: boolean }
? E['fullResult'] extends true
? FullResult
: Row[]
: T['fullResult'] extends true
? FullResult
: Row[]
> {
): Promise<ExecuteResult<E, T>> {
return this.conn.execute(query, args, options, txOptions)
}

Expand All @@ -68,6 +60,14 @@ export class Tx<T extends Config> {
}
}

export type ExecuteResult<E extends ExecuteOptions, T extends Config> = E extends { fullResult: boolean }
? E['fullResult'] extends true
? FullResult
: Row[]
: T['fullResult'] extends true
? FullResult
: Row[]

export class Connection<T extends Config> {
private config: T
private session: Session
Expand Down Expand Up @@ -113,15 +113,7 @@ export class Connection<T extends Config> {
args: ExecuteArgs = null,
options: E = defaultExecuteOptions as E,
txOptions: TxOptions = {}
): Promise<
E extends { fullResult: boolean }
? E['fullResult'] extends true
? FullResult
: Row[]
: T['fullResult'] extends true
? FullResult
: Row[]
> {
): Promise<ExecuteResult<E, T>> {
const sql = args ? format(query, args) : query
const body = JSON.stringify({ query: sql })
const debug = options.debug ?? this.config.debug ?? false
Expand Down Expand Up @@ -160,10 +152,10 @@ export class Connection<T extends Config> {
rowsAffected,
lastInsertId,
rowCount: rows.length
} as any
} as ExecuteResult<E, T>
}

return rows as any
return rows as ExecuteResult<E, T>
}
}

Expand Down

0 comments on commit 95de050

Please sign in to comment.