bitemyapp / bitemyapp/esqueleto

Feature Request: `autoOnClause`

Open
#227 0 comments 3 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
399
Forks
107
Avg merge
1d 17h
Merged PRs (30d)
1

Description

A lot of the time, the `on` clause is obvious.

```
User
name Text

UserOrganization
user UserId
organization OrganizationId

Organization
name Text
```

```haskell
joinOrganizationUsers = do
u :& _ :& o <-
from $
Table @User
`InnerJoin`
Table @UserOrganization
`on` do
\(u :& uo) ->
u ^. UserId ==. uo ^. UserOrganizationUser
`InnerJoin`
Table @Organization
`on` do
\(_ :& uo :& o) ->
uo ^. UserOrganizationOrganization ==. o ^. OrganizationId
pure (u :& o)
```

It would be great to just write:

```haskell
joinOrganizationUsers = do
u :& _ :& o <-
from $
Table @User
`InnerJoin`
Table @UserOrganization
`on` autoOnClause
`InnerJoin`
Table @Organization
`on` autoOnClause
pure (u :& o)
```

This should be feasible.

```haskell
class AutoOnClause a where
autoOnClause :: a -> SqlExpr (Value Bool)

instance
(HasOneForeignRef (Entity a) b)
=>
AutoOnClause (SqlExpr (Entity a) :& SqlExpr (Entity b))
where
autoOnClause (a :& b) =
a ^. singularForeignRefTo ==. b ^. persistIdField

instance ??? => AutoOnClause (a :& SqlEpxr (Entity b)) where
autoOnClause (a :& b) =
getMatchingEntity a ^. singularForeignRefTo ==. b ^. persistIdField
```

Contributor guide

No contributing guide indexed for this repository

Research direction

No files or tests are named. Start by examining the existing join and foreign-reference APIs around `HasOneForeignRef`, `singularForeignRefTo`, and `getMatchingEntity`, then determine how `AutoOnClause` should cover the proposed join shapes. Done means the intended `autoOnClause` examples typecheck and have tests covering the inferred relationships.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.