Kysely

K

Kysely

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

Join

help

query-showcase

announcements

How to access error events -- Is there a best/good practice for error handling

I'm writing my methods and associated tests for db access and when trying to validate error events. I get a SQL event log but nothing is caught in my try-catch block. When I log the response it is either empty or undefined. *Example ```typescript...

`createTable` function with dynamic column array

Hey folks, I'm trying to create a create table function; I feel like this should be straight forward and just work. But, I'm obviously missing something. Please let me know if more details are required. Thank you for your time.  🙏 ```typescript...

Issue Getting TypeScript to work with nested query

Hi there 👋 . I'm experiencing an issue with getting typescript to play nice with Kysely. I should add that I'm still a newbie to Kysely and typescript in general. Here's the code: ```typescript await db.selectFrom((eb) => { return eb.selectFrom("faqs")...
No description

How can one make `jsonObjectFrom` respect nullability of underlying subquery?

Hello, I am getting a type error when using the jsonObjectFrom helper on a subquery where the base table type of a field is not null, but all fields returned by jsonObjectFrom are nullable. We are passing the eb with the jsonObjectFrom helper inline in our query and were wondering if there a way to specify that a field will not be null?...

Filtering data based on relationship value

Hello, I'm using subqueries to fetch and return related data from a resource table. I am using the jsonObjectFrom helper which is working perfectly. My question now is how can I filter the final data based on a field present in the relation data. For example if my main table is User and the related data is Account, how can I fetch Users where the account is active all in the same query? Im already sing the expression builder (eb) for the subquery, do I need to add a separate join expression...

How can I write a query that does multiple insertions at once?

I have an array of Ids that I want to assign to a single id in a "bridging" table for a many to many relationship: ie: singleId: id1 singleId: id2 singleId: id3...

How to "generate" or "store" a query result type?

Hello, I was wondering how can I extract or store the type resulting from a query so that I ca use said type in other parts of my code without it depending on a specific function or implementation location in code.

Migrations in transaction and self management

Hi, I have a questions regarding migrations: - Is it possible to run all migrations in one transaction? - If I want to use kysely migrations for only generating raw sql for another db migration framework (such as umzug), should I use compile()? - How to handle deprecated models? If I remove a column from a model how will this be supported by the typing? ...

Running a function during migration

I am looking to use Kysely with TimescaleDB, which is an extension of Postgres to support timeseries data. There are a few places where I know I won't have type support, and executing raw sql is fine for my purposes. However, the process to create a timeseries table invokes a specific function via SELECT e.g. : SELECT create_hypertable('conditions', 'time', 'location', 4); Since this is part of the table creation, I would like to make sure it happens during migration step. Is there a way to run raw sql as part of a migration?...

Updating JSON object value in column

Hello, Im trying to wrap my head around how to update a JSON column were I want to update the value not by replacing the whole object but by setting the specific field in the JSON using: ```sql set metadata = jsonb_set(metadata, '{root,nested}', '"value"'); ...

Kysely Geometry Parsing Plugin - Convert driver values to/from geojson

0 I've currently written a rough prototype for parsing geometry. Most orms/query builders allow you to provide a mapValueToDb & mapValueFromDb based on a column type. However, kyseley doesn't really seem to have that. I found transformQueryResponse inside a plugin example, so I've written one of those, of which I had to loop over every value (nested too) that could be a buffer and then parse it to geometry. So I've used this to handle parsing from DB to client, however transforming it back to the DB without knowing the column type was going to be near impossible....

Ad-Hoc select from table not in Kysely types

Hey, we are dynamically generating our KyselyDB types using the kysely-codgen cli, and want to know how one can ad-hoc use the selectFrom statement from a table not included in the generated types by typing it ourselves as we build the query?

Convert JSON array result into JS array

Hello, I am running an SQL query that returns data in this format since it is a PG JSON array: ``` { rows: [ {...

Destroying connection necessary?

Hello! If we are using pooling via pgbouncer and using kysely from our edge functions, is it necessary to explicitly destroy the kysely instance/connection at the end to the function? Im asking since at the moment if an error is thrown, the instance is never explicitly destroyed. In local dev we dont have pgbouncer so we connect directly to the DB. After a while during dev and testing, the DB give a too many connections error....

how to insert an array of strings

hey guys, I wanted to know how to store an array of strings with kysely, I created my table using migrations: ```...

Typescript Error when creating MySQL db pool

Hi Everyone, I'm getting the following error when creating a database: ``` kysely Type '() => Promise<Pool>' is not assignable to type 'MysqlPool | (() => Promise<MysqlPool>)'...

Migrate beyond 9 (to `10-...`)

Hey, I am trying to run a new migration called 10-passport but I am always getting this error:
Error: corrupted migrations: expected previously executed migration 2-userguilds to be at index 2 but 10 was found in its place. New migrations must always have a name that comes alphabetically after the last executed migration.
Error: corrupted migrations: expected previously executed migration 2-userguilds to be at index 2 but 10 was found in its place. New migrations must always have a name that comes alphabetically after the last executed migration.
...
No description

Transaction response time

Hey everyone, in my code I have a createUser(), createProfile(), createBusiness(), and createAddress() for this example. Currently, I have creaeUser() execute on it's own and then the other 3 free functions execute as one transaction. Which gives me the benefit of having a fast response time for when a user creates an account. (the loading screen is quick) but I don't know if that benefit outweighs the risk of having user records in the database without a profile, business, and/or address records - *if for some reason the transaction fails but the createUser() executed fine. ...

How to handle failed transactions

Hey everyone, this is not directly related to Kysely but I was wondering what is a common solution for handling failed transactions? - is it supposed to be logged somewhere so you can review it?