Kysely

K

Kysely

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

Join

help

query-showcase

announcements

$if with sql.raw doesn't compile

Hey, I have updated my kysely version after I deleted node_modules and npm installed and I am getting an error for the following code: const filterWhereQuery = sql.raw(LOWER("${dimensionName}") LIKE '%${filter?.toLowerCase()}%'); ...
Solution:
Hey 👋 Try providing <SqlBool> generic to your sql template tag....

ORDER BY in agg function.

Hi, I would like to represent something like
json_agg(table ORDER BY column) as values
json_agg(table ORDER BY column) as values
in kysely. How would I go about doing this?...

Binary fields in mySQL

I have a schema with ids stored as BINARY (sucks I know). Is it possible to write a plugin to convert them to string when receiving?
Solution:
You can use the typeCast config of mysql2 ```ts typeCast: function (field, next) { if (field.type === 'TINY' && field.length === 1) {...

jsonb_build_object and raw sql possibilities

is possible to do this query without use sql.raw? `` sql<string>jsonb_build_object(${sql.join([ sql.raw('hair_type',coalesce (dog.hair_type, dog_breed.hair_type)), sql.raw('ear_type',coalesce (dog.ear_type, dog_breed.ear_type)),...

ts_rank in select

still not possible? ```typescript .select([ 'finance_reimbursement_requests.reimbursement_request_id',...
Solution:
For future readers, when using sql template tag, or ExpressionBuilder within a select, you gotta alias it using the as method.
sql<number>`ts_rank(to_tsvector('english', users.email, websearch_to_tsquery('R2023-185'))`.as('rank'),
sql<number>`ts_rank(to_tsvector('english', users.email, websearch_to_tsquery('R2023-185'))`.as('rank'),
...

How to convert this raw query to kysely?

```sql UPDATE finance_reimbursement_approval_matrix SET has_approved = true WHERE approval_matrix_id = 'c78f11f0-7014-47cf-82b7-838a43b34b20' AND reimbursement_request_id IN (...

Converting a more complex query to Kysely

I wrote this query for my database, and I've been trying to convert it to use Kysely but can't seem to. I'd like to figure it out (rather than just use string SQL) The query: ``` SELECT...

Error "isSelectQueryBuilder" while using doUpdateSet on conflict

Hello, I get an error when dealing with an update object factory for doUpdateSet expression during an on conflict statement for an insert. From my experiments, if the select type of the column is different (not assignable to) the insert/update types than something breaks at the type level and gives me an error Repro: https://kyse.link/?p=s&i=Wt7lre2AUUH7EdAInOja...

New type error in 0.26.3

I recently upgraded from 0.26.1 to 0.26.3 and I'm getting a type error trying to call a reusable expression builder that used to work. ``` export function isOwner() { return (eb: ExpressionBuilder<DB & Record<"r", MzCatalogMzRoles>, "r">) => eb...

what is this error, i started getting it randomly

``` error: syntax error at or near ")" at /Users/username/Workspace/company/project/node_modules/.pnpm/[email protected]/node_modules/pg/lib/client.js:526:17 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async PostgresConnection.executeQuery (file:///Users/username/Workspace/company/project/node_modules/.pnpm/[email protected]/node_modules/kysely/dist/esm/dialect/postgres/postgres-driver.js:69:28)...

Kysely doesn't work in jest/test environment

For some reason Kysely does not work when I'm running test via Jest. I have a simple Express application and all db.selectFrom(... commands just times out. I tried to log things via: ``` log(event) { console.log(event);...

Is there a plan to add "NULLS NOT DISTINCT" (postgresql v15) option to unique constraints/indexes?

In postgresql version 15.0 "NULLS NOT DISTINCT" statement was introduced. How to include this statement? BTW in version 16 it was disallowed for primary keys.

How to work with window function ROW_NUMBER correctly?

How to type correctly something like this? I struggle with the ROW_NUMBER... `` const query = WITH ranked_licenses AS (...

executeTakeFirst does not add limit(1). Why?

If my understanding is correct, there is never any reason not to limit(1) when you do an executeTakeFirst.

How do you formulate a custom type argument?

I would like to pass a record type to a postgres function with kysely. How do I do that? I get "malformed record literal" when I try the obvious things. I'm sure I can raw-sql it together but I'd like to do it as properly as possible.

How do I type the arguments to a function?

I am calling a stored proc with parameters (we have very few, but some make the client's life a lot easier and make transactions shorter). I get an error that it can't find the function given the types:
function my_favorite_function(unknown, unknown, unknown)
function my_favorite_function(unknown, unknown, unknown)
...

InsertInto with all default values?

How do I run the equivalent of
INSERT INTO test DEFAULT VALUES returning
INSERT INTO test DEFAULT VALUES returning
in kysely?

Can this query translate to kysely?

``sql sqlSELECT frr.reimbursement_request_id, frr.reference_no, frrt.request_type,...

Deferred Join

Hi - I'm experimenting with adding deferred join capability (as described here: (https://planetscale.com/learn/courses/mysql-for-developers/examples/deferred-joins) to a limit/offset query. The premise is to apply the order by, limit and offset values to the inner join query but I'm having trouble finding a way syntactically to apply this to the inner join. Do I need to use the sql escape hatch? If so, I'm also struggling with where I'd apply that too! Any pointers appreciated as always, thanks...

Stumped - sqlite kysely no data from select query

I have an electron app with node-sqlite3 and kysely. I'm able to post data, but for some reason select comes up empty. I feel like I'm missing something low-level here. Thank you in advance. I've been banging my head on this off and on for 2 days. db.ts ```ts import sqlite3 from 'sqlite3';...