Live data from Hacker News

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

kysely.dev

41–50 of 65 posts

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

#41
for the db schema definition for this tool, where does the source of truth lives?

I'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't work like that.

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

#42

Another "almost" like SQL library that I've heard floating around: https://github.com/drizzle-team/drizzle-orm I think I've reached the limits of Prisma and embarrassingly I'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.

Been using Drizzle for the past couple of weeks and liking it a lot. Their relation feature is awesome and solves the n+1 problem very nicely.

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

#43
post #20

Doesn't seem like it performs result set nesting on joins? For example with the one to many of owner -> pet, I'd like the results to look something like `{ person: Person, pets: Pet[] }[]`. Knex doesn't do this either (afaict) - wrote a few "deep" queries with some convoluted lodash to group things up but mostly gave up and just live with raw resultsets. I guess I still prefer that to a full on ORM, but that's really…

Have you read this recipe? https://kysely.dev/docs/recipes/relations

Oh sure, knex had a github issue with a similar recommendation. Interesting approach, and I suppose json -> ts is a smooth transformation, thanks for the tip

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

#44
post #10
post #8

How does it compare to zapatos? https://jawj.github.io/zapatos/

We moved away from zapatos because the generated types are good only when selecting from single table. The moment we start selecting some subset of columns from a join of multiple tables, it is upto the developer to provide the right combination of pick and intersection of generated types and type safety takes a hit. The solution we use right now is ts-sql-query [1] which supports automatic type-safety for complex jo…

> which supports automatic type-safety for complex joins, CTEs, subselects etc. I evaluated Kysely as well but found the sql feature set coverage of ts-sql-query better at the time.

Kysely also provides "automatic type-safety for complex joins, CTEs, subselects etc.".

Gotta love how toxic some open-source maintainers are, bashing other libraries while self-promoting.

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

#45
post #41

for the db schema definition for this tool, where does the source of truth lives? I'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't work like that.

Kysely has community projects that offer Database interface auto-generation.

kysely-codegen can introspect all core dialects. prisma-kysely can generate straight from Prisma schemas.

We recommend using these in production apps. You could verify everything is aligned in your CICD workflows.

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

#46
post #23

If you want to run SQL, write SQL. The majority of your SQL will not require building dynamic where clauses or (dog forbid) dynamic joins. Having your SQL as plain statements with simple placeholders (to create safe prepared statements) is the saner approach. 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 d…

I can't think of a single project I've worked on that did not need dynamic where clauses almost immediately.

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

#47
post #17

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.

We love Knex, and are inspired by it. Sadly its not type-safe and reaching type-safety would require a rewrite.

We care a lot about our TypeScript compilation performance and Developer eXperience in general.

v0.25 introduced internal changes that doubled the possible complexity of CTEs, joins and conditional selects.

We also provide helper methods that "reset" the stack in a type-safe way.

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

#49
Hey :wave:

Igal from Kysely here (I did not create it, Sami did).

Our site is a constant WIP. We've recently revamped "Getting Started" and added a lot of examples. If you can't find something there, check the API docs site or JS docs in your IDE - everything is documented.

We respond quite fast on discord if you've got any questions.

Feel free to ask me questions here too. :)

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

#50
post #15
post #5

Earlier quoted context omitted.

If you use it with kysely-codegen, it generates the types from your DB schema, guaranteeing they match. And the strings are verified at compile time - you can’t typo a table name, field name, etc., it won’t compile. Plus, from an ergonomics POV, it integrates very well with auto-complete. It’s very typesafe IMO, more so than most libs that interact with the the DB, where you hand-define the schemas and can more easil…

The only nag I have with this (and it might just be a case of me doing things wrong) is that it seems you also have to create types for tables based on the operation you want to perform by wrapping them in the `Selectable`, `Insertable`, etc. Kysely types. Kinda wish kysely-codegen created all those types along with the base table types. Otherwise, it’s been working pretty well for me so far!

It was also requested in prisma-kysely recently.

Might be a good idea to check kysely-codegen's issues section and open an issue if it wasn't requested yet.

Post reply on HN