Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
84 changes: 84 additions & 0 deletions src/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,90 @@ describe('StarbasePluginRegistry', () => {
expect(result.sql).toBe('SELECT * FROM users /* modified */')
})

it('should call each plugin beforeQuery exactly once per query', async () => {
await registry.beforeQuery({
sql: 'SELECT * FROM users',
params: [1],
})

expect(mockPlugin.beforeQuery).toHaveBeenCalledTimes(1)
})

it('should pass chained sql, params, dataSource and config to each plugin', async () => {
const dataSource = { source: 'internal' } as unknown as DataSource
const config = { role: 'admin' } as unknown as StarbaseDBConfiguration

class RewritePlugin extends StarbasePlugin {
async beforeQuery(opts: { sql: string; params?: unknown[] }) {
return { sql: `${opts.sql} WHERE id = ?`, params: [42] }
}
}

const first = new RewritePlugin('RewritePlugin')
const second = new TestPlugin('TestPlugin')
const firstSpy = vi.spyOn(first, 'beforeQuery')
const secondSpy = vi.spyOn(second, 'beforeQuery')

const chained = new StarbasePluginRegistry({
app: mockApp,
plugins: [first, second],
})

const result = await chained.beforeQuery({
sql: 'SELECT * FROM users',
params: [-1],
dataSource,
config,
})

expect(firstSpy).toHaveBeenCalledTimes(1)
expect(firstSpy).toHaveBeenCalledWith({
sql: 'SELECT * FROM users',
params: [-1],
dataSource,
config,
})
expect(secondSpy).toHaveBeenCalledTimes(1)
expect(secondSpy).toHaveBeenCalledWith({
sql: 'SELECT * FROM users WHERE id = ?',
params: [42],
dataSource,
config,
})
expect(result).toEqual({
sql: 'SELECT * FROM users WHERE id = ?',
params: [42],
})
})

it('should return the original query when no plugins are registered', async () => {
const empty = new StarbasePluginRegistry({ app: mockApp, plugins: [] })

const result = await empty.beforeQuery({
sql: 'SELECT 1',
params: [null],
})

expect(result).toEqual({ sql: 'SELECT 1', params: [null] })
})

it('should propagate errors thrown by a plugin beforeQuery', async () => {
class FailingPlugin extends StarbasePlugin {
async beforeQuery(): Promise<{ sql: string; params?: unknown[] }> {
throw new Error('Query rejected')
}
}

const failing = new StarbasePluginRegistry({
app: mockApp,
plugins: [new FailingPlugin('FailingPlugin')],
})

await expect(
failing.beforeQuery({ sql: 'DELETE FROM users' })
).rejects.toThrowError('Query rejected')
})

it('should call afterQuery on all plugins', async () => {
const result = await registry.afterQuery({
sql: 'SELECT * FROM users',
Expand Down
1 change: 0 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export class StarbasePluginRegistry {
dataSource: opts.dataSource,
config: opts.config,
})
await plugin.beforeQuery(opts)
sql = modified.sql
params = modified.params
}
Expand Down