11import { createResponse } from './utils' ;
2+ import { Env } from './index' ;
23
34export type OperationQueueItem = {
45 queries : { sql : string ; params ?: any [ ] } [ ] ;
@@ -19,7 +20,13 @@ export type RawQueryResponse = {
1920
2021export type QueryResponse = any [ ] | RawQueryResponse ;
2122
22- export function executeQuery ( sql : string , params : any [ ] | undefined , isRaw : boolean , sqlInstance : any ) : QueryResponse {
23+ async function afterQuery ( sql : string , result : any , isRaw : boolean , sqlInstance : any , env ?: Env ) : Promise < any > {
24+ // ## DO NOT REMOVE: TEMPLATE AFTER QUERY HOOK ##
25+
26+ return result ;
27+ }
28+
29+ export async function executeQuery ( sql : string , params : any [ ] | undefined , isRaw : boolean , sqlInstance : any , env ?: Env ) : Promise < QueryResponse > {
2330 try {
2431 let cursor ;
2532
@@ -44,21 +51,22 @@ export function executeQuery(sql: string, params: any[] | undefined, isRaw: bool
4451 result = cursor . toArray ( ) ;
4552 }
4653
47- return result ;
54+ // Apply any post-query transformations prior to returning the result
55+ return await afterQuery ( sql , result , isRaw , sqlInstance , env ) ;
4856 } catch ( error ) {
4957 console . error ( 'SQL Execution Error:' , error ) ;
5058 throw error ;
5159 }
5260}
5361
54- export async function executeTransaction ( queries : { sql : string ; params ?: any [ ] } [ ] , isRaw : boolean , sqlInstance : any , ctx : any ) : Promise < any [ ] > {
55- return ctx . storage . transactionSync ( ( ) => {
62+ export async function executeTransaction ( queries : { sql : string ; params ?: any [ ] } [ ] , isRaw : boolean , sqlInstance : any , ctx : any , env ?: Env ) : Promise < any [ ] > {
63+ return ctx . storage . transactionSync ( async ( ) => {
5664 const results = [ ] ;
5765
5866 try {
5967 for ( const queryObj of queries ) {
6068 const { sql, params } = queryObj ;
61- const result = executeQuery ( sql , params , isRaw , sqlInstance ) ;
69+ const result = await executeQuery ( sql , params , isRaw , sqlInstance ) ;
6270 results . push ( result ) ;
6371 }
6472
@@ -117,7 +125,8 @@ export async function processNextOperation(
117125 sqlInstance : any ,
118126 operationQueue : OperationQueueItem [ ] ,
119127 ctx : any ,
120- processingOperation : { value : boolean }
128+ processingOperation : { value : boolean } ,
129+ env : Env
121130) {
122131 if ( processingOperation . value ) {
123132 // Already processing an operation
@@ -136,17 +145,17 @@ export async function processNextOperation(
136145 let result ;
137146
138147 if ( isTransaction ) {
139- result = await executeTransaction ( queries , isRaw , sqlInstance , ctx ) ;
148+ result = await executeTransaction ( queries , isRaw , sqlInstance , ctx , env ) ;
140149 } else {
141150 const { sql, params } = queries [ 0 ] ;
142- result = executeQuery ( sql , params , isRaw , sqlInstance ) ;
151+ result = await executeQuery ( sql , params , isRaw , sqlInstance , env ) ;
143152 }
144153
145154 resolve ( result ) ;
146155 } catch ( error : any ) {
147156 reject ( error . message || 'Operation failed.' ) ;
148157 } finally {
149158 processingOperation . value = false ;
150- await processNextOperation ( sqlInstance , operationQueue , ctx , processingOperation ) ;
159+ await processNextOperation ( sqlInstance , operationQueue , ctx , processingOperation , env ) ;
151160 }
152161}
0 commit comments