4 ms·
Surprised nobody has mentioned “Kysely” (https://kysely.dev https://kysely.dev). It is a query builder (not an ORM), that (ab)-uses the Typescript type system
by tlonny 2y ago
Surprised nobody has mentioned “Kysely” (https://kysely.dev https://kysely.dev).
It is a query builder (not an ORM), that (ab)-uses the Typescript type system to give you full type safety, intellisense, autocomplete etc.
Crucially it doesn’t require any build/compile step for your queries which is fantastic.
- cies 2y agoI had a look. It says it's type safe but I see strings everywhere. It seems the IDE can autocomplete the strings, but does that count as type safety nowadays? (How does the IDE do the autocomplete on strings? Will the compiler also catch "bad strings"?) Otherwise it looks much like jOOQ but without the "jOOQ generator" (which adds most of the type safety).
- moritzruth 2y agoTypeScript has literal types: https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types https://www.typescriptlang.org/docs/handbook/2/everyday-type...
- cies 2y agoWow did not know that. Looks quite confusing to me, but maybe I'm old or smth. I expect those `"a" | "b" | "c"` to be expressed as an enum, not strings and |s.
- simplify 2y agoThere's almost no material benefit to expressing as an enum. You get type checking either way.
- cies 2y agoI got that. It's just that I'm not used to strings being some kind of enums. But hey, why not!
- simplify 2y agoUnderstandably so, it's the impressive power of TypeScript's type system. No other mainstream language has arbitrary union types like this.
- 1propionyl 2y agoArbitrary unions of literals as types have been done before dynamically (Lisps, Prologs, Erlang) and done elsewhere statically(Python). The more distinctive features in TypeScript would be what you can to with keyof/typeof, indexed access types, conditional types and especially mapped types.
- simplify 2y agoTypeScript's unions are still impressive as you can union anything, not just literal types (TIL python has unions for literals, thanks). Out of curiosity, what do you mean by dynamically? Isn't that just a case statement, or am I misunderstanding?
- 1propionyl 2y ago> TypeScript's unions are still impressive as you can union anything, not just literal types (TIL python has unions for literals, thanks). You can do this in Python too..?
- xide 2y agoEvery string (literal type) passed to Kysely functions is strictly typed and the compiler will fail if something invalid is passed.
- joseferben 2y agothose are string literals and checked by the compiler. there is kysely-codegen which generates types based on the actual database schema.
- tlonny 2y agoTypescript can do some wacky stuff with strings. Not only will it give you autocomplete and prevent “bad strings”, you can write “foo as bar” in your select and typescript figures out the return type has a field “bar” by parsing the string at compile time!