Kysely

K

Kysely

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

Join
tzezar
tzezar4/9/2024

newbie need help with json_build_object

Hi! Just starting with kysely and encountered problem I cant solve. I want to build raw query with kysely builder...
JSON_BUILD_OBJECT('id', unit.id, 'name', unit.name) AS unit
JSON_BUILD_OBJECT('id', unit.id, 'name', unit.name) AS unit
this part gives me trouble. I would be grateful for your help...
Solution:
Here you go https://kyse.link/DqXNv Note that the properties are nullable in the selected unit object because you're using left join. The types are correct. If you are sure there's a unit for each sku then use innerJoin instead....
virk
virk4/8/2024

Solved: Exporting query builder classes

I saw this PR https://github.com/kysely-org/kysely/pull/763 where you guys have decided not to export classes as they are private. Is there any specific reason for which you don't want to make these classes public? Because if you change the public API of these classes, then anyways the query builder will be impacted by the change....
oof2win2
oof2win24/7/2024

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
bombillazo
bombillazo4/2/2024

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 ?
Adophilus
Adophilus4/2/2024

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")...
Shawn Long
Shawn Long3/31/2024

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)...
mike
mike3/26/2024

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
ohmi
ohmi3/23/2024

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")...
kalil
kalil3/12/2024

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....
AlexErrant
AlexErrant3/11/2024

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 `
...
djMax
djMax3/11/2024

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?
Jake
Jake3/7/2024

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)`
...
Kristian Notari
Kristian Notari3/6/2024

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:...
Adophilus
Adophilus3/6/2024

Is the Kysely main site down?

I just want to confirm if Kysely's main site is down
No description
Kristian Notari
Kristian Notari2/28/2024

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...
mike
mike2/23/2024

how to write not (array1 && array2)

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

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
oof2win2
oof2win22/18/2024

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?
bun
bun2/15/2024

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?