在 Vapor 3 中,您可以使用filter
带有 SQLiteBinaryOperator 的方法,因此您可以使用 like 运算符创建查询。我试图在 Vapor 4 中做同样的事情,但找不到任何东西。这是我的代码
蒸气 4
func queryUserMovies(_ req: Request) throws -> Future<[Users]> {
let title = req.parameters.get("title")!
return Movies.query(on: req.db).filter(\.$title == title).first().unwrap(or:Abort(.notFound, reason: "There's no movie")).flatMap{ movie in
return movie.$users.query(on: req.db).all()
}
}
蒸气 3
func queryUserMovies(_ req: Request) throws -> Future<[Users]> {
guard let movie = req.query[String.self, at: "movie"] else {
throw Abort(.badRequest, reason: "Not such movie")
}
return Movies.query(on: req).filter(\.title, .like, movie).first().unwrap(or:Abort(.notFound, reason: "There's no movie")).flatMap{ movie in
return movie.users.query(on: req).all()
}
}
Vapor 4 中是否有类似的东西,还是我需要在 SQL 中执行原始查询?