Skip to content

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
@ayrton ayrton force-pushed the function-overloading branch from c5ea50c to c31d98a Compare December 13, 2023 18:27

type ExecuteAs = 'array' | 'object'

type ExecuteOptions = {
Copy link
Contributor

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; }' 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants