ghdna / athena-express

Athena-Express can simplify executing SQL queries in Amazon Athena AND fetching cleaned-up JSON results in the same synchronous or asynchronous request - well suited for web applications.
https://www.npmjs.com/package/athena-express
MIT License
181 stars 70 forks source link

Fix typing to allow different return values per query #48

Open apricote opened 3 years ago

apricote commented 3 years ago

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[]` 
coveralls commented 3 years ago

Pull Request Test Coverage Report for Build 137


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

💛 - Coveralls
jared-fraser commented 2 years ago

Any chance we can get this merged? 🙏