11import { DurableObject } from "cloudflare:workers" ;
2- import { OperationQueueItem } from "./operation" ;
3- import { createResponse } from "./utils" ;
2+ // import { OperationQueueItem } from "./operation";
3+ // import { createResponse } from "./utils";
44
55export class StarbaseDBDurableObject extends DurableObject {
66 // Durable storage for the SQL database
77 public sql : SqlStorage ;
88 public storage : DurableObjectStorage ;
99
10- // Queue of operations to be processed, with each operation containing a list of queries to be executed
11- private operationQueue : Array < OperationQueueItem > = [ ] ;
10+ // // Queue of operations to be processed, with each operation containing a list of queries to be executed
11+ // private operationQueue: Array<OperationQueueItem> = [];
1212
13- // Flag to indicate if an operation is currently being processed
14- private processingOperation : { value : boolean } = { value : false } ;
13+ // // Flag to indicate if an operation is currently being processed
14+ // private processingOperation: { value: boolean } = { value: false };
1515
1616 /**
1717 * The constructor is invoked once upon creation of the Durable Object, i.e. the first call to
@@ -64,42 +64,43 @@ export class StarbaseDBDurableObject extends DurableObject {
6464 } ;
6565 }
6666
67- /**
68- * Execute a raw SQL query on the database, typically used for external requests
69- * from other service bindings (e.g. auth). This serves as an exposed function for
70- * other service bindings to query the database without having to have knowledge of
71- * the current operation queue or processing state.
72- *
73- * @param sql - The SQL query to execute.
74- * @param params - Optional parameters for the SQL query.
75- * @returns A response containing the query result or an error message.
76- */
77- private async executeExternalQuery (
78- sql : string ,
79- params : any [ ] | undefined
80- ) : Promise < any > {
81- try {
82- const queries = [ { sql, params } ] ;
83- const response = await this . enqueueOperation (
84- queries ,
85- false ,
86- false ,
87- this . operationQueue ,
88- ( ) =>
89- this . processNextOperation (
90- this . sql ,
91- this . operationQueue ,
92- this . ctx ,
93- this . processingOperation
94- )
95- ) ;
96-
97- return response ;
98- } catch ( error : any ) {
99- console . error ( "Execute External Query Error:" , error ) ;
100- return null ;
101- }
102- }
67+ // TODO: Hiding for now as it's not used in the current implementation
68+ // /**
69+ // * Execute a raw SQL query on the database, typically used for external requests
70+ // * from other service bindings (e.g. auth). This serves as an exposed function for
71+ // * other service bindings to query the database without having to have knowledge of
72+ // * the current operation queue or processing state.
73+ // *
74+ // * @param sql - The SQL query to execute.
75+ // * @param params - Optional parameters for the SQL query.
76+ // * @returns A response containing the query result or an error message.
77+ // */
78+ // private async executeExternalQuery(
79+ // sql: string,
80+ // params: any[] | undefined
81+ // ): Promise<any> {
82+ // try {
83+ // const queries = [{ sql, params }];
84+ // const response = await this.enqueueOperation(
85+ // queries,
86+ // false,
87+ // false,
88+ // this.operationQueue,
89+ // () =>
90+ // this.processNextOperation(
91+ // this.sql,
92+ // this.operationQueue,
93+ // this.ctx,
94+ // this.processingOperation
95+ // )
96+ // );
97+
98+ // return response;
99+ // } catch (error: any) {
100+ // console.error("Execute External Query Error:", error);
101+ // return null;
102+ // }
103+ // }
103104
104105 public async executeRawQuery <
105106 T extends Record < string , SqlStorageValue > = Record < string , SqlStorageValue >
@@ -130,8 +131,17 @@ export class StarbaseDBDurableObject extends DurableObject {
130131 }
131132 }
132133
133- public async executeQuery ( opts : { sql : string ; params ?: unknown [ ] } ) {
134+ public async executeQuery ( opts : {
135+ sql : string ;
136+ params ?: unknown [ ] ;
137+ isRaw ?: boolean ;
138+ } ) {
134139 const result = await this . executeRawQuery ( opts ) ;
140+
141+ if ( opts . isRaw ) {
142+ return result ;
143+ }
144+
135145 return result . cursor . toArray ( ) ;
136146 }
137147
@@ -145,7 +155,7 @@ export class StarbaseDBDurableObject extends DurableObject {
145155 try {
146156 for ( const queryObj of queries ) {
147157 const { sql, params } = queryObj ;
148- const result = this . executeQuery ( { sql, params } ) ;
158+ const result = this . executeQuery ( { sql, params, isRaw } ) ;
149159 results . push ( result ) ;
150160 }
151161
@@ -157,90 +167,92 @@ export class StarbaseDBDurableObject extends DurableObject {
157167 } ) ;
158168 }
159169
160- enqueueOperation (
161- queries : { sql : string ; params ?: any [ ] } [ ] ,
162- isTransaction : boolean ,
163- isRaw : boolean ,
164- operationQueue : any [ ] ,
165- processNextOperation : ( ) => Promise < void >
166- ) : Promise < { result ?: any ; error ?: string | undefined ; status : number } > {
167- const MAX_WAIT_TIME = 25000 ;
168- return new Promise ( ( resolve , reject ) => {
169- const timeout = setTimeout ( ( ) => {
170- reject ( createResponse ( undefined , "Operation timed out." , 503 ) ) ;
171- } , MAX_WAIT_TIME ) ;
172-
173- operationQueue . push ( {
174- queries,
175- isTransaction,
176- isRaw,
177- resolve : ( value : any ) => {
178- clearTimeout ( timeout ) ;
179-
180- resolve ( {
181- result : value ,
182- error : undefined ,
183- status : 200 ,
184- } ) ;
185- } ,
186- reject : ( reason ?: any ) => {
187- clearTimeout ( timeout ) ;
188-
189- reject ( {
190- result : undefined ,
191- error : reason ?? "Operation failed." ,
192- status : 500 ,
193- } ) ;
194- } ,
195- } ) ;
170+ // TODO: Hiding for now as it's not used in the current implementation
171+ // private enqueueOperation(
172+ // queries: { sql: string; params?: any[] }[],
173+ // isTransaction: boolean,
174+ // isRaw: boolean,
175+ // operationQueue: any[],
176+ // processNextOperation: () => Promise<void>
177+ // ): Promise<{ result?: any; error?: string | undefined; status: number }> {
178+ // const MAX_WAIT_TIME = 25000;
179+ // return new Promise((resolve, reject) => {
180+ // const timeout = setTimeout(() => {
181+ // reject(createResponse(undefined, "Operation timed out.", 503));
182+ // }, MAX_WAIT_TIME);
196183
197- processNextOperation ( ) . catch ( ( err ) => {
198- console . error ( "Error processing operation queue:" , err ) ;
199- } ) ;
200- } ) ;
201- }
184+ // operationQueue.push({
185+ // queries,
186+ // isTransaction,
187+ // isRaw,
188+ // resolve: (value: any) => {
189+ // clearTimeout(timeout);
202190
203- private async processNextOperation (
204- sqlInstance : any ,
205- operationQueue : OperationQueueItem [ ] ,
206- ctx : any ,
207- processingOperation : { value : boolean }
208- ) {
209- if ( processingOperation . value ) {
210- // Already processing an operation
211- return ;
212- }
191+ // resolve({
192+ // result: value,
193+ // error: undefined,
194+ // status: 200,
195+ // });
196+ // },
197+ // reject: (reason?: any) => {
198+ // clearTimeout(timeout);
213199
214- if ( operationQueue . length === 0 ) {
215- // No operations remaining to process
216- return ;
217- }
200+ // reject({
201+ // result: undefined,
202+ // error: reason ?? "Operation failed.",
203+ // status: 500,
204+ // });
205+ // },
206+ // });
218207
219- processingOperation . value = true ;
220- const { queries, isTransaction, isRaw, resolve, reject } =
221- operationQueue . shift ( ) ! ;
208+ // processNextOperation().catch((err) => {
209+ // console.error("Error processing operation queue:", err);
210+ // });
211+ // });
212+ // }
222213
223- try {
224- let result ;
214+ // TODO: Hiding for now as it's not used in the current implementation
215+ // private async processNextOperation(
216+ // sqlInstance: any,
217+ // operationQueue: OperationQueueItem[],
218+ // ctx: any,
219+ // processingOperation: { value: boolean }
220+ // ) {
221+ // if (processingOperation.value) {
222+ // // Already processing an operation
223+ // return;
224+ // }
225225
226- if ( isTransaction ) {
227- result = await this . executeTransaction ( queries , isRaw ) ;
228- } else {
229- const { sql, params } = queries [ 0 ] ;
230- result = this . executeQuery ( { sql, params } ) ;
231- }
226+ // if (operationQueue.length === 0) {
227+ // // No operations remaining to process
228+ // return;
229+ // }
232230
233- resolve ( result ) ;
234- } catch ( error : any ) {
235- reject ( error . message || "Operation failed." ) ;
236- } finally {
237- processingOperation . value = false ;
238- await this . processNextOperation (
239- sqlInstance ,
240- operationQueue ,
241- ctx ,
242- processingOperation
243- ) ;
244- }
245- }
231+ // processingOperation.value = true;
232+ // const { queries, isTransaction, isRaw, resolve, reject } =
233+ // operationQueue.shift()!;
234+
235+ // try {
236+ // let result;
237+
238+ // if (isTransaction) {
239+ // result = await this.executeTransaction(queries, isRaw);
240+ // } else {
241+ // const { sql, params } = queries[0];
242+ // result = this.executeQuery({ sql, params });
243+ // }
244+
245+ // resolve(result);
246+ // } catch (error: any) {
247+ // reject(error.message || "Operation failed.");
248+ // } finally {
249+ // processingOperation.value = false;
250+ // await this.processNextOperation(
251+ // sqlInstance,
252+ // operationQueue,
253+ // ctx,
254+ // processingOperation
255+ // );
256+ // }
257+ // }
246258}
0 commit comments