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

Fix typing to allow different return values per query #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

apricote
Copy link

The current typing requires one to define the return type for all queries when initializing AthenaExpress.

As one wants to execute different queries with different return types they would have to create new instances of AthenaExpress or ignore the typing (using as or @ts-ignore).

By moving the generic parameter from the class to the query method, the expected return type can be set for every call seperatly.

I also added a default parameter any so one does not need to define the return type if they do not with to do so or do not know the exact typing, while still being able to use the general typing of QueryResult.

This change would require all existing typescript users to update their usage. It would be possible for me to add this in a backwards compatible way if requested.

Example in code:

// Type Definitions to illustrate changes
type Movie = {
  name: string;
  year: number;
}

type Song = {
  title: string;
  artist: string;
}


//
// Current behaviour
//
const athenaExpress = new AthenaExpress<Movie>({ /* ... */ });

const movies = await athenaExpress.query("SELECT * FROM movies LIMIT 3");
// Typing of movies.Items is `Movie[]`

// Using different typing for other query is not possible (without type-casting)
const songs = await athenaExpress.query("SELECT * FROM songs LIMIT 3");
// Typing of songs.Items is still `Movie[]

// Updated behaviour
// With my changes it now looks like this:
//
const athenaExpress = new AthenaExpress({ /* ... */ });

const movies = await athenaExpress.query<Movie>("SELECT * FROM movies LIMIT 3");
// Typing of movies.Items is `Movie[]`

const songs = await athenaExpress.query<Song>("SELECT * FROM songs LIMIT 3");
// Typing of songs.Items is `Song[]`

// And if the return type is unknown, one can just omit the typing:
const result = await athenaExpress.query("SELECT * FROM unknownTable LIMIT 3");
// Typing of result.Items is `any[]` 

The current typing requires one to define the return type for all queries
when initializing AthenaExpress.

As one wants to execute different queries with different return types they
would have to create new instances of AthenaExpress or ignore the typing
(using `as` or `@ts-ignore`).

By moving the generic parameter from the class to the `query` method, the
expected return type can be set for every call seperatly.

I also added a default parameter `any` so one does not need to define the
return type if they do not with to do so or do not know the exact typing,
while still being able to use the general typing of `QueryResult`.
@coveralls
Copy link

coveralls commented Nov 16, 2020

Pull Request Test Coverage Report for Build 137

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 136: 0.0%
Covered Lines: 3
Relevant Lines: 3

💛 - Coveralls

@jared-fraser
Copy link

Any chance we can get this merged? 🙏

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.

3 participants