|
1 | 1 | import { DurableObject } from 'cloudflare:workers' |
2 | | -// import { OperationQueueItem } from "./operation"; |
3 | | -// import { createResponse } from "./utils"; |
4 | 2 |
|
5 | 3 | export class StarbaseDBDurableObject extends DurableObject { |
6 | 4 | // Durable storage for the SQL database |
7 | 5 | public sql: SqlStorage |
8 | 6 | public storage: DurableObjectStorage |
9 | 7 |
|
10 | | - // // Queue of operations to be processed, with each operation containing a list of queries to be executed |
11 | | - // private operationQueue: Array<OperationQueueItem> = []; |
12 | | - |
13 | | - // // Flag to indicate if an operation is currently being processed |
14 | | - // private processingOperation: { value: boolean } = { value: false }; |
15 | | - |
16 | 8 | /** |
17 | 9 | * The constructor is invoked once upon creation of the Durable Object, i.e. the first call to |
18 | 10 | * `DurableObjectStub::get` for a given identifier (no-op constructors can be omitted) |
@@ -73,44 +65,6 @@ export class StarbaseDBDurableObject extends DurableObject { |
73 | 65 | } |
74 | 66 | } |
75 | 67 |
|
76 | | - // TODO: Hiding for now as it's not used in the current implementation |
77 | | - // /** |
78 | | - // * Execute a raw SQL query on the database, typically used for external requests |
79 | | - // * from other service bindings (e.g. auth). This serves as an exposed function for |
80 | | - // * other service bindings to query the database without having to have knowledge of |
81 | | - // * the current operation queue or processing state. |
82 | | - // * |
83 | | - // * @param sql - The SQL query to execute. |
84 | | - // * @param params - Optional parameters for the SQL query. |
85 | | - // * @returns A response containing the query result or an error message. |
86 | | - // */ |
87 | | - // private async executeExternalQuery( |
88 | | - // sql: string, |
89 | | - // params: any[] | undefined |
90 | | - // ): Promise<any> { |
91 | | - // try { |
92 | | - // const queries = [{ sql, params }]; |
93 | | - // const response = await this.enqueueOperation( |
94 | | - // queries, |
95 | | - // false, |
96 | | - // false, |
97 | | - // this.operationQueue, |
98 | | - // () => |
99 | | - // this.processNextOperation( |
100 | | - // this.sql, |
101 | | - // this.operationQueue, |
102 | | - // this.ctx, |
103 | | - // this.processingOperation |
104 | | - // ) |
105 | | - // ); |
106 | | - |
107 | | - // return response; |
108 | | - // } catch (error: any) { |
109 | | - // console.error("Execute External Query Error:", error); |
110 | | - // return null; |
111 | | - // } |
112 | | - // } |
113 | | - |
114 | 68 | private async executeRawQuery< |
115 | 69 | T extends Record<string, SqlStorageValue> = Record< |
116 | 70 | string, |
@@ -177,93 +131,4 @@ export class StarbaseDBDurableObject extends DurableObject { |
177 | 131 | } |
178 | 132 | }) |
179 | 133 | } |
180 | | - |
181 | | - // TODO: Hiding for now as it's not used in the current implementation |
182 | | - // private enqueueOperation( |
183 | | - // queries: { sql: string; params?: any[] }[], |
184 | | - // isTransaction: boolean, |
185 | | - // isRaw: boolean, |
186 | | - // operationQueue: any[], |
187 | | - // processNextOperation: () => Promise<void> |
188 | | - // ): Promise<{ result?: any; error?: string | undefined; status: number }> { |
189 | | - // const MAX_WAIT_TIME = 25000; |
190 | | - // return new Promise((resolve, reject) => { |
191 | | - // const timeout = setTimeout(() => { |
192 | | - // reject(createResponse(undefined, "Operation timed out.", 503)); |
193 | | - // }, MAX_WAIT_TIME); |
194 | | - |
195 | | - // operationQueue.push({ |
196 | | - // queries, |
197 | | - // isTransaction, |
198 | | - // isRaw, |
199 | | - // resolve: (value: any) => { |
200 | | - // clearTimeout(timeout); |
201 | | - |
202 | | - // resolve({ |
203 | | - // result: value, |
204 | | - // error: undefined, |
205 | | - // status: 200, |
206 | | - // }); |
207 | | - // }, |
208 | | - // reject: (reason?: any) => { |
209 | | - // clearTimeout(timeout); |
210 | | - |
211 | | - // reject({ |
212 | | - // result: undefined, |
213 | | - // error: reason ?? "Operation failed.", |
214 | | - // status: 500, |
215 | | - // }); |
216 | | - // }, |
217 | | - // }); |
218 | | - |
219 | | - // processNextOperation().catch((err) => { |
220 | | - // console.error("Error processing operation queue:", err); |
221 | | - // }); |
222 | | - // }); |
223 | | - // } |
224 | | - |
225 | | - // TODO: Hiding for now as it's not used in the current implementation |
226 | | - // private async processNextOperation( |
227 | | - // sqlInstance: any, |
228 | | - // operationQueue: OperationQueueItem[], |
229 | | - // ctx: any, |
230 | | - // processingOperation: { value: boolean } |
231 | | - // ) { |
232 | | - // if (processingOperation.value) { |
233 | | - // // Already processing an operation |
234 | | - // return; |
235 | | - // } |
236 | | - |
237 | | - // if (operationQueue.length === 0) { |
238 | | - // // No operations remaining to process |
239 | | - // return; |
240 | | - // } |
241 | | - |
242 | | - // processingOperation.value = true; |
243 | | - // const { queries, isTransaction, isRaw, resolve, reject } = |
244 | | - // operationQueue.shift()!; |
245 | | - |
246 | | - // try { |
247 | | - // let result; |
248 | | - |
249 | | - // if (isTransaction) { |
250 | | - // result = await this.executeTransaction(queries, isRaw); |
251 | | - // } else { |
252 | | - // const { sql, params } = queries[0]; |
253 | | - // result = this.executeQuery({ sql, params }); |
254 | | - // } |
255 | | - |
256 | | - // resolve(result); |
257 | | - // } catch (error: any) { |
258 | | - // reject(error.message || "Operation failed."); |
259 | | - // } finally { |
260 | | - // processingOperation.value = false; |
261 | | - // await this.processNextOperation( |
262 | | - // sqlInstance, |
263 | | - // operationQueue, |
264 | | - // ctx, |
265 | | - // processingOperation |
266 | | - // ); |
267 | | - // } |
268 | | - // } |
269 | 134 | } |
0 commit comments