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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ export class Database {
* @param sql SQL statement string
* @returns Statement object
*/
prepare(sql: string): Statement {
return new Statement(this, sql);
prepare<T extends object = Record<string, any>>(sql: string): Statement<T> {
return new Statement<T>(this, sql);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function getColumn(handle: Deno.PointerValue, i: number, int64: boolean): any {
*
* See `Database#prepare` for more information.
*/
export class Statement {
export class Statement<TStatement extends object = Record<string, any>> {
#handle: Deno.PointerValue;
#finalizerToken: { handle: Deno.PointerValue };
#bound = false;
Expand Down Expand Up @@ -188,7 +188,7 @@ export class Statement {
* Run the query and return the resulting rows where rows are objects
* mapping column name to their corresponding values.
*/
all<T extends object = Record<string, any>>(
all<T extends object = TStatement>(
...args: RestBindParameters
): T[] {
return this.#allWithArgs(...args);
Expand Down Expand Up @@ -626,7 +626,7 @@ export class Statement {
}

/** Fetch only first row as an object, if any. */
get<T extends object>(
get<T extends object = TStatement>(
...params: RestBindParameters
): T | undefined {
const handle = this.#handle;
Expand Down