I didn’t find any solution to run subquery using pure Fluent syntax , i was forced to use pur sql
func getFavorite(_ req: Request) throws -> Future<[Article]> {
return try req.parameters.next(User.self).flatMap(to: [Article].self) { user in
guard let id = user.id else {
throw Abort(HTTPStatus.notFound)
}
return req.withPooledConnection(to: .psql) { conn in
return conn
.raw("Select * From \"Article\" as Ar where Ar.id in (select UA.articleid From \"Article_User\" as UA where UA.userid = \(id) and UA.favorite = \(true) group by UA.userid, UA.articleid)").all(decoding: Article.self)
}
}
}
Now i’m struggling, how i can use Pagination framework ? with such custom query .
@swiftios which Pagination framework? The Nodes one? If so, I’m not sure if they would support raw SQL (though if it’s a QueryBuilder thing, you should be able to do something like
return conn.raw("Select * From \"Article\" as Ar where Ar.id in (select UA.articleid From \"Article_User\" as UA where UA.userid = \(id) and UA.favorite = \(true) group by UA.userid, UA.articleid)").paginate(for: req, decoding: Article.self)
I suspect that would require a change in the framework to support a decoding type. You could raise an issue on their GitHub for it
“Select * From "Article" as Ar where Ar.id in (select UA.articleid From "Article_User" as UA where UA.userid = (id) and UA.favorite = (true) group by UA.userid, UA.articleid)”
like if have changed the in to not in it’s will impossible to use direct Join operation.
Do you have any idea how run such query using just fluent ?