0

エンティティ型のオブジェクトをデータベースに挿入する必要があります

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
)

ただし、これは 1 対 1 の関係でのみ機能します。ご協力ありがとうございました。

4

1 に答える 1