Git Product home page Git Product logo

Comments (10)

fabianfett avatar fabianfett commented on June 10, 2024

How do you decode the row? can you show your code?

from postgres-nio.

fwgreen avatar fwgreen commented on June 10, 2024

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.

fabianfett avatar fabianfett commented on June 10, 2024

This is very much a problem at the SQLKit/PostgresKit/Fluent layer and not a problem with PostgresNIO.

from postgres-nio.

gwynne avatar gwynne commented on June 10, 2024

@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.

fwgreen avatar fwgreen commented on June 10, 2024

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.

gwynne avatar gwynne commented on June 10, 2024

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.

gwynne avatar gwynne commented on June 10, 2024

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.

fwgreen avatar fwgreen commented on June 10, 2024

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.

fabianfett avatar fabianfett commented on June 10, 2024

@fwgreen Can we close this issue?

from postgres-nio.

fwgreen avatar fwgreen commented on June 10, 2024

Closing. Thanks for all your efforts.

from postgres-nio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.