Skip to content

Commit 7931e62

Browse files
authored
feat: run method that returns affected row count (#598)
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.
1 parent 01beb77 commit 7931e62

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

packages/runtime/src/tag.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ export interface ICursor<T> {
88
}
99

1010
export interface IDatabaseConnection {
11-
query: (query: string, bindings: any[]) => Promise<{ rows: any[] }>;
11+
query: (
12+
query: string,
13+
bindings: any[],
14+
) => Promise<{ rows: any[]; rowCount: number }>;
1215
stream?: (query: string, bindings: any[]) => ICursor<any[]>;
1316
}
1417

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

101+
public runWithCounts: (
102+
params: TParamType,
103+
dbConnection: IDatabaseConnection,
104+
) => Promise<{ result: Array<TResultType>; rowCount: number }>;
105+
98106
public stream: (
99107
params: TParamType,
100108
dbConnection: IDatabaseConnection,
@@ -112,6 +120,17 @@ export class PreparedQuery<TParamType, TResultType> {
112120
const result = await connection.query(processedQuery, bindings);
113121
return mapQueryResultRows(result.rows);
114122
};
123+
this.runWithCounts = async (params, connection) => {
124+
const { query: processedQuery, bindings } = processSQLQueryIR(
125+
this.queryIR,
126+
params as any,
127+
);
128+
const result = await connection.query(processedQuery, bindings);
129+
return {
130+
result: mapQueryResultRows(result.rows),
131+
rowCount: result.rowCount,
132+
};
133+
};
115134
this.stream = (params, connection) => {
116135
const { query: processedQuery, bindings } = processSQLQueryIR(
117136
this.queryIR,

0 commit comments

Comments
 (0)