Kysely

K

Kysely

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

Join

help

query-showcase

announcements

How do I create a helper to be used on a select array of fields?

I want to create a toBool helper to convert from tinyint, however I'm lost on the type I need to pass to get autocomplete of the possible fields in the select ```ts import { sql } from 'kysely' ...
Solution:
I think checking equality to 0 would make this safer (and swapping true/false order), as tinyint can be negative.

How do I cast the type of a eb.fn('...') ?

I tried with eb.fn<string>("CONCAT", [....]) but it didn't worked
Solution:
If it doesn't work for some reason, you can use
eb.fn('now', []).$castTo<Date>()
eb.fn('now', []).$castTo<Date>()
...

cant use kysely-codegen

I followed this part https://kysely.dev/docs/getting-started of documentation till CODEGEN part when you can generate types based on actual database. https://kysely.dev/docs/generating-types this one. I installed needed packages: npm install --save-dev kysely-codegen, and I set database string in .env file. Next step is: kysely-codegen, but I get this error when I type it in the console: ``` kysely-codegen kysely-codegen : The term 'kysely-codegen' is not recognized as the name of a cmdlet, function, script file, o ...
Solution:
oh I feel stupid... npx kysely-codegen works

withSchema is marked as deprecated in ExpressionBuilder?

why and how to be replaced? How can be the schema defined dynamically (env)?
No description

Insert values from subquery

Solution:
comments on eb.lit :
To prevent SQL injections, only boolean, number and null values are accepted. If you need string or other literals, use sql.lit instead.
To prevent SQL injections, only boolean, number and null values are accepted. If you need string or other literals, use sql.lit instead.
...

$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.