Kysely setup in monolith API
Hello, we have a basic http server with a basic router and a PostgreSQL database. We're wondering what is the proper setup to use the Kysely client in our routes to make calls to the database. Should there be a singleton client that is used across routes, or do we need to instantiate a kysely client on every request ?
8 Replies
I don't know if I understand correctly, but I am creating one instance of and import it where I need to make a call. There is connection pool that node-poostgres handles automatically.
Ok got it, no issues with max connections?
Well we have used it in several projects and in our case there were no problems. Btw if you are tempted by postgresjs with its "speed" do some tests, because I checked the performance and in theory the slower pg was about 10% faster + the average time of access to the database was lower
and here you got more technical info about pools
But with kysely it basically wraps up in:
A bit late here but according to the docs the singleton approach seems 100% correct assuming you have a single database. From the docs: "You should create one instance of Kysely per database using the Kysely constructor. Each Kysely instance maintains it's own connection pool.".
Thanks for the confirmation
I don't like that sentence.
There are nuances.
You could pass the same
postgres
/mysql
pool instance to different Kysely
instances and everything would be fine if you have a new Kysely
instance per request. We do some caching of kysely connection instances under the hood that might make it worth "singleton'ing" (memory / time wise), dunno.
In mssql
we do manage a pool per Kysely
instance (we had to 😦 ), so you better singleton.