5 ms·
If you like this syntax and work with TypeScript and Knex, you can checkout my typed-knex library: https://github.com/wwwouter/typed-knex https://github.com/www
by hebrox 7y ago
If you like this syntax and work with TypeScript and Knex, you can checkout my typed-knex library: https://github.com/wwwouter/typed-knex https://github.com/wwwouter/typed-knex
All feedback is welcome :)
- jeswin 7y agoBecause there is no built in support to create expression trees from query expressions, your library is forced to re-implement common query patterns with a custom API. The result is not as clean as C#. And there is some loss of type-safety (The 'SUM' in the groupBy() example). An alternative approach could be to parse real JS expressions using babel (at runtime) - essentially giving you the same flexibility as C#. So you could allow users to write: typedKnex.query(User).where(u => u.id > 100 && u.city === "Calicut")
- recursive 7y ago> Because there is no built in support to create expression trees from query expressions, your library is forced to re-implement common query patterns with a custom API. Well, it could convert the "delegates" back to strings, and re-parse an AST from that. Not that I'm volunteering to write it.
- jeswin 7y agoThat works. An alternative could be to do it during build with a babel based tool. Earlier, I wrote a tool called chimpanzee[1] to parse such ASTs into SQL. Not easy (but not too hard either); there are so many different ways to express something in JS. Here's an example (using chimpanzee) which checks if an expression is doing a sort (on an array/table) and to extract sort fields and order. https://github.com/jeswin-unmaintained/isotropy-ast-analyzer-db/blob/master/src/schemas/sort.js https://github.com/jeswin-unmaintained/isotropy-ast-analyzer... [1] chimpanzee: https://github.com/jeswin/chimpanzee https://github.com/jeswin/chimpanzee