Re:Write an SQL query builder in 150 lines of Python
by throwaway858 on Friday August 27, 2021 @03:08PM
(#28331791)
> In an ideal world (one I think we are getting very close to) I think we will end up having SQL queries validated in our code against the actual DB structure the same way we have any other compilation error.
This library for TypeScript works exactly like this
Re:Write an SQL query builder in 150 lines of Python
by mcdonje on Friday August 27, 2021 @09:08AM
(#28327333)
>SQL queries validated in our code against the actual DB structure the same way we have any other compilation error
Good point about validation. The ideal scenario you propose would indeed be ideal.
Re:Write an SQL query builder in 150 lines of Python
by x-shadowban on Friday August 27, 2021 @10:31AM
(#28328273)
F# (and surely there are others) offers type providers, which can reach into your db schemas at compile time and typecheck linq-ish or even raw sql strings and the subsequent uses of the result set. But in the simplest case it makes compilation a function of not only source files but also db schema state. You can extract schema defs and point the type provider at the "cached" defs, but make sure rebuilding those is part of the build process.
Shameless plug, but I just posted a library I wrote (for node https://github.com/vramework/postgres-typed/blob/master/READ...) which pretty much is a tiny layer ontop of pg-node (which is query based / with value parameters) and provides small util functions with typescript support derived straight from postgres tables.
In an ideal world (one I think we are getting very close to) I think we will end up having SQL queries validated in our code against the actual DB structure the same way we have any other compilation error. But until then we'll need to rely on tests or helper libraries, and for the purpose of refactoring and development I find the latter more enjoyable (although still far from perfect).