Questions tagged [opaleye]

Haskell library that provides an SQL-generating embedded domain specific language for targeting Postgres

A Haskell library to define database tables and write queries against them in Haskell code. Aims to be typesafe in the sense that if the code compiles then the generated SQL query will not fail at runtime.

15 questions
6
votes
2 answers

Why are database queries a good place to use Arrows?

I was reading this, which said: Well, the point is that arrow notation forbids some computations that do notation allows. In particular all “arrow actions” must be “statically” known“. and it explains: Statically known" means that if we have a…
4
votes
1 answer

Does opaleye support upsert / INSERT ON CONFLICT?

I'm trying to create a query like: INSERT INTO users (id, level) VALUES (1, 0) ON CONFLICT (id) DO UPDATE SET level = users.level + 1; However I can't see how to do this with opaleye? Is this not supported? Strangely we have Insert defined with a…
Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
3
votes
4 answers

Left join in Opaleye

I have been trying to run a left join using Opaleye in a project but I'm not being able to make the code compile. I start with two "models" which represent tables that are associated: First: data ModelA' a b = Model { primA :: a, foreignA :: b…
Jesuspc
  • 1,664
  • 10
  • 25
2
votes
2 answers

Array-aggregation across a link-table in Opaleye

I'm trying to construct an Opaleye query that matches the following SQL: select * , (select array_agg(tags.tagname) from articles_tags inner join tags on tags.id = articles_tags.tag_fk where articles_tags.article_fk =…
Ulrich Schuster
  • 1,670
  • 15
  • 24
2
votes
1 answer

How to generalize an Opaleye Query in Haskell (Using Vinyl)?

My question is between the huge banners in the code block below. Forgive the code dump, this is all pasted here for anyone wanting to replicate, and this code does work as expected, although it's a bit strange. Notice the last two lines, they print…
Josh.F
  • 3,666
  • 2
  • 27
  • 37
1
vote
2 answers

Query for all N elements in an M:N relation

Say I have the following tables that model tags attached to articles: articles (article_id, title, created_at, content) tags (tag_id, tagname) articles_tags (article_fk, tag_fk) What is the idiomatic way to retrieve the n newest articles with all…
Ulrich Schuster
  • 1,670
  • 15
  • 24
1
vote
1 answer

leftJoinF in Opaleye and IfPP

I want to use a LEFT JOIN to fetch products and their (optional) attributes. I have types like: type ProductPGR = ProductPoly (Column (PGID Product)) (Column PGText) type ProductAttributePGR = ProductAttributePoly (Column (PGID ProductAttribute))…
limick
  • 55
  • 3
1
vote
1 answer

In Opaleye, how to insert to two tables in the same query?

My database has two tables with bijective row correspondence (as described in this question). It seems this means that to insert a row to each table without violating foreign key constraints requires the two inserts to occur within the same…
mherzl
  • 5,624
  • 6
  • 34
  • 75
1
vote
1 answer

How to implement this fuzzy search sql query in Opaleye?

I have an sql query that I am attempting to convert to Opaleye. I'll simplify my code to focus on the point of my question, namely how to fuzzy search with Opaleye. The Haskell (with some existing Opaleye structures) is namesTable :: O.Table…
mherzl
  • 5,624
  • 6
  • 34
  • 75
1
vote
1 answer

Opaleye newtype

One of the fields in my datatype for a table in my PostgreSQL database is a newtype wrapping UUID called ItemId. import Data.Profunctor.Product.TH (makeAdaptorAndInstance) import Data.DateTime (DateTime) import Data.UUID import GHC.Generics import…
1
vote
2 answers

How to reference newtyped key in Opaleye as nullable?

I'm using newtyped keys for all my tables newtype Key' a = Key a deriving (Show, Generic, Functor) type Key = Key' Int64 type KeyR = Key' (Column PGInt8) type KeyW = Key' (Maybe (Column PGInt8)) $(makeAdaptorAndInstance "pKey" ''Key') I now want to…
0
votes
0 answers

How to view the SQL generated by an Opaleye query?

In the Opaleye Tutorial, line 88 lists the following example ghci command. ghci> printSql personQuery personQuery there is a predefined query, and it appears that printSql is a function which prints the SQL generated by it. Where is this printSql…
mherzl
  • 5,624
  • 6
  • 34
  • 75
0
votes
1 answer

Declaring an Opaleye table without using TemplateHaskell

The opaleye basic tutorial gives an example on how to use user defined types in record types and queries: data Birthday' a b = Birthday { bdName :: a, bdDay :: b } type Birthday = Birthday' String Day type BirthdayColumn = Birthday' (Column PGText)…
Damian Nadales
  • 4,907
  • 1
  • 21
  • 34
0
votes
1 answer

Can Haskell's Opaleye DSL generate any given SQL?

My team uses Opaleye to query Postgres from Haskell. However, we also use raw SQL to do such things as: Initialize the database. Including commands create database and create table Perform database migrations when we change our schema, including…
mherzl
  • 5,624
  • 6
  • 34
  • 75
0
votes
2 answers

Opaleye query by String to Maybe

I want to run a query against my table for a given value, and return a Maybe a depending on whether a row was found. I have this domain: data User' a b c d e f = User { usrId :: a, usrApproved :: b, …
Alex
  • 8,093
  • 6
  • 49
  • 79