TanStackT
TanStack12mo ago
7 replies
moderate-tomato

Server function dependency injection

What is the best way to DI something such as the db into the handler of a server fn? In remix for example we have getLoadContext which allows us to set up dependencies that get passed as the
context
argument to loaders and actions.

Coming from remix my expectation was:

const getUserById = createServerFn({ method: 'GET' })
  .validator(z.string())
   // context serves as server side DI container for db/services/etc.
  .handler(async ({ data, context }) => {
    return context.db.query.users.findFirst({ where: eq(users.id, data) })
  })
Was this page helpful?