I'm trying to route an incoming GET to return the following string:
"The total number of our models is 12"
where 12 is the actual number of entries of a specific model saved to the database.
Now one way to do this is to use the following:
func index(_ req: Request) throws -> Future<String> {
return Model.query(on: req).all().map { models in
return "The total number of our models is \(models.count)"
}
}
This is the most documented and at the same time most inefficient way of doing this. I couldn't find any query that maps to "SELECT COUNT(*) FROM Model;"
So I resorted to writing my own raw SQL against the database. I've gotten this far but I can't figure out how to map [PostgreSQLColumn : PostgreSQLData] to Future<String>
func index(_ req: Request) throws -> Future<String> {
return req.withPooledConnection(to: .psql) { (conn) in
conn.raw("SELECT COUNT(*) FROM MODEL").all()
///....something something
}
}