Kysely

K

Kysely

Join the community to ask questions about Kysely and get answers from other members.

Join

help

query-showcase

announcements

building kysely makes the package size very large

hi. i'd be more than happy to figure this out because i like kysely a lot more than drizzle. the thing was that the package size was so much larger with drizzle. i made a repro here. when running bun build --minify --outfile, i get kysely to be 340.40kB whilst drizzle is 55.35kB, meanwhile the functional code besides the database is identical

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 ?

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(eb => eb .selectFrom("ads")...

Separating results of join into objects of each type

Hey guys, I'm curious if anyone knows a way to unmerge the results of a join into objects of both types. For example, if you have a table User and table Bio, and query a single row which joins those two, the result is an a flat object with keys from both tables. What I'd like to do is separate the result into a User object and Bio object. Solutions that work but I don't love: 1. manually extracting values from the object based on the current schema. (future changes to schema will break this, so I'd like to avoid that)...

Snippet compilation

Hi, is it somehow possible to get only part of the compiled code to use it as snippet in ORM? Or vice-versa is it possible to build only part of the code with type helpers and compile them without the parent query? For example I would like to get only the code for the header_index and header_order aliases: ```...
Solution:
There are many ways to do this, but here are couple of them: https://kyse.link/Ion4f

Creating an 'enum' type column

Hi all, Trying to re-create this MySQL in Kysely ``` ......
Solution:
Like this: ```ts await trx.schema .createTable("expected_available_songs")...

Error when destructuring QueryCreator in withRecursive

I'm new to Kysely and attempting to migrate a Next app from prisma to kysely. I've generated types, successfully run some simple queries, and now I'm experiencing an error (in the title) that I'm unsure how to debug with a more complex recursive query. Here's a playground link to demonstrate: https://kyse.link/ZD1EV Can anyone help point me in the right direction? I cobbled this query together based on other examples I found but I haven't found anyone else with this error....

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 like I can't do the dumb thing and just string concat the pieces together like so:
let q = sql.raw` noteFtsFv.rowid ` + sql.raw` in `
let q = sql.raw` noteFtsFv.rowid ` + sql.raw` in `
...

Multiple 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 fixed it by passing an array to a single values call instead, but this seems like a bug no? Either the types should reflect that values can no longer be called after it has been, or it should collect the values calls into one array?

using pgvector with Kysely

Following the example here: https://github.com/pgvector/pgvector-node?darkschemeovr=1#kysely But I get the error ERROR: column "embedding" is of type vector but expression is of type character varying Happy to provide code if necessary but there aren’t any differences from the tutorial ...
Solution:
// Replace `string` with the type you have in the table interface.
sql<string>`${sql.ref(column)} <=> ${toSql(value)}::vector(3)`
// Replace `string` with the type you have in the table interface.
sql<string>`${sql.ref(column)} <=> ${toSql(value)}::vector(3)`
...

Static/reusable custom window functions

Hello, I'm trying to buld a properly typed array_agg window function and would like to reuse the existing FunctionModule in a reusable way, so to have orderby, over, and so on. Is there a way to get the function module you get from fn.agg outside of an expression builder and without creating a puppet kysely instance, so to create an array_agg function and return what fn.agg return but properly typed? As an example:...

Is the Kysely main site down?

I just want to confirm if Kysely's main site is down
No description

How to loop an array and based on this create a dynamic CTE but preserve type ?

``ts for (const [references, columns] of referencesArray) { const newQb = qb.with(cte_${references}`, (wb) => { wb = wb.selectFrom(references).distinct().selectAll(references); for (const column of columns) {...
Solution:
It's impossible to do this while keeping the types. Not only because of Kysely but also because of typescript. - You can't change the type of a variable in typescript. - You can't turn a string into a string literal. - Even if your strings had string literal types, you couldn't loop over them...

On Conflict do update set ALL to be inserted columns

Here I am once again with a question for the on conflict bit in postgres. Would there be a way to automatically compose an update set statement for the on conflict do update bit with the information about all the columns we're going to insert, and automatically do an update with the excluded for those? Example here...

how to write not (array1 && array2)

Hi again, how to write following code? I would love to use not(expression) Expected result:...

Partial compile

Hello everyone, is it possible (somehow) to generate only part of the compiled query? I have an edge-case when want to use only part ("where" statement in my case) of the compiled query from kysely. Is there any short-way to do it? I definitely don't want to parse the result. Thanks Mike

Querying on jsonArrayFrom

Hi. So i need to fetch posts with comments from a database that are written by a set group of authors and have comments that are written by a set group of people. How would I accomplish this with sql? The best thing I can think of is fetch all posts and comments where the posts are written by the authors, and then do further clientside filtering on the comments - but is there a way to do this with the sql itself?

Why 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 instead?

Is there a way to keep full date precision?

It appears that selecting dates from Postgresql loses some precision, which wreaks havoc with my paging logic. Is there a way to get dates to have full precision on the JS side?