From 48274b777ce9b0bf1507419f59a9cf45fe427687 Mon Sep 17 00:00:00 2001 From: Treasure520520 <203431274+Treasure520520@users.noreply.github.com> Date: Wed, 13 May 2026 08:56:13 +0800 Subject: [PATCH] fix: run plugin before query hooks once --- src/plugin.test.ts | 58 ++++++++++++++++++++++++++++++++++++++++++---- src/plugin.ts | 1 - 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/plugin.test.ts b/src/plugin.test.ts index 006e6df..89573e5 100644 --- a/src/plugin.test.ts +++ b/src/plugin.test.ts @@ -56,6 +56,55 @@ describe('StarbasePlugin', () => { expect(result).toEqual({ data: [] }) }) it('should apply beforeQuery modifications in order', async () => { + class PluginA extends StarbasePlugin { + beforeQuery = vi.fn( + async (opts: { + sql: any + params?: any + dataSource?: DataSource | undefined + config?: StarbaseDBConfiguration | undefined + }) => { + return { sql: `[A] ${opts.sql}`, params: opts.params } + } + ) + } + + class PluginB extends StarbasePlugin { + beforeQuery = vi.fn( + async (opts: { + sql: any + params?: any + dataSource?: DataSource | undefined + config?: StarbaseDBConfiguration | undefined + }) => { + return { sql: `[B] ${opts.sql}`, params: opts.params } + } + ) + } + + const pluginA = new PluginA('PluginA') + const pluginB = new PluginB('PluginB') + + const registry = new StarbasePluginRegistry({ + app: mockApp, + plugins: [pluginA, pluginB], + }) + + const result = await registry.beforeQuery({ + sql: 'SELECT * FROM users', + }) + + expect(pluginA.beforeQuery).toHaveBeenCalledTimes(1) + expect(pluginB.beforeQuery).toHaveBeenCalledTimes(1) + expect(pluginB.beforeQuery).toHaveBeenCalledWith( + expect.objectContaining({ + sql: '[A] SELECT * FROM users', + }) + ) + expect(result.sql).toBe('[B] [A] SELECT * FROM users') + }) + + it('should pass modified params to the next beforeQuery plugin', async () => { class PluginA extends StarbasePlugin { async beforeQuery(opts: { sql: any @@ -63,7 +112,7 @@ describe('StarbasePlugin', () => { dataSource?: DataSource | undefined config?: StarbaseDBConfiguration | undefined }) { - return { sql: `[A] ${opts.sql}`, params: opts.params } + return { sql: opts.sql, params: [...(opts.params ?? []), 'A'] } } } @@ -74,7 +123,7 @@ describe('StarbasePlugin', () => { dataSource?: DataSource | undefined config?: StarbaseDBConfiguration | undefined }) { - return { sql: `[B] ${opts.sql}`, params: opts.params } + return { sql: opts.sql, params: [...(opts.params ?? []), 'B'] } } } @@ -88,9 +137,10 @@ describe('StarbasePlugin', () => { const result = await registry.beforeQuery({ sql: 'SELECT * FROM users', + params: ['initial'], }) - expect(result.sql).toBe('[B] [A] SELECT * FROM users') + expect(result.params).toEqual(['initial', 'A', 'B']) }) }) @@ -139,7 +189,7 @@ describe('StarbasePluginRegistry', () => { sql: 'SELECT * FROM users', }) - expect(mockPlugin.beforeQuery).toHaveBeenCalled() + expect(mockPlugin.beforeQuery).toHaveBeenCalledTimes(1) expect(result.sql).toBe('SELECT * FROM users /* modified */') }) diff --git a/src/plugin.ts b/src/plugin.ts index f1879c1..a8be3ad 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -89,7 +89,6 @@ export class StarbasePluginRegistry { dataSource: opts.dataSource, config: opts.config, }) - await plugin.beforeQuery(opts) sql = modified.sql params = modified.params }