Questions tagged [esqueleto]

The esqueleto EDSL (embedded domain specific language) for SQL queries which replaces Database.Persist

esqueleto is a bare bones, type-safe EDSL for SQL queries that works with unmodified persistent SQL backends. Its language closely resembles SQL, so (a) you don't have to learn new concepts, just new syntax, and (b) it's fairly easy to predict the generated SQL and optimize it for your backend. Most kinds of errors committed when writing SQL are caught as compile-time errors---although it is possible to write type-checked esqueleto queries that fail at runtime.

persistent is a library for type-safe data serialization. It has many kinds of backends, such as SQL backends (persistent-mysql, persistent-postgresql, persistent-sqlite) and NoSQL backends (persistent-mongoDB).

While persistent is a nice library for storing and retrieving records, currently it has a poor interface for SQL backends compared to SQL itself. For example, it's extremely hard to do a type-safe JOIN on a many-to-one relation, and simply impossible to do any other kinds of JOINs (including for the very common many-to-many relations). Users have the option of writing raw SQL, but that's error prone and not type-checked.

The name of this library means "skeleton" in Portuguese and contains all three SQL letters in the correct order =). It was inspired by Scala's Squeryl but created from scratch.

Taken from the HackageDB site

77 questions
144
votes
1 answer

Handling List-types with Esqueleto

I have data types defined as: data ComitteeView = CommitteeView { committeeId :: CommitteeId , committeeMembers :: [Person] } data CommitteesView = CommitteesView { committeeView…
nomen
  • 3,626
  • 2
  • 23
  • 40
85
votes
1 answer

How can I get esqueleto to generate an SQL string for me?

How can I get esqueleto to generate an SQL string from a from statement? The documentation of toRawSql says that "you may just turn on query logging of persistent". I tried all possible forms of MonadLogger that I could understand, but it never…
Tom Ellis
  • 9,224
  • 1
  • 29
  • 54
13
votes
1 answer

Representing sum types in sql with Persistent, and Esqueleto joins

I have been trying to figure out a sensible way to represent Haskell sum types in a SQL backend using persistent. My target Haskell data type is along the lines of data Widget = FooWidget Int | BarWidget T.Text data HElement = HElement { name …
Michael Thomas
  • 1,354
  • 7
  • 18
9
votes
3 answers

Esqueleto: How can I delete an item using a join

Is the following query possible using esqueleto? DELETE Table1 FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.ItemID I've tried: delete $ from $ \(table1 `InnerJoin` table2) -> on (table1 ^. Table1ID ==. table2 ^.…
jamshidh
  • 12,002
  • 17
  • 31
9
votes
2 answers

What is a correct way of doing COUNT(id) in Esqueleto and Yesod

I'm trying to figure out how to write the following query using Esqueleto SELECT COUNT("person"."id") FROM "person" WHERE (("person"."admin" = 't' OR "person"."vip" = 't') // 't' as in True OR "person"."karma" >= 5000 AND "person"."hellbanned"…
Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327
8
votes
1 answer

Can't write double left join using Esqueleto

I have a database schema with teachers, schools, and districts. The TEACHERS table has a nullable SCHOOL_ID column (a teacher may or may not belong to a school) and the SCHOOLS table has a nullable DISTRICT_ID column (a school may or may not belong…
Sean
  • 29,130
  • 4
  • 80
  • 105
5
votes
1 answer

Select from subquery and join on subquery in Esqueleto

How can I do select ... from (select ...) join (select ...) in Esqueleto? I'm aware that I can use rawSql from Persistent, but I'd like to avoid that. For the record, here is the full query: select q.uuid, q.upvotes, q.downvotes,…
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
5
votes
1 answer

Dynamically build SQL-Queries wit Esqueleto and Template Haskell?

I'm writing a webapp wit Yesod & Persistent. I have a SQL-Database with several tables, containing characteristics of my "projects". I have a main table and for the Option with multiple values extra tables linked with the id. The user should be able…
Nudin
  • 351
  • 2
  • 11
5
votes
2 answers

Esqueleto: join on subquery (using subList_select)

I'm trying to translate the following SQL to Esqueleto: SELECT id, task_id, author_id FROM scenario INNER JOIN ( SELECT task_id as tId, author_id as aId, MAX(last_update) as lastUp FROM scenario GROUP BY task_id, author_id …
mb21
  • 34,845
  • 8
  • 116
  • 142
4
votes
0 answers

How to implement MySql's GROUP_CONCAT using Esqueleto in Haskell?

I need to find out how to implement MySql's Group_Concat under Esqueleto in Haskell. The esqueleto function should look like the following but this version is suitable for Sqlite. So I need a working solution for Mysql: sqliteGroupConcat :: …
angie
  • 59
  • 5
4
votes
0 answers

Specifying Postgres table schemas in Esqueleto/Persistent

I'm using Esqueleto with Postgres and I don't see a way to specify the schema that a table resides in. Currently I'm issuing the following sql to set the schemas: set search_path to foo,bar; This allows me to use the tables I want as long as they…
user3776949
  • 237
  • 2
  • 8
4
votes
1 answer

Counting rows with esqueleto

I am trying to count the rows of an inner-join with Esqueleto (version 2.1.2.1). Unfortunately, my code doesn't compile and I don't understand why. I looked at the following examples of how to do this but couldn't figure out what I'm doing wrong:…
Lemming
  • 4,085
  • 3
  • 23
  • 36
4
votes
1 answer

Aggregate function as a select result

Is it possible to write select min(next) from participant; as an esqueleto query? UPDATE I discovered the min_ function. However, the code nextMessageTime = from $ \p -> min_ (p ^. ParticipantNext) apparently has some ambiguous types: Could not…
Roman Cheplyaka
  • 37,738
  • 7
  • 72
  • 121
3
votes
1 answer

Esqueleto's types in `leftJoin` seem inconsistent with the documentation

I'm trying to do a leftJoin using the following query: select $ do (event :& _ :& tags) <- from $ table @EventRow `leftJoin` table @EventTagRow `on` ( \(event :& links) -> event ^.…
GTF
  • 8,031
  • 5
  • 36
  • 59
3
votes
2 answers

Esqueleto to return just a single column

I'm quite new to Haskell, currently doing my best to prevent my brain from going inside out. I've got a very simple DB schema defined in that way: share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase| Instrument ticker…
LA.27
  • 1,888
  • 19
  • 35
1
2 3 4 5 6