2
votes

I have 3 tables, LastName, MiddleName and FirstName, I would like to create a new record or update record in FirstName, MiddleName only if lastName exists. if last name does not exist, then would like to return error back.


final class LastName: Model, Content {
    static let schema = "lastnames"
    
    @ID(key: .id)
    var id: UUID?

    @Field(key: "name")
    var name: String

    
    @Children(for: \.$lastname)
    var middle_names: [MiddleName]
    
    init() { }

    init(id: UUID? = nil, name: String) {
        self.id = id
        self.name = name
    }
}

final class MiddleName: Model, Content {
    static let schema = "middlenames"
    
    @ID(key: .id)
    var id: UUID?

    @Parent(key: "last_name_id")
    var lastname: LastName
    
    @Field(key: "name")
    var name: String

    @Children(for: \.$middleNameId)
    var firstNames: [FirstName]
    
    init() { }

    init(id: UUID? = nil, lastname: LastName, name: String ) {
        self.id = id
        self.lastname = lastname
        self.name = name
    }
}


final class FirstName: Model, Content {
    static let schema = "firstnames"
    
    @ID(key: .id)
    var id: UUID?
    
    @Parent(key: "middle_name_id")
    var middleNameId: MiddleName
    
    
    @Field(key: "name")
    var name: String
   
    
    init() { }

    init(id: UUID? = nil, middleNameId: MiddleName, name: String) {
        self.id = id
        self.middleNameId = testBundleId
        self.name = name
    }
}

Fluent docs are very minimal on creating new record by doing nested joins,As ID's are auto generated when creating Lastname, how do I get the ID for creating MiddleName/FirstName. Also are there any good example of doing an update or create in multiple tables based on relationships?

Basically I want fluent to do something like

 LastName.query(on: req.db).filter(\.$name == "Smith")
            .first()
            .unwrap(or: Abort(.notFound))
            // Now Find if middle name exists, if exists use the ID to create first name and maybe even update middle name table, 
            // if middle name does not exist, create middle name record, then use the new ID to create first name record
            // return the new record(s) as some new Swift Codable DTO
1
Do you really need the id for this? You query for a LastName and then you search the middle_names array for the given middle name and either attach the first name to the result or to a new MiddleName object that you insert into the middle_names array before saving. It was some time ago since I used Fluent and Vapor but this should be possible.Joakim Danielson
@JoakimDanielson I don't need ID, Sorry I should have clarified.. ID or name or just the record should be fine..user2616355

1 Answers

3
votes

You can do something like

LastName.query(on: req.db).filter(\.$name == "Smith")
  .first()
  .unwrap(or: Abort(.notFound))
  .flatMap { lastName in
    let middlenameQuery = MiddleName.query(on: req.db).filter(\.$name == "Jane").first().flatMap { middlenameFound in
      if let middleName = middlenameFound {
        return req.eventLoop.future(middleName)
      } else {
        let newMiddlename = Middlename(name: "Jane")
        return newMiddlename.create(on: req.db).transform(to: newMiddlename)
      }
    }
    return middlenameQuery.flatMap { middlename in
      // Use last name and middle name here
    }

}