Assign global context to Hono that can be used across #2966
Unanswered
simplenotezy
asked this question in
Q&A
Replies: 1 comment
-
Fixed it with ContextVariableMap: hono.use(async (c, next) => {
c.set('dataSource', await createDbClient(c.env));
await next()
});
// somewhere else:
hono.get('/test', (c) => {
const client = c.get('dataSource'); // pg client
});
// remember
declare module 'hono' {
interface ContextVariableMap {
dataSource: Client;
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
How can one assign a global context that can be used across the app?
For instance I wish to create a database client (using HYPERDRIVE), and then be able to access that in any router.
When using Apollo server integration for Cloudflare, this is how it's done:
https://github.com/apollo-server-integrations/apollo-server-integration-cloudflare-workers/blob/main/README.md?plain=1#L65-L67
Beta Was this translation helpful? Give feedback.
All reactions