Kysely

K

Kysely

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

Join

help

query-showcase

announcements

Asserting type of .countAll() (MySQL)

I've noticed that the return type from the result of aggregation functions like countAll() and sum() are string | number | bigint. I assume this is in-case the number returned is too large to be stored in a JS "number." In my case, it appears to be being returned as strings. Is it possible to assert it as a number so that I don't need to wrap every query in a parseInt(result as string, 10)?...
Solution:
nvm got it ```ts await db .selectFrom("player_stats") .where("last_active", ">", dateThreshold)...

Coalesce return empty array

Hey is there a way to return an empty array rather than null when an array column is empty? I already tried ``ts .select(coalesce("categories",sqlARRAY[]::"CollectionCategory"[]`).as("categories"))...

kysely-codegen for multiple databases

I have two MySQL 'databases' within the same instance. From the documentation, it doesn't look like the codegen has innate support for multiple databases with different schemas? I could codegen with the two different connection strings with a different --out-file, rename the exported interfaces, then manually update index.d.ts, but I was wondering if there's a better solution?...
Solution:
Hey 👋 type file & kysely instance per database is what I would go for....

Using MySQL functions in SELECT statement

Hi! Just trying to migrate over from Knex. Skimmed over the documentation but still unsure of how to replicate this query in Kysely, or might be looking in the wrong place? In Knex, I'd often have to use selectRaw for these SELECT a, b, UPPER(c) FROM x;...

Extract OrderBy TS Keys

Hey I have the following query ```ts const getBaseQuery = ({ offset, pageSize, slug }: GetBaseQuery) => db...

insert into with mix of static and table values

How might I execute an insert into that combines JS-side values with a select, like:
INSERT INTO some_table (a, b, c, d) SELECT o.a, o.b, 16, 'hello world' FROM other_table O WHERE somekey = 32;
INSERT INTO some_table (a, b, c, d) SELECT o.a, o.b, 16, 'hello world' FROM other_table O WHERE somekey = 32;
...

Migration error "TypeError: Cannot read properties of undefined (reading 'getExecutor')"

I am trying to run a migration using Kysely, and its returning this error: ``` file:///Users/brunocruz/code/look-manager/node_modules/kysely/dist/esm/raw-builder/raw-builder.js:61 : executorProvider.getExecutor(); ^...

where clause with length

How do I have a where clause with a length, e.g. select * from data where length(prodcode) = 3...

show generated sql

how can i see what the generated sql is for a kysely query? e.g. ``` result = await db.selectFrom("data") .select(['partner_cd', 'reporter_cd', 'product_cd', 'period'])...

Property does not exist on type

Hello. I'm trying to run the following query: ```javascript...
Solution:
Hey 👋 What kysely version are you using? cmpr (and other cool new expression builder stuff) is only available from 0.24.x...

Correct type definitions for function receiving builder

I have something like this and I was wondering if it's the correct way to type my helper function. ```ts // ----- Helper function I want to type const addCategoriesWhere = (...

Transform Postgres array into JS array

Hey, I was wondering if there's a helper or something to transform Postgres arrays {one, two} into JS arrays ['one', 'two']. Right I know the Postgres array is turned into a string when executing queries
Solution:
So you'd do
.select(sql<string[]>`categories::text[]`.as('categories'))
.select(sql<string[]>`categories::text[]`.as('categories'))
You can cast it to any type you want....
Message Not Public
Sign In & Join Server To View

Noob Question: SQL INSERT that combines static values with a SELECT statement

Hey Kysely community! I'm just getting into Kysely and I have a question about how I could run the following query with Kysely: ```sql INSERT INTO employees (employee_id, first_name, last_name, department_id) VALUES (1234, 'John', 'Doe', ...

Is there a way to execute an ExpressionBuilder?

Using the expression hasDogNamed example from the docs ``` const eb = expressionBuilder<DB, 'person'>() ...

Argument of type 'string' is not assignable to parameter of type 'DynamicReferenceBuilder<never>'.

The following statement, is giving me the error on the title
fn("round", [fn.min("ld.price_btc"), sql.lit(5)]).as("floor_price"),
fn("round", [fn.min("ld.price_btc"), sql.lit(5)]).as("floor_price"),
...

How do Date columns works?

I am learning Kysely from the website, at the getting started section I can see the following line:
modified_at: ColumnType<Date, string | undefined, never>
modified_at: ColumnType<Date, string | undefined, never>
...

converting result.insertedId to number

when using INT as PK i can safe convert result.insertedId from bigInt to Int using Number(result.insertedID)?

Advice on building plugin for working with parameters

I am currently working on a driver for YDB https://github.com/Gaspero/kysely-ydb YDB dialect expects query parameters to be explicitly declared as a part of the query using DECLARE statement (reference https://ydb.tech/en/docs/yql/reference/syntax/declare ) I want to build a custom plugin that would automatically add DECLARE statements for each parameter in query ...
Solution:
I am currently working on a driver for YDB https://github.com/Gaspero/kysely-ydb YDB dialect expects query parameters to be explicitly declared as a part of the query using DECLARE statement (reference https://ydb.tech/en/docs/yql/reference/syntax/declare ) I want to build a custom plugin that would automatically add DECLARE statements for each parameter in query ...

Mysql Query execution never ends

Hello, I have a question regarding my new integration of kysely in trpc. I wanted to port my prisma code to kysely and found that queries are indeed firing to db, but they never end the execution. Anyone knows why this is happening? Connection is taken from pool and mysql workbench also shows connection to db. Just the data is not returning. Same code is working with Prisma. Not sure where its going wrong. ...
Solution:
Hey 👋 Try importing createPool from mysql2 and not mysql2/promise....

How to do join with raw sql snippets?

How can I do this join with kysely? left join videos on videos.id = CAST(highlights_content.item AS INT) AND highlights_content.collection = 'videos' I tried using the provided sql helper but can't really figure out which parts I need to replace with raw SQL for this to be possible....
Solution: