Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aggregate entities #399

Open
marcosh opened this issue Aug 16, 2024 · 0 comments
Open

aggregate entities #399

marcosh opened this issue Aug 16, 2024 · 0 comments

Comments

@marcosh
Copy link

marcosh commented Aug 16, 2024

currently arrayAgg from Database.Esqueleto.PostgreSQL allows to aggregate Values.

It would be nice if it was possible also to aggregate entities.

For example, consider a 1:n relation between tables foo and bar. IT would be nice to write

data Baz = Baz
  { bazFoo :: Entity Foo
  , bazBars :: [Entity Bar]
  }

$(deriveEsqueletoRecord ''Baz)

query :: FooId -> SqlPersistT m [Baz]
query fooId =
  select $ do
    foo :& bar <-
      from $
        table @Foo
          `leftJoin` table @Bar
            `on` (\(foo :& bar) -> just foo.id ==. bar.foo)
    where_ $ foo.id = val fooId
    groupBy foo.id
    pure $
      SqlBaz
        { bazFoo = foo
        , bazBars = arrayAgg bar
        }

or something analogous

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant