我需要在数据库中插入一个实体类型的对象
case class Entity(id: Long, name: String)
case class OtherEntity(id: Long, entity_id: Long, info: String)
case class AnotherEntity(other_entity_id: Long, data: String)
如果在输入时我收到大约
{
"name": "string",
"data": [
{
"info": "string",
"data": [
{
"data": "string"
}
]
}
]
}
主要问题是我想不出 doobie.ConnectioIO 的模拟 foreach。
sql"insert into entity (name) values('name')"
.update.withUniqueGeneratedKeys[Long]("id")
.flatmap(entityId =>
sql"insert into other_entity (entity_id, info) values ($entityId, 'info')"
.update.withUniqueGeneratedKeys[Long]("id")
).flatmap(otherEntityId =>
sql"insert into another_entity (other_entity_id, data) values ($otherEntityId, 'data')"
.update.run
)
但这仅适用于一对一的关系。感谢您的帮助。