rightfold / purescript-postgresql-client

https://pursuit.purescript.org/packages/purescript-postgresql-client
BSD 3-Clause "New" or "Revised" License
35 stars 20 forks source link

[question] Why Are there Different Definitions of Models? #44

Closed VizorKit closed 3 years ago

VizorKit commented 4 years ago

label:question Sorry, I'm new to purescript. I'm curious why we define 2 different models. It states that one is passed to the ffi. I transpiled each of these definitions to javascript, and only the first one exists. however, the first one appears to be a valid state to pass to the ffi method. Is there something in purescript that states we should do this, whats the reason?

type PoolConfiguration =
    { database :: Database
    , host :: Maybe String
    , idleTimeoutMillis :: Maybe Int
    , max :: Maybe Int
    , password :: Maybe String
    , port :: Maybe Int
    , user :: Maybe String
    }

-- | Configuration which we actually pass to FFI.
type PoolConfiguration' =
    { user :: Nullable String
    , password :: Nullable String
    , host :: Nullable String
    , port :: Nullable Int
    , database :: String
    , max :: Nullable Int
    , idleTimeoutMillis :: Nullable Int
    }
Kamirus commented 4 years ago

As users, we use PoolConfiguration, but when this config needs to be passed to the underlying js library we don't want values of type Maybe there.

Starting from the line linked below: https://github.com/rightfold/purescript-postgresql-client/blob/680da31ee1c6430fe296788994fe029024c75ade/src/Database/PostgreSQL.purs#L84 is a function which performs this transformation from PoolConfiguration to PoolConfiguration' and then calls the FFI function ffiNewPool

Record of type PoolConfiguration' is a valid object in javascript, because PureScript's records are just js objects with keys as labels. But for example Maybe String is not represented as a string or null (feel free to call unsafeStringify to see for yourself), whereas Nullable has this exact runtime representation that is suitable for FFI functions

VizorKit commented 4 years ago

Cool, thank you. This does make sense, maybe in more time I would see the reason as to why would we even use Maybe over Nullable in purescript. For now I can go with it. I'm sorry I chose this project to work with purescript and figure out the complex parts with Aff, Effect, FFI, etc. If you want me to go somewhere else I can.

Kamirus commented 4 years ago

Awesome :smile: You can check out the forum, here is the link to the specific thread with a repo you might be interested in https://discourse.purescript.org/t/purescript-ffi-best-practices/1081, and there's more if you search for 'FFI'. And there's more knowledge regarding Aff, Effect and FFI in PS on the internet, you can ask someone on slack