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

Making statements generic #150

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

Conversation

varanauskas
Copy link

@varanauskas varanauskas commented Feb 15, 2025

Currently some statement methods support generics for type hinting the result of the query, for example:

class Statement {
  all<T extends object = Record<string, any>>(...args: RestBindParameters): T[]
  get<T extends object>(...params: RestBindParameters): T | undefined
}

In a lot of cases the generic used is the same between these two methods because the same query was used to prepare the statement.

I propose adding a generic type to statement itself that would allow to both of these methods to be typed automatically:

interface UserTable {
  name: string;
  email: string;
}
const statement: Statement<UserTable> = new Database().prepare<UserTable>("SELECT name, email FROM users");

const singleUser: UserTable | undefined = statement.get();
const allUsers: UserTable[] = statement.all();

This would lay the groundwork for typing the iterator in the future so it could be used like so:

for (const row of statement) {
  const user: UserTable = row;
}

What remains unsolved is typing of value and values methods, I propose leaving them as is for simplifying the API/proposal

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.

1 participant