Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ ORM query interception allows you to intercept the high-level ORM API calls. The
```ts
db.$use({
id: 'cost-logger',
onQuery: async ({ model, operation, args, query }) => {
onQuery: async ({ model, operation, args, proceed }) => {
const start = Date.now();
const result = await query(args);
const result = await proceed(args);
console.log(`[cost] ${model} ${operation} took ${Date.now() - start}ms`);
return result;
},
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/client/client-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
const onQuery = plugin.onQuery;
if (onQuery) {
const _proceed = proceed;
proceed = () => onQuery({ client, model, operation, args, query: _proceed }) as Promise<unknown>;
proceed = () => onQuery({ client, model, operation, args, proceed: _proceed }) as Promise<unknown>;
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/runtime/src/client/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ type OnQueryHookContext<Schema extends SchemaDef> = {
args: unknown;

/**
* The query function to proceed with the original query.
* The function to proceed with the original query.
* It takes the same arguments as the operation method.
*
* @param args The query arguments.
*/
query: (args: unknown) => Promise<unknown>;
proceed: (args: unknown) => Promise<unknown>;

/**
* The ZenStack client that is performing the operation.
Expand Down
20 changes: 10 additions & 10 deletions packages/runtime/test/plugin/on-query-hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('On query hooks tests', () => {
} else if (ctx.operation === 'update') {
updateHookCalled = true;
}
return ctx.query(ctx.args);
return ctx.proceed(ctx.args);
},
});

Expand All @@ -61,7 +61,7 @@ describe('On query hooks tests', () => {
hooksCalled = true;
expect(ctx.model).toBe('User');
}
return ctx.query(ctx.args);
return ctx.proceed(ctx.args);
},
});
await expect(
Expand All @@ -84,7 +84,7 @@ describe('On query hooks tests', () => {
hooksCalled = true;
expect(ctx.model).toBe('User');
expect(ctx.operation).toBe('findFirst');
return ctx.query(ctx.args);
return ctx.proceed(ctx.args);
},
});
await expect(
Expand All @@ -106,9 +106,9 @@ describe('On query hooks tests', () => {
onQuery: async (ctx) => {
if (ctx.model === 'User' && ctx.operation === 'findFirst') {
hooksCalled = true;
return ctx.query({ where: { id: 'non-exist' } });
return ctx.proceed({ where: { id: 'non-exist' } });
} else {
return ctx.query(ctx.args);
return ctx.proceed(ctx.args);
}
},
});
Expand All @@ -132,11 +132,11 @@ describe('On query hooks tests', () => {
onQuery: async (ctx) => {
if (ctx.model === 'User' && ctx.operation === 'findFirst') {
hooksCalled = true;
const result = await ctx.query(ctx.args);
const result = await ctx.proceed(ctx.args);
(result as any).happy = true;
return result;
} else {
return ctx.query(ctx.args);
return ctx.proceed(ctx.args);
}
},
});
Expand All @@ -159,10 +159,10 @@ describe('On query hooks tests', () => {
onQuery: async (ctx) => {
if (ctx.model === 'User' && ctx.operation === 'create') {
hooksCalled = true;
await ctx.query(ctx.args);
await ctx.proceed(ctx.args);
throw new Error('trigger error');
} else {
return ctx.query(ctx.args);
return ctx.proceed(ctx.args);
}
},
});
Expand Down Expand Up @@ -194,7 +194,7 @@ describe('On query hooks tests', () => {
id: 'test-plugin',
onQuery: (ctx) => {
findHookCalled = true;
return ctx.query(ctx.args);
return ctx.proceed(ctx.args);
},
});

Expand Down
4 changes: 2 additions & 2 deletions samples/blog/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ async function main() {
},
}).$use({
id: 'cost-logger',
onQuery: async ({ model, operation, args, query }) => {
onQuery: async ({ model, operation, args, proceed }) => {
const start = Date.now();
const result = await query(args);
const result = await proceed(args);
console.log(`[cost] ${model} ${operation} took ${Date.now() - start}ms`);
return result;
},
Expand Down
Loading