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

Q: How to use Where In Array List #61

Closed codingedgar closed 2 years ago

codingedgar commented 2 years ago

Hi, super nice lib, thank you all for creating and maintaining it.

rn I'm doing this query:

    withConnection pool
      ( \conn -> do
          query conn
            ( Query
                ( """
                SELECT "URL", response 
                FROM apicache
                WHERE "URL" IN (""" <> (urls # map (\url -> "'" <> url <> "'") # joinWith ", ") <> ")"
                )
            )
            Row0
      )

Because (I might be terribly wrong) it seems I can only add Row* to query, but urls is Array String, and I do not know how many there could be

Is there an idiomatic way to pass Array String to query?

    withConnection pool
      ( \conn -> do
          query conn
            ( Query
                ( """
                SELECT "URL", response 
                FROM apicache
                WHERE "URL" IN $1
                )
            )
            urls
      )
codingedgar commented 2 years ago

After digging a bit into the code it seems

( \conn -> do
          query conn
            ( Query
                ( """
                SELECT "URL", response 
                FROM apicache
                WHERE "URL" IN ($1)"""
                )
            )
            (unsafeToForeign <$> URLs)
      )

Is fine

codingedgar commented 2 years ago

Note: My previous example was working for an array of 1 element.

According to https://github.com/brianc/node-postgres/wiki/FAQ#11-how-do-i-build-a-where-foo-in--query-to-find-rows-matching-an-array-of-values

    withConnection pool
      ( \conn -> do
          query conn
            ( Query
                ( """
                SELECT "URL", response 
                FROM apicache
                WHERE "URL" = ANY ($1)
                """
                )
            )
            [ unsafeToForeign [ (unsafeToForeign <$> urls) ] ]
      )

Would be better.