diff --git a/README.md b/README.md index aa18a08f..77bdfe1f 100644 --- a/README.md +++ b/README.md @@ -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; }, diff --git a/packages/runtime/src/client/client-impl.ts b/packages/runtime/src/client/client-impl.ts index 75515b0d..72404590 100644 --- a/packages/runtime/src/client/client-impl.ts +++ b/packages/runtime/src/client/client-impl.ts @@ -379,7 +379,7 @@ function createModelCrudHandler onQuery({ client, model, operation, args, query: _proceed }) as Promise; + proceed = () => onQuery({ client, model, operation, args, proceed: _proceed }) as Promise; } } diff --git a/packages/runtime/src/client/plugin.ts b/packages/runtime/src/client/plugin.ts index 496d308b..7f087a5d 100644 --- a/packages/runtime/src/client/plugin.ts +++ b/packages/runtime/src/client/plugin.ts @@ -69,12 +69,12 @@ type OnQueryHookContext = { 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; + proceed: (args: unknown) => Promise; /** * The ZenStack client that is performing the operation. diff --git a/packages/runtime/test/plugin/on-query-hooks.test.ts b/packages/runtime/test/plugin/on-query-hooks.test.ts index 7bd80c98..42e19ec0 100644 --- a/packages/runtime/test/plugin/on-query-hooks.test.ts +++ b/packages/runtime/test/plugin/on-query-hooks.test.ts @@ -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); }, }); @@ -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( @@ -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( @@ -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); } }, }); @@ -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); } }, }); @@ -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); } }, }); @@ -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); }, }); diff --git a/packages/runtime/test/policy/client-extensions.test.ts b/packages/runtime/test/policy/client-extensions.test.ts index 25140cd2..f1f916b4 100644 --- a/packages/runtime/test/policy/client-extensions.test.ts +++ b/packages/runtime/test/policy/client-extensions.test.ts @@ -23,10 +23,10 @@ describe('client extensions tests for policies', () => { const ext = definePlugin({ id: 'prisma-extension-queryOverride', - onQuery: async ({ args, query }: any) => { + onQuery: async ({ args, proceed }: any) => { args = args ?? {}; args.where = { ...args.where, y: { lt: 300 } }; - return query(args); + return proceed(args); }, }); @@ -54,10 +54,10 @@ describe('client extensions tests for policies', () => { const ext = definePlugin({ id: 'prisma-extension-queryOverride', - onQuery: async ({ args, query }: any) => { + onQuery: async ({ args, proceed }: any) => { args = args ?? {}; args.where = { ...args.where, y: { lt: 300 } }; - return query(args); + return proceed(args); }, }); @@ -85,10 +85,10 @@ describe('client extensions tests for policies', () => { const ext = definePlugin({ id: 'prisma-extension-queryOverride', - onQuery: async ({ args, query }: any) => { + onQuery: async ({ args, proceed }: any) => { args = args ?? {}; args.where = { ...args.where, y: { lt: 300 } }; - return query(args); + return proceed(args); }, }); @@ -116,10 +116,10 @@ describe('client extensions tests for policies', () => { const ext = definePlugin({ id: 'prisma-extension-queryOverride', - onQuery: async ({ args, query }: any) => { + onQuery: async ({ args, proceed }: any) => { args = args ?? {}; args.where = { ...args.where, y: { lt: 300 } }; - return query(args); + return proceed(args); }, }); @@ -145,8 +145,8 @@ describe('client extensions tests for policies', () => { const ext = definePlugin({ id: 'prisma-extension-resultMutation', - onQuery: async ({ args, query }: any) => { - const r: any = await query(args); + onQuery: async ({ args, proceed }: any) => { + const r: any = await proceed(args); for (let i = 0; i < r.length; i++) { r[i].value = r[i].value + 1; } diff --git a/samples/blog/main.ts b/samples/blog/main.ts index ac6bee10..46cb754e 100644 --- a/samples/blog/main.ts +++ b/samples/blog/main.ts @@ -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; },