> 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?
I 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
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
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.