Using Esqueleto to do advanced SQL queries
We have used the Persistent library and SQL expressions using the Database.Persist.SQL
module. We have used the generated types for each field in the filter, insert, and update expressions. But the complexity of the query can increase rapidly. Of course, there is a way to do a plain SQL query with the persistent library. Here, in this recipe, we will be using the Esqueleto
library to do complex queries such as joins.
In this recipe, we will write a complex SQL query that is type-safe and easy to write. Being type-safe is good, because we will catch any major issues earlier on!
How to do it...
- Create a new project called
using-esqueleto
with asimple
stack template:
stack new using-esqueleto simple
- Add dependencies on the
persistent
,persistent-template
,persistent-sqlite
,text
, andmtl
libraries in thebuild-depends
sub-section of theexecutable
section. Also addesqueleto
to the same sub-section:
executable using-esqueleto hs-source-dirs...