I think I'd need to see what it's doing

I think I'd need to see what it's doing to make a certain answer here. If you're wanting to do it from the Worker and RPC into the DO and execute SQL you could do it like this but I also can just be 0 coffee's deep this morning and misssing the question altogether 😅
No description
1 Reply
gruntlord6
gruntlord6•4mo ago
well without showing all the code in the file that I use to actually talk with it I will show the actual DO code. essentially I just have a /sql endpoint that we use through rpc. import { DurableObject } from "cloudflare:workers"; export class ShopifyStorage extends DurableObject { constructor(ctx: DurableObjectState, env: any) { super(ctx, env); } / * Execute a SQL query directly - similar to D1's prepare().bind().run() */ async run(query: string, ...params: any[]): Promise<{ success: boolean; error?: string; meta?: any }> { try { const cursor = this.ctx.storage.sql.exec(query, ...params); return { success: true, meta: { rowsRead: cursor.rowsRead, rowsWritten: cursor.rowsWritten } }; } catch (error) { return { success: false, error: error.message }; } } / * Execute a SQL query and return first result - similar to D1's prepare().bind().first() */ async first(query: string, ...params: any[]): Promise<{ result: any; error?: string }> { try { const cursor = this.ctx.storage.sql.exec(query, ...params); const results = cursor.toArray(); const result = results.length > 0 ? results[0] : null;
return { result }; } catch (error) { return { result: null, error: error.message }; } } / * Execute a SQL query and return all results - similar to D1's prepare().bind().all() */ async all(query: string, ...params: any[]): Promise<{ results: any[]; error?: string; meta?: any }> { try { const cursor = this.ctx.storage.sql.exec(query, ...params); const results = cursor.toArray();
return { results, meta: { rowsRead: cursor.rowsRead, rowsWritten: cursor.rowsWritten } }; } catch (error) { return { results: [], error: error.message }; } } /
* Execute raw SQL - for more complex operations */ async executeQuery(query: string, ...params: any[]): Promise<{ results?: any[]; result?: any; error?: string }> { try { const cursor = this.ctx.storage.sql.exec(query, ...params); const results = cursor.toArray(); const result = results.length > 0 ? results[0] : null;
return { results, result }; } catch (error) { return { error: error.message }; } } } I know thats not really a great way to do it so thats why I am hoping this sdk offers something better then what I am doing, since I have to have the workers do those things and not the DO itself

Did you find this page helpful?