Best place to initialize a DB connection #1940
Unanswered
ChristophP
asked this question in
Help & Questions
Replies: 1 comment
-
Hi Dido, have you tried something like that? async function getConnection() {
if (!globalThis.connection) globalThis.connection = await initConnection()
return globalThis.connection
}
That's determined by Vite. |
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
-
Hi,
Short story
We want to have HMR in our monorepo, therefore we want to include many deps in the SSR bundle (to get HMR https://vite.dev/config/ssr-options.html#ssr-external). That means moving DB connecting logic from the Express app into the Vike code.
What the best place to put things like connecting to a DB?
Places we've tried
renderer/+onBeforeRender
) on the top-level (so that it connect upon import) with a top level await -> gives an esbuild error saying that to top level await is not legal for the targetschrome87, safari 14, ....
(apparently it is trying to build for client which is surprising since+onBeforeRender
is a server only hook)onBeforeRender
-> not a great place since we need to connect before the requests comeonBoot
but it doesn't seem to be implemented yetWhat's the general recommendation for this sort of thing?
We appreciate some pointers.
Beta Was this translation helpful? Give feedback.
All reactions