You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allowing newtype wrapped columns would be especially useful for (foreign) keys.
For example, it's easy to mistype and join on the wrong fields if they're all ints, but if we add a newtype, then this should be a lot safer. For example, I would love if this worked:
newtypePersonId=PersonIdIntderive instancepersonIdNewtype :: NewtypePersonId_derive instancepersonEq :: EqPersonIdpeople∷Table
( id∷PersonId
, name∷String
, age∷MaybeInt
)
people = Table { name: "people" }
bankAccounts∷Table
( id∷AutoInt
, personId∷PersonId
, balance∷DefaultInt
)
bankAccounts = Table { name: "bank_accounts" }
qNamesWithBalance
∷∀s. FullQuerys { name ∷ ColsString , balance ∷ Cols (MaybeInt) }
qNamesWithBalance =
selectFrom people \{ id, name, age } → do-- FROM people
{ balance } ← leftJoin bankAccounts -- LEFT JOIN bank_accounts
\acc → id .== acc.personId -- ON people.id = bank_accounts.personId
restrict $ id .> lit 1-- WHERE people.id > 1
pure { name, balance } -- SELECT people.name, bank_accounts.balance
So we're wrapping the person id in PersonId Int. Right now this gives an error on selectFrom people since it's looking for an Int but finding a PersonId. Ideally, it would work on any Newtype t Int by wrapping/unwrapping the values.
Would this be possible to implement? I might have a go, given some direction.
The text was updated successfully, but these errors were encountered:
I believe what you (almost) want is already implemented on the master branch, but the problem is that we haven't yet published it. Here is a link to the guide which explains how to deal with custom types just in case you've missed it
The error comes from the use of id .> lit 1. So changing the lit to handle Newtyped values would do the trick - but this kind of implicit dispatch does not really fit IMO (partly because of the current experimental version of Lit on the scope-as-backend branch which would not cope with it, I could elaborate on it more if want to hear more)
A simple solution is to use litPG (it will change to just lit eventually when we'll decide to commit to the changes on the scope-as-backend branch)
Allowing newtype wrapped columns would be especially useful for (foreign) keys.
For example, it's easy to mistype and join on the wrong fields if they're all ints, but if we add a newtype, then this should be a lot safer. For example, I would love if this worked:
So we're wrapping the person id in
PersonId Int
. Right now this gives an error onselectFrom people
since it's looking for anInt
but finding aPersonId
. Ideally, it would work on anyNewtype t Int
by wrapping/unwrapping the values.Would this be possible to implement? I might have a go, given some direction.
The text was updated successfully, but these errors were encountered: