Hi
I have the following route
func detail(req: Request) async throws -> User.ReadDTO {
guard let userId = req.userId else {
throw Abort(.unauthorized)
}
let persons = try await Person.query(on: req.db)
.filter(\.$user.$id == userId)
.with(\.$activities)
.with(\.$rewards)
.all()
.map { person in
try Person.ReadDTO(
id: person.requireID(),
name: person.name,
points: person.points,
activities: try person.activities.map { activity in
try PersonActivity.ReadDTO(
id: activity.requireID(),
title: activity.title,
description: activity.description,
reward: activity.reward,
lastPerformed: activity.lastPerformed,
createdAt: activity.createdAt!
)
},
rewards: try person.rewards.map { reward in
try PersonReward.ReadDTO(
id: reward.requireID(),
title: reward.title,
description: reward.description,
price: reward.price,
createdAt: reward.createdAt!
)
}
)
}
return User.ReadDTO(persons: persons)
}
and I would expect it to fetch the desired data from the database, transform it into DTOs and then send it as JSON. When I print out the data I get the following
{"persons":[{"points":0,"id":"304B2D4D-ACA5-4BAF-8C56-2740B339143A","rewards":[],"activities":[{"reward":10,"id":"E882FF63-58F8-44D3-8975-6D70DCD8CC03","title":"Clean","description":"PersonActivity(output: _PostgresSQLRow(randomAccessView: PostgresNIO.PostgresRandomAccessRow(columns: [PostgresNIO.RowDescription.Column(name: \"person_activities_id\", tableOID: 16671, columnAttributeNumber: 1, dataType: UUID, dataTypeSize: 16, dataTypeModifier: -1, format: binary), PostgresNIO.RowDescription.Column(name: \"person_activities_title\", tableOID: 16671, columnAttributeNumber: 2, dataType: VARCHAR, dataTypeSize: -1, dataTypeModifier: 44, format: binary), PostgresNIO.RowDescription.Column(name: \"person_activities_description\", tableOID: 16671, columnAttributeNumber: 3, dataType: TEXT, dataTypeSize: -1, dataTypeModifier: -1, format: binary), PostgresNIO.RowDescription.Column(name: \"person_activities_reward\", tableOID: 16671, columnAttributeNumber: 4, dataType: INTEGER, dataTypeSize: 4, dataTypeModifier: -1, format: binary), PostgresNIO.RowDescription.Column(name: \"person_activities_last_performed\", tableOID: 16671, columnAttributeNumber: 5, dataType: TIMESTAMPTZ, dataTypeSize: 8, dataTypeModifier: -1, format: binary), PostgresNIO.RowDescription.Column(name: \"person_activities_created_at\", tableOID: 16671, columnAttributeNumber: 6, dataType: TIMESTAMPTZ, dataTypeSize: 8, dataTypeModifier: -1, format: binary), PostgresNIO.RowDescription.Column(name: \"person_activities_deleted_at\", tableOID: 16671, columnAttributeNumber: 7, dataType: TIMESTAMPTZ, dataTypeSize: 8, dataTypeModifier: -1, format: binary), PostgresNIO.RowDescription.Column(name: \"person_activities_person_id\", tableOID: 16671, columnAttributeNumber: 8, dataType: UUID, dataTypeSize: 16, dataTypeModifier: -1, format: binary)], cells: [Optional(ByteBuffer { readerIndex: 0, writerIndex: 16, readableBytes: 16, capacity: 16, storageCapacity: 512, slice: _ByteBufferSlice { 406..<422 }, storage: 0x00000001306052f0 (512 bytes) }), Optional(ByteBuffer { readerIndex: 0, writerIndex: 5, readableBytes: 5, capacity: 5, storageCapacity: 512, slice: _ByteBufferSlice { 426..<431 }, storage: 0x00000001306052f0 (512 bytes) }), Optional(ByteBuffer { readerIndex: 0, writerIndex: 14, readableBytes: 14, capacity: 14, storageCapacity: 512, slice: _ByteBufferSlice { 435..<449 }, storage: 0x00000001306052f0 (512 bytes) }), Optional(ByteBuffer { readerIndex: 0, writerIndex: 4, readableBytes: 4, capacity: 4, storageCapacity: 512, slice: _ByteBufferSlice { 453..<457 }, storage: 0x00000001306052f0 (512 bytes) }), nil, Optional(ByteBuffer { readerIndex: 0, writerIndex: 8, readableBytes: 8, capacity: 8, storageCapacity: 512, slice: _ByteBufferSlice { 465..<473 }, storage: 0x00000001306052f0 (512 bytes) }), nil, Optional(ByteBuffer { readerIndex: 0, writerIndex: 16, readableBytes: 16, capacity: 16, storageCapacity: 512, slice: _ByteBufferSlice { 481..<497 }, storage: 0x00000001306052f0 (512 bytes) })], lookupTable: [\"person_activities_title\": 1, \"person_activities_id\": 0, \"person_activities_deleted_at\": 6, \"person_activities_reward\": 3, \"person_activities_created_at\": 5, \"person_activities_person_id\": 7, \"person_activities_last_performed\": 4, \"person_activities_description\": 2]), decoder: PostgresKit.PostgresDataDecoder))","createdAt":"2023-03-13T15:35:40Z"},{"reward":10,"id":"3DBA0637-7C08-4CE4-A7B9-570F79D0ED6B","title":"Cook","description":"PersonActivity(output: _PostgresSQLRow(randomAccessView: PostgresNIO.PostgresRandomAccessRow(columns: [PostgresNIO.RowDescription.Column(name: \"person_activities_id\", tableOID: 16671, columnAttributeNumber: 1, dataType: UUID, dataTypeSize: 16, dataTypeModifier: -1, format: binary), PostgresNIO.RowDescription.Column(name: \"person_activities_title\", tableOID: 16671, columnAttributeNumber: 2, dataType: VARCHAR, dataTypeSize: -1, dataTypeModifier: 44, format: binary), PostgresNIO.RowDescription.Column(name: \"person_activities_description\", tableOID: 16671, columnAttributeNumber: 3, dataType: TEXT, dataTypeSize: -1, dataTypeModifier: -1, format: binary), PostgresNIO.RowDescription.Column(name: \"person_activities_reward\", tableOID: 16671, columnAttributeNumber: 4, dataType: INTEGER, dataTypeSize: 4, dataTypeModifier: -1, format: binary), PostgresNIO.RowDescription.Column(name: \"person_activities_last_performed\", tableOID: 16671, columnAttributeNumber: 5, dataType: TIMESTAMPTZ, dataTypeSize: 8, dataTypeModifier: -1, format: binary), PostgresNIO.RowDescription.Column(name: \"person_activities_created_at\", tableOID: 16671, columnAttributeNumber: 6, dataType: TIMESTAMPTZ, dataTypeSize: 8, dataTypeModifier: -1, format: binary), PostgresNIO.RowDescription.Column(name: \"person_activities_deleted_at\", tableOID: 16671, columnAttributeNumber: 7, dataType: TIMESTAMPTZ, dataTypeSize: 8, dataTypeModifier: -1, format: binary), PostgresNIO.RowDescription.Column(name: \"person_activities_person_id\", tableOID: 16671, columnAttributeNumber: 8, dataType: UUID, dataTypeSize: 16, dataTypeModifier: -1, format: binary)], cells: [Optional(ByteBuffer { readerIndex: 0, writerIndex: 16, readableBytes: 16, capacity: 16, storageCapacity: 1024, slice: _ByteBufferSlice { 508..<524 }, storage: 0x0000000141045a00 (1024 bytes) }), Optional(ByteBuffer { readerIndex: 0, writerIndex: 4, readableBytes: 4, capacity: 4, storageCapacity: 1024, slice: _ByteBufferSlice { 528..<532 }, storage: 0x0000000141045a00 (1024 bytes) }), Optional(ByteBuffer { readerIndex: 0, writerIndex: 11, readableBytes: 11, capacity: 11, storageCapacity: 1024, slice: _ByteBufferSlice { 536..<547 }, storage: 0x0000000141045a00 (1024 bytes) }), Optional(ByteBuffer { readerIndex: 0, writerIndex: 4, readableBytes: 4, capacity: 4, storageCapacity: 1024, slice: _ByteBufferSlice { 551..<555 }, storage: 0x0000000141045a00 (1024 bytes) }), nil, Optional(ByteBuffer { readerIndex: 0, writerIndex: 8, readableBytes: 8, capacity: 8, storageCapacity: 1024, slice: _ByteBufferSlice { 563..<571 }, storage: 0x0000000141045a00 (1024 bytes) }), nil, Optional(ByteBuffer { readerIndex: 0, writerIndex: 16, readableBytes: 16, capacity: 16, storageCapacity: 1024, slice: _ByteBufferSlice { 579..<595 }, storage: 0x0000000141045a00 (1024 bytes) })], lookupTable: [\"person_activities_title\": 1, \"person_activities_id\": 0, \"person_activities_deleted_at\": 6, \"person_activities_reward\": 3, \"person_activities_created_at\": 5, \"person_activities_person_id\": 7, \"person_activities_last_performed\": 4, \"person_activities_description\": 2]), decoder: PostgresKit.PostgresDataDecoder))","createdAt":"2023-03-13T18:26:37Z"}],"name":"Bob"}]}
It looks like I'm not loading the activities
and rewards
correctly when I look at the description
field which is an OptionalField
but I am not sure where I am going wrong.