Razumov
Razumov

Reputation: 324

Haskell postgresql-simple query access single record

In postgresql-simple, the query method returns a list. When I am attempting to access a single record (by pk for example), I know the list should either be empty or a single item. Is there a canonical way to handle this with postgresql-simple or do I need to layer my own function on top of it?

Currently I do the following with the list returned by query to get a single item:

  case retrieved of
    [] -> throwError err404
    [r] -> pure r
    (_r : _rs) -> throwError err500

For one, the third case will never be hit. Moreover, this feels like such common pattern that there should be a way to represent it in the type annotation that postgresql-simple requires.

Addendum:

An established pattern for handling this may also extend to operations like delete by pk where we only want a single record to be affected.

Upvotes: 1

Views: 63

Answers (0)

Related Questions