Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow a type parameter for the row results #154

Merged
merged 5 commits into from
Dec 14, 2023
Merged

Conversation

ayrton
Copy link
Contributor

@ayrton ayrton commented Dec 13, 2023

Accept a type parameter for the execute function that represents the returned results. This should allow you to use any of these styles:

// 1. use default row type `type Row = Record<string, any>`
const got = await connection.execute('SELECT 1, null from dual;')

// 2. use default row type `type Row = Record<string, any>` and cast
type Row = {
      ':vtg1'?: number
      null?: null
}
const got: ExecutedQuery<Row> = await connection.execute('SELECT 1, null from dual;')

// 3. use your own row type
type Row = {
      ':vtg1'?: number
      null?: null
}
const got = await connection.execute<Row>('SELECT 1, null from dual;')

It uses function overloading to differentiate between rows as object and rows as array.

Fixes #150
Fixes #71

@ayrton ayrton force-pushed the function-overloading branch from 249bf14 to c5ea50c Compare December 13, 2023 18:21
src/index.ts Outdated Show resolved Hide resolved
@ayrton ayrton force-pushed the function-overloading branch from c5ea50c to c31d98a Compare December 13, 2023 18:27
src/index.ts Outdated Show resolved Hide resolved
@@ -100,18 +98,9 @@ interface QueryResult {
rows?: QueryResultRow[]
}

type ExecuteAs = 'array' | 'object'

type ExecuteOptions = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we still want to keep this because we want to limit the number of valid options passed into Execute.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can only pass these options:

{ as?: 'object'; cast?: Cast } | { as: 'array'; cast?: Cast }

Anything else Typescript will complain:

    261     const got = await connection.execute('SELECT 1 from dual;', null, { as: 'RANDOM' })
                                                                                ~~

      dist/index.d.ts:95:9
        95         as?: 'object';
                   ~~
        The expected type comes from property 'as' which is declared here on type '{ as?: "object"; cast?: (field: Field, value: string) => any; }'
      dist/index.d.ts:99:9
        99         as: 'array';
                   ~~
        The expected type comes from property 'as' which is declared here on type '{ as: "array"; cast?: (field: Field, value: string) => any; }'

@ayrton ayrton enabled auto-merge December 14, 2023 22:53
@ayrton ayrton merged commit 0a7eeee into main Dec 14, 2023
4 checks passed
@ayrton ayrton deleted the function-overloading branch December 14, 2023 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] A type can be defined for rows in the query result New as array/object option breaks Typescript
4 participants