2

OK, so, I've been struggling with Vapor/Fluent and the more complex returns of data.

I am basically trying to return the following query in fluent-mysql:

select e.*,
    et.description,
    es.description
from
    e inner join et on e.tid = et.id
    inner join e.sid=es.id
where e.deleted=0

Fairly standard. So, the func I am using has this 'query':

return Equipment.query(on: req)
    .filter(\Equipment.deleted==false)
    .join(\Equipment.typeid, to: \EquipmentTypes.id )
    .join(\Equipment.seriesid, to: \EquipmentSeries.id )
    .alsoDecode(EquipmentTypes.self)
    .alsoDecode(EquipmentSeries.self)
    .all()

I know that I can't have the function return a basic model a la:

return Equipment.query(on: req).decode(Equipment.self).all()

So I need to 'map' or 'flatMap' to a struct, so I defined one (ignore the init for now):

struct EquipmentArray: Encodable {
     let id: Int
     let description: String
     let seriesid: Int
     let series: String
     let typeid: Int
     let type: String
     let mintime: Int
     let maxtime: Int
     let room_length: Double
     let room_width: Double
     let lamps_bottom: Bool
     let lamps_top: Bool
     let lamps_facial: Bool
     let lamps_shoulder: Bool
     let lamps_neck: Bool
     let deleted: Bool

     init(id: Int){
      self.id = id
     }

}

and that gives me the function header to use as:

func getAllHandler(_ req: Request) throws -> Future<[EquipmentArray]>{

Here is where I am hung up - I can see examples of where to flatMap/map 1 result to a struct, but not an example of mapping a tuple (?) to struct. Basically mapping [((Equipment, EquipmentType), EquipmentSeries)] -> [EquipmentArray]

Has anyone got any good examples of how to do this?

nathan
  • 9,329
  • 4
  • 37
  • 51
ConBran
  • 369
  • 2
  • 15

0 Answers0