Skip to content

Commit 620c614

Browse files
committed
beforeQuery and afterQuery from registry for plugins
1 parent 3cbf69c commit 620c614

File tree

5 files changed

+61
-21
lines changed

5 files changed

+61
-21
lines changed

src/handler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export class StarbaseDB {
8787
plugins: this.plugins,
8888
})
8989
await registry.init()
90+
this.dataSource.registry = registry
9091

9192
this.app.post('/query/raw', async (c) =>
9293
this.queryRoute(c.req.raw, true)

src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,6 @@ export default {
177177
}),
178178
] satisfies StarbasePlugin[]
179179

180-
dataSource.plugins = plugins
181-
182180
const starbase = new StarbaseDB({
183181
dataSource,
184182
config,

src/operation.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,11 @@ async function beforeQuery(opts: {
5757
}): Promise<{ sql: string; params?: unknown[] }> {
5858
let { sql, params, dataSource, config } = opts
5959

60-
if (dataSource?.plugins?.length) {
61-
await Promise.all(
62-
dataSource.plugins.map(async (plugin: StarbasePlugin) => {
63-
const { sql: _sql, params: _params } =
64-
await plugin.beforeQuery(opts)
65-
sql = _sql
66-
params = _params
67-
})
68-
)
60+
if (dataSource?.registry) {
61+
const { sql: _sql, params: _params } =
62+
await dataSource?.registry?.beforeQuery(opts)
63+
sql = _sql
64+
params = _params
6965
}
7066

7167
return { sql, params }
@@ -81,13 +77,10 @@ async function afterQuery(opts: {
8177
let { result, isRaw, dataSource } = opts
8278
result = isRaw ? transformRawResults(result, 'from') : result
8379

84-
if (dataSource?.plugins?.length) {
85-
await Promise.all(
86-
dataSource.plugins.map(async (plugin: StarbasePlugin) => {
87-
result = await plugin.afterQuery(opts)
88-
})
89-
)
90-
}
80+
result = await dataSource?.registry?.afterQuery({
81+
...opts,
82+
result,
83+
})
9184

9285
return isRaw ? transformRawResults(result, 'to') : result
9386
}
@@ -100,7 +93,7 @@ function transformRawResults(
10093
// Convert our result from the `raw` output to a traditional object
10194
result = {
10295
...result,
103-
rows: result.rows.map((row: any) =>
96+
rows: result?.rows?.map((row: any) =>
10497
result.columns.reduce(
10598
(obj: any, column: string, index: number) => {
10699
obj[column] = row[index]

src/plugin.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,52 @@ export class StarbasePluginRegistry {
6969
throw e
7070
}
7171
}
72+
73+
public async beforeQuery(opts: {
74+
sql: string
75+
params?: unknown[]
76+
dataSource?: DataSource
77+
config?: StarbaseDBConfiguration
78+
}): Promise<{ sql: string; params?: unknown[] }> {
79+
let { sql, params } = opts
80+
81+
if (this.plugins?.length) {
82+
await Promise.all(
83+
this.plugins.map(async (plugin: StarbasePlugin) => {
84+
const { sql: _sql, params: _params } =
85+
await plugin.beforeQuery(opts)
86+
sql = _sql
87+
params = _params
88+
})
89+
)
90+
}
91+
92+
return {
93+
sql,
94+
params,
95+
}
96+
}
97+
98+
public async afterQuery(opts: {
99+
sql: string
100+
result: any
101+
isRaw: boolean
102+
dataSource?: DataSource
103+
config?: StarbaseDBConfiguration
104+
}): Promise<any> {
105+
let { result } = opts
106+
107+
if (this.plugins?.length) {
108+
await Promise.all(
109+
this.plugins.map(async (plugin: StarbasePlugin) => {
110+
result = await plugin.afterQuery({
111+
...opts,
112+
result,
113+
})
114+
})
115+
)
116+
}
117+
118+
return result
119+
}
72120
}

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { StarbaseDBDurableObject } from './do'
2-
import { StarbasePlugin } from './plugin'
2+
import { StarbasePlugin, StarbasePluginRegistry } from './plugin'
33

44
export type QueryResult = Record<string, SqlStorageValue>
55

@@ -56,7 +56,7 @@ export type DataSource = {
5656
context?: Record<string, unknown>
5757
cache?: boolean
5858
cacheTTL?: number
59-
plugins?: StarbasePlugin[]
59+
registry?: StarbasePluginRegistry
6060
}
6161

6262
export enum RegionLocationHint {

0 commit comments

Comments
 (0)