Kysely

K

Kysely

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

Join

help

query-showcase

announcements

How to plugin column aliases with table prefix?

I am not familiar with kysely plugin creation. I am also not sure if my issue can be done with such a plugin. My aim is to have overload method like this:
.selectAll('t1', 'prefix_t1')
.selectAll('t1', 'prefix_t1')
...

Query building optimization question

Hello, Id like some input from Kysely advanced users about my approach to using the eb. If I have a conditional select query, is this efficiently using the eb object for my query? Is it ok to use multiple levels of ebs ? ```ts // some input args = { param: ['foo', 'bar']...
Solution:
Creating an expression builder instance is in the order of microseconds. Running the query is in the order of milliseconds or hundreds of milliseconds. You don't need to worry about expression builder instances unless you create a thousand of them per query.

Why is eb inferred as any in this update query?

I'm trying to do an update from values inserted in a CTE. But the update's set operation doesn't seem to properly infer a type for the eb variable in its expression builder. If I change the where clause to an eb callback, it does properly infer the type, which makes me concerned I'm not doing this correctly. Am I doing something wrong? Is this a minor typing bug? Here's the code example:...

Can you mix kysely with pg transactions?

For example: ``` try { await client.query('BEGIN') ...

Zero number omitted from parameters

I looks like the compile() works incorrectly with zero number when used as parameter - the parameter is omitted from the array of sql components. Not included in sql params: ``` // used in case/when...

Is there any way to postgres notify and listen with kysley?

I want to use pg.on but I would create a new instance connection just for that. It would be much better if I can use kysley for pg_notify listener? Could I build a plugin with the current plugin systems? is it possible? or I can already use raw sql query?

bool_or

Hi, How to use bool_or ? I am using it in case-when-then construction. https://www.postgresql.org/docs/8.4/functions-aggregate.html...

How to retrieve field name raising error in database?

I am using kysely, zod, expressjs. I need to return field error and display it to the end user in frontend form. However I am not sure how to do it with kysely. Code for backend is pretty simple: ``` const FormData = z.object({ name: z.string({ required_error: "name is required" }), });...

Using sql.lit

Hey I have a question about using sql.lit and using dates ```` const query = kyselyBase...

Any thoughts on how to move data migrations from knex to kysely?

I've been working on shifting my code over to kysely, and it's been fantastic so far. However, I'm not entirely sure how to restructure my migrations directory into Kysely while maintaining up/down state without some manual overhead. The documentation has great advice for setting up migrations from scratch, but not as much for moving an existing project to its structure. I plan to collect all existing table data into a new initial migration, so that makes this somewhat easier. But there are still challenges. The way I see it I have 2 options: 1. Put a hacky check in the initial migration to apply its change only if one of my tables is not present. 2. Manually create the kysely migration record in existing databases to note that the initial migration has been run....

Using a custom HTTP driver for Kysely

Hi. I want to use neondb as my database. The issue is that I can't create a PostgresDialect class instance from the default provided pool, as that uses websockets - which is not what I want. Is there a way to tell Kysely to use HTTP connections (such as with kysely-planetscale), or do I need to make the driver myself?
Solution:
Hey 👋 Have you tried the kysely-neon community dialect?...

How to order by with embeddings using pgvector?

I'm trying to get this to work: ```js const raw = db .selectFrom("embeddingChunks")...
Solution:
You can't have parameters inside strings. https://kyse.link/?p=s&i=mSBxXrsodd1aLwlEKulE

Recipe for generated types + JSON?

Hey all! I'm currently migrating my application from objection to Kanon + Kysely, and I'm having trouble figuring out a good solution for the JSON Data Types. I have zod objects built for all of my JSON, but I'm unsure of how to integrate these types in a clean way. As I see it, my best option is to import each Table's interface into a separate Database file. In that file, for each type that has a json object, omit the existing column type and union in the new column type. Of course, that results in unfortunately ugly type annotations. The other option loses basically all of the benefits of Kanon outside of the first generation. In this method, I stop using the type auto-generation, and simply edit the json values into the schema and manually update the kysely schema whenever I need to update....

Consensus on immutable vs. mutable result for .executeTakeFirst

Hi folks, love Kysely, thanks for your work. Is there a consensus on whether or not the query result for executeTakeFirst should be immutable? Or is this up to the dialect/not a concern of Kysely? I'm asking because I seem to have found an inconsistency with kysely-libsql where the result of executeTakeFirst is immutable, but this isn't the case for built-in dialects (checked Sqlite and Pg). ...
Solution:
The result is not meant to be immutable. It's up to the dialect and the underlying driver. Unless you use something like CamelCasePlugin, Kysely doesn't touch the result in any way. It just returns what the driver returns.

Relational table join question

Hey, just out of curiosity, does Kysely have any helpers for joining tables across a relational table? For example, if I have tables user and account joined via the user_account relational tables, if I use jsonArrayFrom from a query on the user table to get accounts, do I always need to do a join with the intermediary table or does Kysely provide any type of shortcut for this?

Raw SQL in select type to number

Hellow, I have the following sql expression in a select statement array:
sql<number>`(${sql.raw(precisionScore)} * 100)::NUMERIC(10,2)`
sql<number>`(${sql.raw(precisionScore)} * 100)::NUMERIC(10,2)`
but the return type of the column in the row is a string. How can I get the type to be a number?...
Solution:
The pg driver returns numeric type as a string. Use the pg-types package to configure the type.

Select by id?

With this code: ``` export const getProductById: RequestHandler = async (req, res, next) => { const { id } = req.params try {...
Solution:
That is because Kanel create a "special" type for the id

Why has the json_agg and to_json call functions not been released?

Hi Recently I have favored Kysely. I would like to implement a query that calls to_json using kysely....
Solution:
Hey 👋 We don't have a roadmap. It'll be released when we're ready to cut a version....

Why jsonArrayFrom converts a Generated<number> field into string | number?

Look at this pic
Solution:
Since you're not using table specifiers, are you sure some "parent" table doesn't also have those columns and you are actually selecting them?
No description

kysely uses prepared statements like knex?

im planning use rds proxy and prepared statements pin connection
Solution:
Hey 👋🏻 nope. you can use kysely as a standalone query builder and execute the sql using a 3rd party driver's prepared statement execution api....