Best place to initialize a DB connection #1940
Unanswered
ChristophP
asked this question in
Discussion
Replies: 1 comment 1 reply
|
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. |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
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+onBeforeRenderis a server only hook)onBeforeRender-> not a great place since we need to connect before the requests comeonBootbut it doesn't seem to be implemented yetWhat's the general recommendation for this sort of thing?
We appreciate some pointers.
All reactions