Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyuhang0 committed Oct 23, 2024
1 parent 6c2bee2 commit 9406684
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
6 changes: 4 additions & 2 deletions integration-test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const table = 'employee'

const EmployeeTable = `CREATE TABLE ${database}.${table} (emp_no INT,first_name VARCHAR(255),last_name VARCHAR(255))`

function assertType<T>(value: T): void {}
function assertType<T>(value: T): void {
console.log(`Value is of type ${typeof value}`)
}

beforeAll(async () => {
dotenv.config()
Expand All @@ -25,7 +27,7 @@ describe('basic', () => {
const con = connect({ url: databaseURL, database: database, fetch, debug: true })
const results = await con.execute(`SHOW TABLES`)

assertType<Row[]>(results)
assertType<Row>(results)

expect(JSON.stringify(results)).toContain(`${table}`)
})
Expand Down
2 changes: 1 addition & 1 deletion integration-test/type.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { connect, Row, FullResult } from '../dist/index'
import { connect } from '../dist/index'
import { fetch } from 'undici'
import * as dotenv from 'dotenv'
import { uint8ArrayToHex } from '../src/format'
Expand Down
32 changes: 20 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,27 @@ interface QueryExecuteResponse {

const defaultExecuteOptions: ExecuteOptions = {}

export class Tx<T extends Config = {}> {
export class Tx<T extends Config = object> {
private conn: Connection<T>

constructor(conn: Connection<T>) {
this.conn = conn
}

async execute<E extends ExecuteOptions = {}>(
async execute<E extends ExecuteOptions = object>(
query: string,
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[])
> {
E extends { fullResult: boolean }
? E['fullResult'] extends true
? FullResult
: Row[]
: T['fullResult'] extends true
? FullResult
: Row[]
> {
return this.conn.execute(query, args, options, txOptions)
}

Expand All @@ -64,7 +68,7 @@ export class Tx<T extends Config = {}> {
}
}

export class Connection<T extends Config = {}> {
export class Connection<T extends Config = object> {
private config: T
private session: Session

Expand Down Expand Up @@ -97,22 +101,26 @@ export class Connection<T extends Config = {}> {
return this.config
}

async begin(txOptions: TxOptions = {}) {
async begin(txOptions: TxOptions = object) {

Check failure on line 104 in src/index.ts

View workflow job for this annotation

GitHub Actions / test

Cannot find name 'object'. Did you mean 'Object'?

Check failure on line 104 in src/index.ts

View workflow job for this annotation

GitHub Actions / test-ubuntu

Cannot find name 'object'. Did you mean 'Object'?

Check failure on line 104 in src/index.ts

View workflow job for this annotation

GitHub Actions / test-macos

Cannot find name 'object'. Did you mean 'Object'?
const conn = new Connection<T>(this.config)
const tx = new Tx<T>(conn)
await tx.execute<T>('BEGIN', undefined, undefined, txOptions)
return tx
}

async execute<E extends ExecuteOptions = {}>(
async execute<E extends ExecuteOptions = object>(
query: string,
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[])
E extends { fullResult: boolean }
? E['fullResult'] extends true
? FullResult
: Row[]
: T['fullResult'] extends true
? FullResult
: Row[]
> {
const sql = args ? format(query, args) : query
const body = JSON.stringify({ query: sql })
Expand Down

0 comments on commit 9406684

Please sign in to comment.