Preferred way to share database connection pool #525
Unanswered
ravinggenius
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a single file where I instantiate a connection:
Then in my data access layer, I have functions that look like this:
Normally I wouldn't need the
await
keyword in front ofpool
, but becausecreatePool()
isasync
, it's required. This seems uncomfortable, but maybe not as bad as callingconst pool = await createPool(config.databaseUrl);
in every function that needs to use the database. I also tried using top-levelawait
insrc/library/_/datastore.ts
when definingpool
, but that was more trouble. This is a Next app by the way.It seems my options are as follows:
pool
in centraldatastore.ts
, then import andawait
it to use (as shown above)createPool()
in every model function as needed. this seems weird, because generally each function is independent of the others. most only run a single query, but some have a couple in a single transaction.async
getter function indatastore.ts
, then import and call it everywhere. this getter would in turn callcreatePool()
. this is functionally the same as the second option, but it gives me a chance to simplify it a bit.What is the preferred approach? Is there something I might have missed that would be better?
Beta Was this translation helpful? Give feedback.
All reactions