Go to content

Alexander Webb - fragment/1: Ecto.Query's underrated superweapon

https://elixirforum.com/t/elixirconf-2023-alexander-webb-fragment-1-ecto-querys-underrated-superweapon/59945 https://2023.elixirconf.com/presenters#speaker-alexander-webb-elixirconf-us-2023 Ecto's query DSL is _very_ nice and powerful, even without `fragment`. However, many extremely interesting corners of SQL query syntax are out of reach to the DSL. That's where `fragment` comes in. I know, of course, that Ecto also supports SQLite and MySQL, but I have never used them professionally and won't touch on them here. All of the Ecto content should generalize, but I will show some advanced Postgres query features which won't. In this talk, I will show: - The basics of how a DSL query turns into SQL. - How fluent and easy it is to use `fragment` when the DSL syntax isn't available. - How there are powerful expansion-time safety features that make it much harder to misuse than you would think - How to combine `fragment` with PG's JSON support and aggregates to write some cool and performant queries that unwrap deeply nested joins into flat queries returning nested JSON (which Ecto conveniently turns right into Elixir nested maps) - How to safely wrap `fragment` in another macro to reduce duplication without making a SQL-injection vulnerability

September 5, 2023