3 ms·
> Eg, transform a schema type that describes my database tables into an easy-to-use query builder and data abstraction API with rich method chaining. TypeScrip
by spion 4y ago
> Eg, transform a schema type that describes my database tables into an easy-to-use query builder and data abstraction API with rich method chaining.
TypeScript can do this. It was done already halfway-well 6 years ago with way fewer features in TypeScript (https://github.com/brianc/node-sql/blob/master/lib/types.d.ts https://github.com/brianc/node-sql/blob/master/lib/types.d.t...) - today we can do much better.
- spion 4y agoFWIW I've given up the SQL query builder route and instead went with GraphQL query builder / Hasura route (https://typed-graphql-builder.spion.dev/ https://typed-graphql-builder.spion.dev/). Its technically codegen but only from the schema; queries are fully typed on-the-fly.
- jitl 4y agoI want to derive a Model type with methods from a Row type so I can write something like `const openDiscussions = blockModel.getContent({ where: c => c.getType() !== “page” }).andRecurse().getDiscussions({ where: d => !d.getResolvedAt() })` given an input row type like `type BlockRow = { id: BlockId, type: “page” | “text”, content?: BlockId[], discussions?: DiscussionId[] }; type DiscussionRow = { id: DiscussionId, resolvedAt?: Date }`
- spion 4y agoYou cannot use the lambdas operators combo without a preprocessor (like ttypescript) mainly because closure captures cannot be accessed from the function AST. You will also need to use a tuntime dsl to describe the schema, and then derive both the model classes and the model types from it (just because runtime parts cannot be derived from types) Other than that, the rest is very possible.
- camgunz 4y ago> You will also need to use a tuntime dsl to describe the schema This is kind of the whole ballgame right? How would you do any of this with types if the schema isn't defined until runtime?
- spion 4y agoI meant that since types are erased at compile-time in typescript, its much easier to make a schema description DSL that would then serve as the base to derive both the ORM DSL (runtime objects) and the types (compile time checks). If you go the types-first route, you will be forced to use proxies which can be more painful and constraining. The popular choice here is to use classes plus decorators, but its not the only choice https://typegraphql.com/docs/introduction.html#what https://typegraphql.com/docs/introduction.html#what The convenient bit here is that classes already have both a type and a runtime representation, and decorators are also available to attach any extra metadata necessary to that runtime representation