I’m working on an Elixir open-source project in which I have an Accounts
context with a function find_or_create
to find or create an account in the database with the metadata passed by OAuth providers like GitHub.
To find the user I’d need to run a sequence of queries and return early when I find the user. Something along the lines of:
- If a user with the same GitHub
uid
exists, then return the account associated to the user. - Otherwise, if a user exist with the same email as GitHub’s, return the account associated to th euser.
- If none of the above is met, then create the user along with its account.
In other programming languages (I’m new to Elixir), I’d write something along the lines of:
if user do
return user.account
end
But the above is not possible in Elixir so I was wondering what’d be the Elixir way of tackling it. Note that I don’t need to run the second query if the first yields a value.
Thanks beforehand