Skip to content

Commit f7216ca

Browse files
committed
fix: Propagate raw query
1 parent acbb672 commit f7216ca

File tree

3 files changed

+82
-82
lines changed

3 files changed

+82
-82
lines changed

src/do.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class StarbaseDBDurableObject extends DurableObject {
102102
// }
103103
// }
104104

105-
public async executeRawQuery<
105+
private async executeRawQuery<
106106
T extends Record<string, SqlStorageValue> = Record<string, SqlStorageValue>
107107
>(opts: { sql: string; params?: unknown[] }) {
108108
const { sql, params } = opts;
@@ -116,15 +116,7 @@ export class StarbaseDBDurableObject extends DurableObject {
116116
cursor = this.sql.exec<T>(sql);
117117
}
118118

119-
return {
120-
cursor,
121-
columns: cursor.columnNames,
122-
rows: Array.from(cursor.raw()),
123-
meta: {
124-
rows_read: cursor.rowsRead,
125-
rows_written: cursor.rowsWritten,
126-
},
127-
};
119+
return cursor;
128120
} catch (error) {
129121
console.error("SQL Execution Error:", error);
130122
throw error;
@@ -136,13 +128,20 @@ export class StarbaseDBDurableObject extends DurableObject {
136128
params?: unknown[];
137129
isRaw?: boolean;
138130
}) {
139-
const result = await this.executeRawQuery(opts);
140-
131+
const cursor = await this.executeRawQuery(opts);
132+
141133
if (opts.isRaw) {
142-
return result;
134+
return {
135+
columns: cursor.columnNames,
136+
rows: Array.from(cursor.raw()),
137+
meta: {
138+
rows_read: cursor.rowsRead,
139+
rows_written: cursor.rowsWritten,
140+
},
141+
};
143142
}
144143

145-
return result.cursor.toArray();
144+
return cursor.toArray();
146145
}
147146

148147
public executeTransaction(

src/handler.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,13 @@ export class StarbaseDB {
243243
return { sql, params };
244244
});
245245

246-
const response = await executeTransaction(
246+
const response = await executeTransaction({
247247
queries,
248248
isRaw,
249-
this.dataSource,
250-
this.config
251-
);
249+
dataSource: this.dataSource,
250+
config: this.config,
251+
});
252+
252253
return createResponse(response, undefined, 200);
253254
} else if (typeof sql !== "string" || !sql.trim()) {
254255
return createResponse(undefined, 'Invalid or empty "sql" field.', 400);

src/operation.ts

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -182,86 +182,85 @@ export async function executeQuery(opts: {
182182
return [];
183183
}
184184

185-
try {
186-
// If the allowlist feature is enabled, we should verify the query is allowed before proceeding.
187-
await isQueryAllowed({
188-
sql: sql,
189-
isEnabled: config?.features?.allowlist ?? false,
190-
dataSource,
191-
config,
192-
});
185+
// If the allowlist feature is enabled, we should verify the query is allowed before proceeding.
186+
await isQueryAllowed({
187+
sql: sql,
188+
isEnabled: config?.features?.allowlist ?? false,
189+
dataSource,
190+
config,
191+
});
193192

194-
// If the row level security feature is enabled, we should apply our policies to this SQL statement.
195-
sql = await applyRLS({
196-
sql,
197-
isEnabled: config?.features?.rls ?? true,
198-
dataSource,
199-
config,
200-
});
193+
// If the row level security feature is enabled, we should apply our policies to this SQL statement.
194+
sql = await applyRLS({
195+
sql,
196+
isEnabled: config?.features?.rls ?? true,
197+
dataSource,
198+
config,
199+
});
201200

202-
// Run the beforeQuery hook for any third party logic to be applied before execution.
203-
const { sql: updatedSQL, params: updatedParams } = await beforeQuery({
204-
sql,
205-
params,
201+
// Run the beforeQuery hook for any third party logic to be applied before execution.
202+
const { sql: updatedSQL, params: updatedParams } = await beforeQuery({
203+
sql,
204+
params,
205+
dataSource,
206+
config,
207+
});
208+
209+
// If the query was modified by RLS then we determine it isn't currently a valid candidate
210+
// for caching. In the future we will support queries impacted by RLS and caching their
211+
// results.
212+
if (!isRaw) {
213+
// If a cached version of this query request exists, this function will fetch the cached results.
214+
const cache = await beforeQueryCache({
215+
sql: updatedSQL,
216+
params: updatedParams,
206217
dataSource,
207-
config,
208218
});
209219

210-
// If the query was modified by RLS then we determine it isn't currently a valid candidate
211-
// for caching. In the future we will support queries impacted by RLS and caching their
212-
// results.
213-
if (!isRaw) {
214-
// If a cached version of this query request exists, this function will fetch the cached results.
215-
const cache = await beforeQueryCache({
216-
sql: updatedSQL,
217-
params: updatedParams,
218-
dataSource,
219-
});
220-
221-
if (cache) {
222-
return cache as QueryResponse;
223-
}
224-
}
225-
226-
let result;
227-
228-
if (dataSource.source === "internal") {
229-
result = await dataSource.rpc.executeQuery({
230-
sql: updatedSQL,
231-
params: updatedParams,
232-
});
233-
} else {
234-
result = await executeExternalQuery({
235-
sql: updatedSQL,
236-
params: updatedParams,
237-
dataSource,
238-
config,
239-
});
220+
if (cache) {
221+
return cache as QueryResponse;
240222
}
223+
}
241224

242-
// If this is a cacheable query, this function will handle that logic.
243-
if (!isRaw) {
244-
await afterQueryCache({ sql, params: updatedParams, result, dataSource });
245-
}
225+
let result;
246226

247-
return await afterQuery({
227+
if (dataSource.source === "internal") {
228+
result = await dataSource.rpc.executeQuery({
248229
sql: updatedSQL,
249-
result,
230+
params: updatedParams,
250231
isRaw,
232+
});
233+
} else {
234+
result = await executeExternalQuery({
235+
sql: updatedSQL,
236+
params: updatedParams,
251237
dataSource,
252238
config,
253239
});
254-
} catch (error: any) {
255-
throw new Error(error.message ?? "An error occurred");
256240
}
241+
242+
// If this is a cacheable query, this function will handle that logic.
243+
if (!isRaw) {
244+
await afterQueryCache({ sql, params: updatedParams, result, dataSource });
245+
}
246+
247+
return await afterQuery({
248+
sql: updatedSQL,
249+
result,
250+
isRaw,
251+
dataSource,
252+
config,
253+
});
257254
}
258255

259-
export async function executeTransaction(
260-
queries: { sql: string; params?: any[] }[],
261-
isRaw: boolean,
262-
dataSource: DataSource,
263-
config: StarbaseDBConfiguration
264-
): Promise<QueryResponse> {
256+
export async function executeTransaction(opts: {
257+
queries: { sql: string; params?: any[] }[];
258+
isRaw: boolean;
259+
dataSource: DataSource;
260+
config: StarbaseDBConfiguration;
261+
}): Promise<QueryResponse> {
262+
const { queries, isRaw, dataSource, config } = opts;
263+
265264
if (!dataSource) {
266265
console.error("Data source not found.");
267266
return [];
@@ -277,6 +276,7 @@ export async function executeTransaction(
277276
dataSource,
278277
config,
279278
});
279+
280280
results.push(result);
281281
}
282282

0 commit comments

Comments
 (0)