Kysely

K

Kysely

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

Join

help

query-showcase

announcements

Figuring out where in codebase an exception originated from

Hi all, This might be a stupid question, but what is the best way to figure out which .execute() call threw a SQL exception? I know ideally I should be doing error handling on each call, but I have a large codebase and I'm not sure where this error is coming from. Is there any easy way of figuring this out? Maybe a way to have a wrapper that prints out the query if malformed, or to print out the stack trace where the original call was executed? ``` You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1. Trace: Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1...

How to insert into all values (and columns) from another table / view?

I realize I could write the raw sql, however I was curious how I would go about creating the following insert statement using kysely. insert into <table> select * from <view_or_table> ...

Triggers vs stored procedures

Hey everyone, I don't think this is related to Kysely directly, so excuse me but this is the only SQL type of server I am in. I am wondering what is the best way to automatically update a table attribute based on when another attribute changes (in the same table or another table). For example, if I want to have the attributes updatedAt and lastLogin updated on their own from the Model below ```ts model User { id Int @id @default(autoincrement()) createdAt DateTime @default(now()) @db.Date updatedAt DateTime @db.Timestamp() ...

`Selectable`, `Insertable` and `Updateable` wrappers

Hey everyone, I am using this package: https://github.com/valtyr/prisma-kysely and I noticed that the output file does not include
export type Person = Selectable<PersonTable>
export type NewPerson = Insertable<PersonTable>
export type EditedPerson = Updateable<PersonTable>
export type Person = Selectable<PersonTable>
export type NewPerson = Insertable<PersonTable>
export type EditedPerson = Updateable<PersonTable>
...

How to sort by random?

Imagine this query: ```ts export default function findUsers() { return db...
Solution:
orderBy(sql`random()`)
orderBy(sql`random()`)
Should do it...

Using jsonArrayFrom with mariadb.

Hello, Madiadb does not have cast to JSON, and cannot reference nested subqueries. I'm a typescript newbie. 🙂 How can I rewrite this function to achieve a similar result with Mariadb?...

insert data into a table that has id of type bigserial

I created a table that has id(bigserial), cognitoId(varchar), subaccountInfo(json), I'm trying like this: ``` const obj = [{ nome: "nil", ...

Unable to connect to DB

Thank God there is a Discord server xD I been trying to make this work for like 2 hours and I can't get it to work ```ts import { Pool } from 'pg'; import {...
Solution:
There are a few approaches: https://kysely.dev/docs/recipes/schemas 1. WithSchemaPlugin to apply schema globally to all queries. 2. withSchema method to apply to specific queries on the fly....

Limit with a literal value

Hi folks, thanks for all your work on Kysely, it's great! I'm wondering how I can add a limit clause with a literal value, rather than a parameter. I have a strange use case, I'm querying a https://materialize.com/ database, which presents as postgres, but has different features. It also has some limitations, one of which is limit must be a literal value, not an expression or parameter. ...

Prisma-kysely generator converts boolean to a number

Hi, so using prisma-kysely, the generated type for a prisma Bool appears to be a number for some reason. Is this correct?
Solution:
Oh my mistake. mysql does in fact not have a bool type lol

Is there a way to not have as many Selectable<> generics?

Hi. I'm using prisma-kysely to generate my Kysely types, and it generates them with the Generated generic. I therefore need to use the Selectable generic to change it to the return type of what is selected when I want to use the type. Is there a way to avoid using it so often?

insertId return by transaction

the insertId returned by a transaction is of the last executed query, right?

How can I use Kysely migration inside NextJS 13 project?

I created a folder called kysely in root directory of next js then created a migrator.ts which contains migration code (Migrator) and created a folder inside kysely folder called migrations then I placed all migrations in it. I tried to run the migrator.ts file with ts-node but it's throwing some module import issues (cannot use import module outside a module) how can I run migration in nextjs
Solution:
Fixed the issue by replacing ts-node with tsx and also changed migrationFolder path to absolute path ```typescript const migrator = new Migrator({...

i am trying to select my id from type uuid

when i select all id from administrator table, it returns me status 200 and returns all id ``` const responseDb = await db .withSchema("schemaName")...

I am getting an error when entering an id with a uuid value

const uuid = uuidv4() await db .withSchema("schema_name") .insertInto("table_name")...
Solution:
Hey 👋 You should try casting it to uuid using sql template tag: ```ts...

How can i enable postgres Extensions (eg: uuid) with in Kysely Migration File

const query = sqlcreate extension "uuid-ossp";; how to execute this raw query inside migration file up method...
Solution:
Hey 👋 You can execute raw queries by passing your Kysely instance to .execute like this: ```ts...

Unable to insert geometry Postgres

Hey everyone 👋🏻 Greetings! I am trying to insert point data into my Postgresql table but getting error: parse error - invalid geometry error. Here is the code ...

Achieve Prisma-like nested selects

Hi. Is it possible to achieve Prisma-like nested selects with Kysely? I have the following code and it has the type below in Prisma, but I'm not sure how to achieve the same in Kysely without just doing an additional select * from CommitteeCountry where id in (select id from Committee) or something along those lines and then mapping the results in JS ```ts const data = await prisma.Committee.findMany({ include: { countries: true,...
Solution:
Hey 👋 Have you read this? https://kysely.dev/docs/recipes/relations...

Why is numUpdatedRows a BigInt?

Hi. I'd just like to ask as to why the number of updated rows in Kysely is a BigInt, as for all other ORMs that I've seen, it's just a normal number. Was there any reason for this decision?
Solution:
In reality it can't. That SSD would use up all the atoms in the universe 😄 It's more for consistency. The inserted row's ID in the InsertObject needs to be a bigint. Therefore all the others are too....