Kysely

K

Kysely

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

Join

help

query-showcase

announcements

Hey, there a way to update an existing migration/table?

Hey all, I'm trying to add a new field to an existing table, but I don't know how can I do that. I can't add the new field in the existing migration 'cause when I run again I'm getting the error something like "Table exists...". So the think what I'm trying to do is something like "[timestamp]_update_users_table.ts" and add the new fields!...
Solution:
Forget it, stupid question, I have discovered
alterTable
alterTable
in the schema module 😊 you can do this
await db.schema.alterTable('users').addColumn('...')
await db.schema.alterTable('users').addColumn('...')
...

Does kysely convert Table names lowercase when it's building a query ?

When I try to run this code it tries to insert into "profile" table not "Profile": Note: I have only extracted important parts ```typescript...

Type of query result is {}[]

Hi all! Loving using Kysely at the moment, it's super easy to use. I ran into one problem, which I believe is probably a lack of understanding from myself around certain elements of TypeScript or Kysely itself. Here is the DB Schema from kysely-codegen:...
Solution:
It looks like the reassignment of query to itself was causing the issue. If I do something like this, the end result has the correct typing: ``` let queryBuilder; ...

Unable to compile project with the TypeScript compiler (tsc) v.5.1.6

Hey folks, apologies in advance in case I am missing something obvious. I've noticed something very strange. the tsc compiler which I rarely use (but still need sometimes nevertheless) is unable to compile because of some OnConflictBuilder error, which is way too cryptic for me to understand. I made a bare-minimum reproducible demo here:...
Solution:
I just released 0.26.0 that has the fix in it

How to reference the parent query when creating a subquery itself?

What if I want to use kysely to create a subquery with a literal reference only and use it in sequelize ORM as literal? // Here I want to generate the subquery with a reference to non-existing parent const kyselySubquery = ``` select...

Can't get filterWhere to work with joins from another table.

Hey everyone. I am trying to recreate the following SQL (simplified demo): ```sql SELECT "users"."id",...
Solution:
Well, nevermind, I replaced filterWhere with filterWhereRef and now it's working correctly.

is this uuid() correct in mysql?

export async function up(db: Kysely<any>): Promise<void> { await db.schema .createTable("Payment") .addColumn("id", "char", (col) => col.defaultTo(raw("UUID()")).primaryKey())...

aliases vs strict mode in tsconfig

I have a problem with tsconfig. When I have strict mode kysely does not find aliases. What is the "most safe strict" mode which still works with kysely?
Solution:
This is "not as intended" usage. Our internals treat keys defined in DB as a whole and when trying to figure out if an alias was used we compare against ${T} as {string}.

Kysely origin

I have to ask. Where does the “kysely” name come from…?🤷‍♂️ It means “sour” in my mother tongue 😂...

coalesce in where statement

I've found examples and documentation for coalesce in select however I still struggle how to use it in where statement. ```const published = new Date(); const tagId = -1; ...

Which approach is better for coalesce and similar functions?

The fn and raw looks to me familiar as in sequelize there is fn and literal. What is the right approach for using coalesce as follows? ... AND COALESCE("Article"."AuthorCode", '1ee15c10-606b-6b81-db96-00f59ee1f648') = COALESCE("Article2"."AuthorCode", '1ee15c10-606b-6b81-db96-00f59ee1f648')...

Single source of truth (defaultAlias-schema-tableName)

Hi, is there any way how to set default alias to schema.table mapping to have single source of truth for kysely as well as for existing ORM implementation? We have existing project with huge sequelize implementation and want to take advantage of kysely simplicity for some specific queries. We have existing database architecture mapping in the following object: type ModelName = string; interface TableLocation {...

What's the pattern for writing migrations exactly?

I'm not familiar with SQL migrations, so my question is how do we create the migration files, what rules should it follow ? As seen in the kysely's GitHub repo example, the migration files have dates, but those files aren't generated, so where did the dates come from ? Well, let's say I don't want the migration files to have dates then I just create separate files for migrations and name them whatever? Ok, then how do I run them? Do I run them everytime the program starts ? Just help me out here, the docs doesn't mention anything about migrations I think it just assumes you are familiar with them already.....

writing a transaction that calls functions

Hey everyone, I like what I've got going here but before I did this for my whole code base I just wanted to make sure I was on the right path ```ts export async function createUser(user: NewUser, trx: Transaction<DB>) { return db .insertInto('User') .values({...

how to search inside a json type column, I'm using postgresql

I looked in the documentation, and it talks about using the jsonObjectFrom function that is in the kysely lib, but I didn't find it, should I have done the function?

.withSchema() and raw SQL

when doing sql <ReturnType> SELECT some_function(param);.execute(db) the function is not found because it doesn't exist in the public schema, although db was created with .withSchema('myschema'). If I change the call to SELECT myschema.some_function(param);.execute(db) it still doesn't work, because the function references tables in myschema...

JSON in columns (postgresql)

Hey <a:YA_Wave:743393941369651201> I am completely new to using SQL databases and have a (hopefully) simple question. I want to store JSON inside a column and want to make certain keys not null and/or set a default value, how would I do that? In this case, I have a language column and want to set a default value for the locale key...

Escaped paramaters in raw sql

For a few queries I made postgreSQL functions, this is the current way of how I call this function: ``ts export async function getRandomAnswerByQuestionId({ questionId, answerLimit }: { questionId: string, answerLimit: number }) { return sqlSELECT * FROM get_random_answers(${sql.val(questionId)}, ${sql.val(answerLimit)})`.execute(databaseClient); }...
Solution:
That's fine, but you don't even need to use sql.val in this case https://kyse.link/?p=s&i=MEBVYcJHvb7Uj4c6chaI...

Should database tables mimic form fields?

Hey everyone, this question is not Kysely specific just wanna point that out don't know where to ask but here. I have a form for business registration that looks like this