@@ -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