Lurker News

Comments (191) 32 threads

Earlier comments
ricksharp2 replies#
Nice! I built something similar - A set of scripts that generate SQL statements from typescript types to wrap a PostgresSql db.

It’s more a hybrid data structure where only relationships and certain fields become columns (which can be indexed and searched) and most of the other data becomes a Json column.
I wasn’t completely satisfied with the final results, but it does at least allow me to add additional data fields without having to migrate the db structure and to use Postgres sql to filter and page data.
I think the goal of being able to define a true database in typescript is great and I look forward to trying your code.
It’s also pretty cool if you are able to extract types from a string using the type system - that’s next level typescript wizardry.

ricksharp2 replies#
Thinking about this more. I think this project has some promising practical uses when combined with code generation from the typescript ast (which is what my project does).

Since the type system can parse the SQL strings, then it becomes possible to include those strings as a source for the ast. Then the code generator can use the typescript parser to parse all the types and the sql. From that it can generate all the required code to create the runtime queries.
I don’t like ORMs because they make it very difficult to write complex queries (and my own attempt did not solve that very well - I still had to manually type any complex queries). However, if this could essentially allow writing a complex query in raw sql and have it type-checked at code-time, it becomes easier to use. Then the code generator would convert that into a type-safe runtime library which solves 2 problems: 1. The poor editor performance of the type system inferring so many nested types. 2. Actually having a runtime that executes the queries.
The outstanding problem would be migrations...
Anyway, this is certainly a thought provoking project.

thrashh#
I find the ORM in SQLAlchemy pretty cool — I’ve really written complex queries as if I had witten it by hand but then finally mapped them into the ORM at the end with like a line of code.

However it was a learning curve to figure out how to do it at first, but I didn’t have to drop down to raw SQL

gfody1 reply#
my thoughts as well. compile-time checked arbitrary sql would defeat the purpose of so many abominable ORMs. make it work with DDL instead of sample data and you've really got something nice!

Continue this thread — 1 reply

johnghanks1 reply#
> I built something similar - A set of scripts that generate SQL statements from typescript types to wrap a PostgresSql db.

If you think these are similar I don't think you understand what this is / what it's actually doing.

ricksharp#
Yes, I only meant similar in the sense that it attempts to use typescript types as the primary source for the definition of the database schema.

I agree my wording could have been better.

dandare3 replies#
Could someone ELI5?

Actually, I wrote a lot of code in TypeScript and I know my SQL but I have absolutely no idea what is going on here.

alex_duf2 replies#
I'm not entirely sure, but I suspect it's one of these programming "joke" where you take a concept and bring it far enough that it becomes ridiculous.

So I'm guessing there's no real practical use case for this, and is a nice flex from the author.
It would be nice if that were made dead clear in the Readme, something like "Why? Because it's fun"

gfody#
strong typing for arbitrary sql queries is actually really nice and this is much better than stuff like sequelize or typeorm which seem sadistic in comparison
nsonha1 reply#
generating types from a swagger spec? Graphql? Or any kind of schema? Look up fsharp type providers.
alex_duf1 reply#
As far as I understand (and I might be wrong so please correct me), this can't be used to generate anything. Of course all the technologies you've listed are useful and brilliant but this isn't what I'm seeing.

It's more of an experiment of thoughts to generate types that are defined by the result of the SQL query, bending a compiler to a new strange behaviour that happens to be SQL like.

Continue this thread — 1 reply

tombh#
I think this is an exploration of the somewhat surprising fact that type systems can be, and often are, Turing Complete[1]. A very naive view of type systems is that they're merely the next level up from code comments, except with the annoying burden of having the power to prevent compilation.

As this project demonstrates type systems are far more than just glorified comments. Indeed, they are in fact a rigorous scaffolding that seemingly magically "intuit" the intention of code without having to run it. This project playfully explores the extent of Typescript's particular powers.
1. https://en.wikipedia.org/wiki/Turing_completeness

dandare1 reply#
This is fascinating! On one hand, as an old guard JS developer that upscaled to TS, I often find new TS syntax incomprehensible. On the other hand I am still slowly learning new concepts. Am I correct to assume these new concepts are imported from more advanced languages? Or are these invented in TS for TS?
nsonha#
This particular feature is invented in ts, other instances of ts inventing a completely new thing are conditional types and mapped types. F# has type provider which is similar in some of its usages but different enough i dont think they're related.
gitgud1 reply#
Wow, I hope the typescript compiler is ready for this kind of dynamic complexity...

    type Protocol = 'http' | 'https';
    type DomainExtension = 'com' | 'org' | 'net';
    type DomainName = 'example' | 'google';
    type Domain = `${DomainName}.${DomainExtension}`;
    type Path = string;
    type ApiEndpoint = `${Protocol}://${Domain}/${Path}`;
This would seemingly get out of hand very quickly...
ruffrey#
I work on a codebase with a lot of this type of code. So far the compiler is up for it. The only time it gets slow is using the Zod language for runtime validation.

Load more comments