【问题标题】:request, that save array of models in vapor 3请求,将模型数组保存在蒸汽 3 中
【发布时间】:2019-03-05 16:11:59
【问题描述】:

如何使用transactions 将模型数组更新为mysql?操作成功后返回Ok响应

【问题讨论】:

  • 使用原始 SQL 查询或 Fluent ?
  • @nathan,在Vapor 3 swift 框架中使用Fluent MySQL

标签: fluent vapor


【解决方案1】:

所以,我在没有交易的情况下解决了我的问题:

func update(_ req: Request, todos: [CreateTodoRequest]) throws -> Future<HTTPStatus> {
    let user = try req.requireAuthenticated(User.self)
    // 1
    return try todos.map { try Todo(todo: $0, userID: user.requireID()).create(on: req) }
        // 2
        .flatten(on: req)
        // 3
        .transform(to: .ok)
}

.create(on: req) 返回一个EventLoopFuture&lt;Todo&gt;。所以todos.map 将返回一个[EventLoopFuture&lt;Todo&gt;] 的数组。第二步是将其转换为EventLoopFuture&lt;[Todo]&gt;。而第三步,将其转化为HttpStatus.ok响应

【讨论】:

  • 这救了我。谢谢!
猜你喜欢
  • 2020-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多