K
Kyselybombillazo

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 ?
tzezar
tzezar•39d ago
I don't know if I understand correctly, but I am creating one instance of
export const db = new Kysely<DB>({
dialect,
})
export const db = new Kysely<DB>({
dialect,
})
and import it where I need to make a call. There is connection pool that node-poostgres handles automatically.
bombillazo
bombillazo•39d ago
Ok got it, no issues with max connections?
tzezar
tzezar•39d ago
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
tzezar
tzezar•39d ago
But with kysely it basically wraps up in:
const dialect = new PostgresDialect({
pool: new Pool({
connectionString: "",
max: 10
})
})
const dialect = new PostgresDialect({
pool: new Pool({
connectionString: "",
max: 10
})
})
Johan Grundberg
Johan Grundberg•37d ago
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.".
bombillazo
bombillazo•37d ago
Thanks for the confirmation
Igal
Igal•37d ago
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.
Want results from more Discord servers?
Add your server
More Posts
Is it possible to get the total count while fetching rows in a single query?I have a query which looks like this: ```typescript const { items, count } = await db .selectFrom(Separating results of join into objects of each typeHey guys, I'm curious if anyone knows a way to unmerge the results of a join into objects of both tySnippet compilationHi, is it somehow possible to get only part of the compiled code to use it as snippet in ORM? Or vicCreating an 'enum' type columnHi all, Trying to re-create this MySQL in Kysely ``` ... CREATE TABLE ...( ... members ENUM('femalError when destructuring QueryCreator in withRecursiveI'm new to Kysely and attempting to migrate a Next app from prisma to kysely. I've generated types, Is `sql.join` the best way to concatenate sql templates?I have some business logic that incrementally builds up a sql WHERE query as a string. It looks likeMultiple calls to values in insert statement?I just had a production bug where I was calling `.values` multiple times on a query object. I've fix0.27.3 is out. Didn't have time to write0.27.3 is out. Didn't have time to write release notes yet since it was a hotfix to support typescriusing pgvector with KyselyFollowing the example here: https://github.com/pgvector/pgvector-node?darkschemeovr=1#kysely But I Static/reusable custom window functionsHello, I'm trying to buld a properly typed `array_agg` window function and would like to reuse the eIs the Kysely main site down?I just want to confirm if Kysely's main site is downOn Conflict do update set ALL to be inserted columnsHere I am once again with a question for the on conflict bit in postgres. Would there be a way to auHow to use kysely in edge runtime (like NextJS 14 middleware)?Error: The edge runtime does not support Node.js 'crypto' module. Learn More: https://nextjs.org/dochow to write not (array1 && array2)Hi again, how to write following code? I would love to use not(expression) Expected result: ``` noPartial compileHello everyone, is it possible (somehow) to generate only part of the compiled query? I have an edgeQuerying on jsonArrayFromHi. So i need to fetch posts with comments from a database that are written by a set group of authorWhy does `.stream()` return a single result when I pass in a `chunkSize` option ?I'm guessing it returns `chunkSize` mount of rows for each iteration, but shouldn't it be an array iIs there a way to keep full date precision?It appears that selecting dates from Postgresql loses some precision, which wreaks havoc with my pag