Skip to content

Commit

Permalink
feat: run method that returns affected row count
Browse files Browse the repository at this point in the history
Because we throw away all other details of the result, this adds a new
method that will be able to retain more than just the result. For now
we can return the row count.
  • Loading branch information
amerryma committed Jan 30, 2025
1 parent 01beb77 commit fb7e7e4
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion packages/runtime/src/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export interface ICursor<T> {
}

export interface IDatabaseConnection {
query: (query: string, bindings: any[]) => Promise<{ rows: any[] }>;
query: (
query: string,
bindings: any[],
) => Promise<{ rows: any[]; rowCount: number }>;
stream?: (query: string, bindings: any[]) => ICursor<any[]>;
}

Expand Down Expand Up @@ -95,6 +98,11 @@ export class PreparedQuery<TParamType, TResultType> {
dbConnection: IDatabaseConnection,
) => Promise<Array<TResultType>>;

public runWithCounts: (
params: TParamType,
dbConnection: IDatabaseConnection,
) => Promise<{ result: Array<TResultType>; rowCount: number }>;

public stream: (
params: TParamType,
dbConnection: IDatabaseConnection,
Expand All @@ -112,6 +120,17 @@ export class PreparedQuery<TParamType, TResultType> {
const result = await connection.query(processedQuery, bindings);
return mapQueryResultRows(result.rows);
};
this.runWithCounts = async (params, connection) => {
const { query: processedQuery, bindings } = processSQLQueryIR(
this.queryIR,
params as any,
);
const result = await connection.query(processedQuery, bindings);
return {
result: mapQueryResultRows(result.rows),
rowCount: result.rowCount,
};
};
this.stream = (params, connection) => {
const { query: processedQuery, bindings } = processSQLQueryIR(
this.queryIR,
Expand Down

0 comments on commit fb7e7e4

Please sign in to comment.