TanStackT
TanStack4w ago
2 replies
primary-violet

Db context in middleware

I'm wondering if there's a more efficient way to add db to middleware. I get an error if I attempt to move the createDb outside of the function to module level

import { createMiddleware } from '@tanstack/react-start'

export const dbMiddleware = createMiddleware({ type: 'function' }).server(
async ({ next }) => {
const { createDb } = await import('@/db')
const db = await createDb()

return next({
context: { db },
})
},
)

export * from './db/schema'
Was this page helpful?