athena-express icon indicating copy to clipboard operation
athena-express copied to clipboard

Fix typing to allow different return values per query

Open apricote opened this issue 3 years ago • 2 comments

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[]` 

apricote avatar Nov 16 '20 18:11 apricote

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

coveralls avatar Nov 16 '20 19:11 coveralls

Any chance we can get this merged? 🙏

jared-fraser avatar Sep 20 '22 00:09 jared-fraser