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
58 changes: 54 additions & 4 deletions src/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,63 @@ 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
params?: any
dataSource?: DataSource | undefined
config?: StarbaseDBConfiguration | undefined
}) {
return { sql: `[A] ${opts.sql}`, params: opts.params }
return { sql: opts.sql, params: [...(opts.params ?? []), 'A'] }
}
}

Expand All @@ -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'] }
}
}

Expand All @@ -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'])
})
})

Expand Down Expand Up @@ -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 */')
})

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