Join dictionaries like SQL does.
Check Dict.Joinable
.
OneToOne and ManyToOne modules are just two Joinable implementations that were suitable to my use case.
The idea is to create yours. It would be a pleasure to merge a pull request of your joinable container.
Join User and City dictionaries for matching ids in Dict
:
import Dict exposing (Dict)
import Dict.OneToOne exposing (..)
type alias User = {...}
type alias City = {...}
type alias FavoritePub = {...}
type alias Database =
{ users : Dict Int User
, cities : Dict Int City
, favoritePubs : Dict Int FavoritePub
}
type alias UserWithCity =
{ user : User
, city : City
, favoritePub : FavoritePub
}
getUserWithCity : Database -> Dict Int UserWithCity
getUserWithCity db =
select UserWithCity
|> from db.users
|> innerJoin db.cities
|> innerJoin db.favoritePubs
SQL Equivalent for getUserWithCity
function:
SELECT *
FROM users
INNER JOIN cities
ON users.id = cities.id
INNER JOIN favoritePubs
ON users.id = favoritePubs.id
Note that we always take Dict
key for join match in this package so we don't use ON
like SQL does.