TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Kysely, a type-safe SQL query builder for TypeScript

90 pointsby fernandohuralmost 2 years ago

20 comments

sdcoffeyalmost 2 years ago
We've been using this in production for several months, and I'm a huge fan. Unopinionated but incredibly helpful. Big ups to the authors!
评论 #36421254 未加载
yashapalmost 2 years ago
Have recently started using it, big fan, especially in concert with kysely-codegen (generate Kysely types from DB schema). The combo is similar to Prisma, but IMO better in practice - Prisma is too limited, too little support for any sort of custom&#x2F;non-standard types. For example, using PostGIS with Prisma is a terrible experience, but fine with Kysely.<p>Also, a query builder with generated types that match the DB schema has many of the advantages of an ORM, with IMO few of the disadvantages. I’m not spending so much time trying to figure out how to translate what I want to do in SQL into the ORM language, as it’s basically a thin, more type safe and composable layer over SQL.
评论 #36421285 未加载
bedersalmost 2 years ago
If you want to run SQL, write SQL.<p>The majority of your SQL will not require building dynamic where clauses or (dog forbid) dynamic joins.<p>Having your SQL as plain statements with simple placeholders (to create safe prepared statements) is the saner approach.<p>Not only can you pluck them into your favorite SQL tools and analyzers, but you will not be surprised by terribly performing queries, because you created them dynamically without understanding their complexity.<p>We&#x27;ve learned the hard lessons decades ago by misusing ORMs. While using alternative SQL syntax builders is avoiding many of those pitfalls, you will still inherit complexity by translating a SQL dialect to the builder pattern.<p>It is not worth it IMHO.
评论 #36421212 未加载
评论 #36421344 未加载
评论 #36421762 未加载
评论 #36421138 未加载
评论 #36421214 未加载
评论 #36422297 未加载
评论 #36422872 未加载
评论 #36421443 未加载
评论 #36421419 未加载
评论 #36421101 未加载
paraph1nalmost 2 years ago
How does it compare to zapatos?<p><a href="https:&#x2F;&#x2F;jawj.github.io&#x2F;zapatos&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;jawj.github.io&#x2F;zapatos&#x2F;</a>
评论 #36419409 未加载
评论 #36422915 未加载
tortealmost 2 years ago
Have used it for a smaller project for a bit now and I really like it as well. Concise code and I can still map it to actual SQL in my brain.<p>What has been a bit non-intuitive for me though is the expression builder since the latest major version of kysely. When writing queries with `OR` conditions it always takes me a while to wrap my head around it again. It is also challening to make this easily readable with lots of dynamic `OR` conditions and I usually end up with a wrapper function which returns the array for the statements passed into the `or(` block. Could be improved in my opinion, otherwise a great tool
评论 #36428573 未加载
paulbaresalmost 2 years ago
I am building squashql-js for a slightly different use case (database agnostic SQL-like Typescript query builder among others) but Kysely and pypika (for Python) have been a great source of inspiration.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;squashql&#x2F;squashql&#x2F;blob&#x2F;main&#x2F;documentation&#x2F;QUERY.md">https:&#x2F;&#x2F;github.com&#x2F;squashql&#x2F;squashql&#x2F;blob&#x2F;main&#x2F;documentation...</a>
评论 #36428602 未加载
bzzztalmost 2 years ago
This is only partially type-safe. Having a few SQL keywords abstracted into methods doesn&#x27;t deliver a lot of value. I&#x27;ve found some value in type-safe mappings between database fields and language fields, at least you can directly see the problems in your code if some entity class has been changed. String-based field references will only fail at runtime so you need 100% unit tests to refactor with confidence.
评论 #36419056 未加载
评论 #36419191 未加载
peterisdirsaalmost 2 years ago
For Java, JOOQ is great and offers better type safety than this. Additionally, JOOQ generates data model classes from the DB schema.
评论 #36428700 未加载
评论 #36419078 未加载
idbeholdalmost 2 years ago
See now this is what I thought that &quot;Selectric Typeballs&quot; post [1] was going to be. I think it would make for a better name than &quot;Kysely&quot; if I&#x27;m being honest.<p>[1] <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=36406352">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=36406352</a>
评论 #36420536 未加载
LewisJEllisalmost 2 years ago
Love Kysely, been rooting for it ever since it was first released. Paired with kysely-codegen, it&#x27;s my favorite TS &lt;&gt; SQL interface. Does just enough, but not too much.<p>Had lots of good experiences working with Knex.js over the years, and Kysely is the TS-native spiritual successor to Knex.
评论 #36423288 未加载
afavouralmost 2 years ago
Has anyone used this and compared to the types provided by Knex? I’m largely very happy with Knex but when you start dealing with subqueries and the like the types fall apart. Suspect this kind of thing is pushing TS types to their limit, though.
评论 #36421661 未加载
评论 #36422316 未加载
sickcodebruhalmost 2 years ago
I typed up a comment saying how much I’d love to drop this in alongside my Prisma code so I can keep using the ORM for simple things and have the option to drop into SQL as needed. It ended asking if anyone knew whether such a thing was possible. I decided to check the docs one more time and… here it is! <a href="https:&#x2F;&#x2F;github.com&#x2F;valtyr&#x2F;prisma-kysely">https:&#x2F;&#x2F;github.com&#x2F;valtyr&#x2F;prisma-kysely</a><p>I’m excited. I enjoy Prisma but SQL can be so expressive. Looking forward to trying this.
评论 #36421267 未加载
eevoalmost 2 years ago
Doesn&#x27;t seem like it performs result set nesting on joins? For example with the one to many of owner -&gt; pet, I&#x27;d like the results to look something like `{ person: Person, pets: Pet[] }[]`. Knex doesn&#x27;t do this either (afaict) - wrote a few &quot;deep&quot; queries with some convoluted lodash to group things up but mostly gave up and just live with raw resultsets.<p>I guess I still prefer that to a full on ORM, but that&#x27;s really the one missing feature I want from these SQL query builder libraries
评论 #36421240 未加载
threatofrainalmost 2 years ago
Another &quot;almost&quot; like SQL library that I&#x27;ve heard floating around:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;drizzle-team&#x2F;drizzle-orm">https:&#x2F;&#x2F;github.com&#x2F;drizzle-team&#x2F;drizzle-orm</a><p>I think I&#x27;ve reached the limits of Prisma and embarrassingly I&#x27;m thinking about ripping it out. The benefits of not having to constantly reference your DB schema and having IDE guidance is not matching up to the idiosyncrasy and incomplete DB support of Prisma.
评论 #36421948 未加载
jadboxalmost 2 years ago
Kysely looks interesting, although I really like being able to write the query directly so I&#x27;m able to test the raw query directly. PgTyped is an interesting library I&#x27;ve used in the past where queries can be written as regular template strings that gets checked and responses become types. <a href="https:&#x2F;&#x2F;pgtyped.dev&#x2F;docs&#x2F;ts-file" rel="nofollow noreferrer">https:&#x2F;&#x2F;pgtyped.dev&#x2F;docs&#x2F;ts-file</a>
ghnwsalmost 2 years ago
Fun fact: &quot;Kysely&quot; is &quot;query&quot; in Finnish.
评论 #36422439 未加载
igalklebanovalmost 2 years ago
Hey :wave:<p>Igal from Kysely here (I did not create it, Sami did).<p>Our site is a constant WIP. We&#x27;ve recently revamped &quot;Getting Started&quot; and added a lot of examples. If you can&#x27;t find something there, check the API docs site or JS docs in your IDE - everything is documented.<p>We respond quite fast on discord if you&#x27;ve got any questions.<p>Feel free to ask me questions here too. :)
programmarchyalmost 2 years ago
Currently using knex, but this looks like a nice improvement. Getting the types via codegen is brilliant.
igalklebanovalmost 2 years ago
Huge shoutout to our community, bloggers and youtubers!<p><a href="https:&#x2F;&#x2F;github.com&#x2F;kysely-org&#x2F;awesome-kysely">https:&#x2F;&#x2F;github.com&#x2F;kysely-org&#x2F;awesome-kysely</a>
garevealmost 2 years ago
for the db schema definition for this tool, where does the source of truth lives?<p>I&#x27;m trying to think what happens when a column gets deleted or added in the prod, ci, or dev db tier. Ideally those db schema changes should happen at the same time but real life doesn&#x27;t work like that.
评论 #36422233 未加载