Live data from Hacker News

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

kysely.dev

1–10 of 65 posts

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

#3
This is only partially type-safe. Having a few SQL keywords abstracted into methods doesn't deliver a lot of value. I'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.

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

#4
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/non-standard types. For example, using PostGIS with Prisma is a terrible experience, but fine with Kysely.

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.

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

#5
post #3

This is only partially type-safe. Having a few SQL keywords abstracted into methods doesn't deliver a lot of value. I'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.

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 easily make typos.

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

#6

For Java, JOOQ is great and offers better type safety than this. Additionally, JOOQ generates data model classes from the DB schema.

There’s a popular sister project, kysely-codegen, that lets you do the same (generate types from the DB schema).

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

#7
post #3

This is only partially type-safe. Having a few SQL keywords abstracted into methods doesn't deliver a lot of value. I'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.

With generated database types (we use zapatos) this is the most type safe query builder, the typing is extensive and very correct

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

#9
post #5
post #3

This is only partially type-safe. Having a few SQL keywords abstracted into methods doesn't deliver a lot of value. I'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.

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…

I didn't go deep into the docs, but the 'movie' on the homepage clearly shows string-based field name mappings so I instantly saw that as a red flag...

Nice to know it's implemented better though.

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

#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 joins, CTEs, subselects etc. I evaluated Kysely as well but found the sql feature set coverage of ts-sql-query better at the time.

I maintain a code-generator [2] for this project that can generate the table mappers from database schema similar to how zapatos.

We don't have as good support for lateral joins and deriving json from database though, which zapatos does really well.

[1] https://ts-sql-query.readthedocs.io/

[2] https://github.com/lorefnon/ts-sql-codegen

Post reply on HN