Comments (10)
How do you decode the row? can you show your code?
from postgres-nio.
I'm just using a string array:
protocol TeamMember {
static var jobTitle: String {get}
}
struct Nurse : TeamMember, Codable { static var jobTitle: String { "RN"} }
struct Assistant : TeamMember, Codable { static var jobTitle: String { "NA" } }
//...
struct WorkAssignmentView<T : TeamMember> : Codable {
var id: Int
var assignee: String
var unitTelephone: String
var rooms: [String] //<--- Here!
var jobTitle: String
var hasAssignment: Bool
var roomCount: Int
var teamMemberId: String
enum CodingKeys: String, CodingKey {
case id, assignee, rooms
case unitTelephone = "unit_telephone"
case jobTitle = "job_title"
case hasAssignment = "has_assignment"
case roomCount = "room_count"
case teamMemberId = "team_member_id"
}
}
//...
//I was hoping that I wouldn't have to manually decode the array...
func listAll() throws -> [WorkAssignmentView] {
let query = "SELECT * FROM work_assignment_view WHERE job_title = $1"
return try dbClient.query(query, [T.jobTitle.asPgData]).wait()
.compactMap { try $0.sql().decode(model: WorkAssignmentView.self) }
}
//...
let nurseAssignments = try WorkAssignmentView<Nurse>.listAll()
from postgres-nio.
This is very much a problem at the SQLKit/PostgresKit/Fluent layer and not a problem with PostgresNIO.
from postgres-nio.
@fwgreen Can you show the CREATE TABLE
statement used to create the underlying table? (Or your migration, if you're using Fluent)
from postgres-nio.
It's the product of a rather messy view:
drop view if exists work_assignment_view;
create view work_assignment_view as
select
tm.team_member_id as team_member_id,
tm.job_title as job_title,
tm.first_name || ' ' || tm.last_name as assignee,
(
select
count(patient_room.id)
from
patient_room,
nursing_team
where
patient_room.room_number = nursing_team.room_number
and (
tm.team_member_id = nursing_team.nurse_id
or tm.team_member_id = nursing_team.assistant_id
)
) as room_count,
(
select
case when count(patient_room.id) > 0 then true else false end
from
patient_room,
nursing_team
where
patient_room.room_number = nursing_team.room_number
and (
tm.team_member_id = nursing_team.nurse_id
or tm.team_member_id = nursing_team.assistant_id
)
) as has_assignment,
(
select
coalesce(jsonb_agg(patient_room.room_number) filter(where patient_room.room_number is not null), '[]')
from
patient_room,
nursing_team
where
patient_room.room_number = nursing_team.room_number
and (
tm.team_member_id = nursing_team.nurse_id
or tm.team_member_id = nursing_team.assistant_id
)
) as rooms,
(
select unit_telephone.telephone_number
from
unit_telephone
where
unit_telephone.team_member_id = tm.team_member_id
) as unit_telephone,
row_number() over () as id
from
team_member tm,
work_availability wa
where
tm.team_member_id = wa.team_member_id
and wa.is_on_shift = true
group by
tm.id;
from postgres-nio.
Ah, yeah, this will end up with the rooms
array coming back as a straight-up JSONB
value rather than the expected TEXT[]
. You can work around it with an ARRAY
constructor and the jsonb_array_elements_text()
function:
select
ARRAY(jsonb_array_elements_text(coalesce(jsonb_agg(patient_room.room_number) filter(where patient_room.room_number is not null), '[]')))
Or by using array_agg()
instead of jsonb_agg()
, if possible in your use case:
select
coalesce(array_agg(patient_room.room_number) filter(where patient_room.room_number is not null), [])
from postgres-nio.
Ironically, if PostgresKit didn't have explicit workarounds to make arrays work in general, this problem would not come up (the value would be decoded as JSON to an array on the Swift side), but then other uses of arrays would stop working right.
from postgres-nio.
Thanks for the suggestion. I was being too inflexible and a plain text array works fine. After all, in my case, it is just a view.
from postgres-nio.
@fwgreen Can we close this issue?
from postgres-nio.
Closing. Thanks for all your efforts.
from postgres-nio.
Related Issues (20)
- Error detail regression HOT 40
- async/await prepared statement API
- Allow PostgresConnection creation without specifying an EventLoop
- `serverClosedConnection` error can never occur
- Merge `PostgresDynamicTypeThrowingEncodable` with `PostgresEncodable`
- Investigate adopting Tracing HOT 1
- Do not require passing a logger on every call HOT 1
- 1.18 crashes on Linux HOT 10
- CI: Investigate running tests in release mode HOT 1
- Successfully closing connection logs `PSQLError`
- Composable PostgresQuery HOT 4
- Allow changing Postgres auth credentials while ConnectionPool is alive HOT 2
- Support marking connection as going away
- Support changing the number of allowed streams on a connection HOT 1
- Error "How can we receive a read, if the connection is closed" HOT 7
- Double ping pong that crashes the app. HOT 2
- Connection Pool Crash: Precondition failure when ping and close happen at the same time
- ConnectionPool: Allow connection scoring/preference, for example for a certain EventLoop HOT 1
- Fatal error: How can we receive a read, if the connection is closed HOT 3
- TIMESTAMP columns don't return the same value as was inserted HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from postgres-nio.