postgresql-simple 中的嵌套数据类型

Nested data types in postgresql-simple

考虑以下可以找到一组坐标的代码:

data Coord = Coord { lat :: Float
                   , lon :: Float
                   }

instance FromRow Coord where
  fromRow = Coord
    <$> field
    <*> field

findSomePoints :: Connection -> Int -> IO [Coord]
findSomePoints = undefined

那么我想为命名的坐标集定义数据类型:

data Path = Path { name :: String
                 , points :: [Coord]
                 }

instance FromRow Path where
  fromRow = Path
    <$> field
    <*> -- PROBLEM: would like to call something like `findSomePoints conn field` here...

findPath :: Connection -> Int -> IO Path
findPath = undefined

不幸的是,我不知道如何用查询组合数据类型(在我的例子中是 PathCoord)。这样的事情甚至可能吗(以及如何?)。

我会为数据库中的每个 table 编写一个数据类型和一个 FromRow 实例,并将数据库代码与更改架构代码分开。 PathRow[Coord] 是您可以直接从数据库中获取的内容。 makePath :: PathRow -> [Coord] -> Path,构建你想要的嵌套结构,完全不需要和数据库交互。那么findPath就可以根据这几块来实现