Skip to content

Commit 3831052

Browse files
committed
After query event hook
1 parent 45b5909 commit 3831052

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ build/Release
5353

5454
node_modules/
5555
jspm_packages/
56+
.pnpm-lock.yaml
57+
\*.package-lock.json
5658

5759
# Snowpack dependency directory (https://snowpack.dev/)
5860

src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { handleApiRequest } from "./api";
1313

1414
const DURABLE_OBJECT_ID = 'sql-durable-object';
1515

16-
interface Env {
16+
export interface Env {
1717
AUTHORIZATION_TOKEN: string;
1818
DATABASE_DURABLE_OBJECT: DurableObjectNamespace;
1919
REGION: string;
@@ -35,7 +35,7 @@ enum RegionLocationHint {
3535
ME = 'me', // Middle East
3636
}
3737

38-
export class DatabaseDurableObject extends DurableObject {
38+
export class DatabaseDurableObject extends DurableObject<Env> {
3939
// Durable storage for the SQL database
4040
public sql: SqlStorage;
4141

@@ -89,7 +89,7 @@ export class DatabaseDurableObject extends DurableObject {
8989
false,
9090
false,
9191
this.operationQueue,
92-
() => processNextOperation(this.sql, this.operationQueue, this.ctx, this.processingOperation)
92+
() => processNextOperation(this.sql, this.operationQueue, this.ctx, this.processingOperation, this.env)
9393
);
9494

9595
return response;
@@ -127,7 +127,7 @@ export class DatabaseDurableObject extends DurableObject {
127127
true,
128128
isRaw,
129129
this.operationQueue,
130-
() => processNextOperation(this.sql, this.operationQueue, this.ctx, this.processingOperation)
130+
() => processNextOperation(this.sql, this.operationQueue, this.ctx, this.processingOperation, this.env)
131131
);
132132

133133
return createResponseFromOperationResponse(response);
@@ -147,7 +147,7 @@ export class DatabaseDurableObject extends DurableObject {
147147
false,
148148
isRaw,
149149
this.operationQueue,
150-
() => processNextOperation(this.sql, this.operationQueue, this.ctx, this.processingOperation)
150+
() => processNextOperation(this.sql, this.operationQueue, this.ctx, this.processingOperation, this.env)
151151
);
152152

153153
return createResponseFromOperationResponse(response);
@@ -242,7 +242,7 @@ export class DatabaseDurableObject extends DurableObject {
242242
false,
243243
false,
244244
this.operationQueue,
245-
() => processNextOperation(this.sql, this.operationQueue, this.ctx, this.processingOperation)
245+
() => processNextOperation(this.sql, this.operationQueue, this.ctx, this.processingOperation, this.env)
246246
);
247247

248248
ws.send(JSON.stringify(response.result));

src/operation.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createResponse } from './utils';
2+
import { Env } from './index';
23

34
export type OperationQueueItem = {
45
queries: { sql: string; params?: any[] }[];
@@ -19,7 +20,13 @@ export type RawQueryResponse = {
1920

2021
export 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
}

wrangler.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class_name = "DatabaseDurableObject"
2424
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#migrations
2525
[[migrations]]
2626
tag = "v1"
27-
new_sqlite_classes = ["DatabaseDurableObject"] # Array of new classes
27+
new_sqlite_classes = ["DatabaseDurableObject"]
2828

2929
[vars]
3030
AUTHORIZATION_TOKEN = "ABC123"

0 commit comments

Comments
 (0)