-
Hi, First of all, thank you for providing such an amazing library! I searched around the documentation but have not yet found a way to "append" another table's results into a decodable request…? // Records
struct Space: Decodable, Hashable, FetchableRecord, Sendable { … }
struct ScientificClassification: Decodable, Hashable, FetchableRecord, Sendable {
static let plant = belongsTo(Plant.self)
var plant: QueryInterfaceRequest<Plant> {
request(for: Self.plant)
}
}
struct Plant: Decodable, Hashable, FetchableRecord, Sendable {
static let scientificClassification = hasOne(ScientificClassification.self)
var scientificClassification: QueryInterfaceRequest<ScientificClassification> {
request(for: Self.scientificClassification)
}
}
// FetchKeyRequest
struct Value: Decodable, Hashable, FetchableRecord, Sendable {
var space: Space
var plantsInfo: [PlantInfo]
init(space: Space, plantsInfo: [PlantInfo] = []) {
self.space = space
self.plantsInfo = plantsInfo
}
}
struct PlantInfo: Decodable, Hashable, FetchableRecord, Sendable {
var plant: Plant
var scientificClassification: ScientificClassification
}
let scientificClassificationAlias = TableAlias()
let scientificClassification = Plant.scientificClassification
.aliased(scientificClassificationAlias)
.forKey("scientificClassification")
let plantsAlias = TableAlias()
let plantsInfoRequest = Plant
.all()
.aliased(plantsAlias)
.including(required: scientificClassification)
let request = Space
.filter(id: space.id)
// What I'm trying to do:
.append(plants.forKey("plantsInfo"))
// Later…
try Value.fetchOne(db, request) ?? defaultValue |
Beta Was this translation helpful? Give feedback.
Answered by
groue
Mar 3, 2025
Replies: 1 comment 2 replies
-
Hello @Pomanks, It looks like you want to perform two fetches, one for the space, and another one for the plants, and compose the results in your func fetch(_ db: Database, spaceId: Space.ID) throws -> Value? {
// One fetch...
guard let space = try Space.fetchOne(db, id: spaceId) else {
return nil
}
// Another fetch...
let plantInfos = try Plant
.including(required: Plant.scientificClassification)
.asRequest(of: PlantInfo.self)
.fetchAll(db)
// ...et voilà!
return Value(space: space, plantInfos: plantInfos)
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
Pomanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @Pomanks,
It looks like you want to perform two fetches, one for the space, and another one for the plants, and compose the results in your
Value
type: