codewinsdotcom/PostgresClientKit

RowDecoder

khanlou opened this issue · 6 comments

I've written a RowDecoder, which lets you easily convert from a PostgresClientKit.Row to a Codable struct, like

struct User: Codable {
    let id: Int
    let name: String
}

It's working well in my project, and if you think it'd be good to add to this library, let me know and I would be happy to clean it up and pull request it.

Sure, that would be great!

For the PR, please use develop as the base branch (instead of master).

Or, if you prefer, feel free to send me (or point me to) whatever you currently have, and I can integrate it (and credit you in the release notes).

Thanks!

Here it is:

https://gist.github.com/khanlou/ff3e8788026b35150a9670b87b676ad2

Dates get a little hairy, because you do need a timezone to convert some of the fields to date. I think UTC is a reasonable default since that's what most servers should be running, but you could also parameterize the TZ on RowDecoder.

Thanks! I hope to take a look this weekend.

This looks good; thank you.

I'm going to try adding a couple of things:

  • Making PostgresTimestampWithTimeZone, PostgresTimestamp, PostgresDate, and PostgresTime conform to Decodable. I think this is the cleanest way to get dates/times out. I like your suggestion of parameterizing the time zone on the Decoder, if someone wants to decode to a Foundation Date property.

  • Add a second flavor of Decoder that can be used if retrieveColumnMetadata is false, by assuming the result set columns are in CodingKeys order (or, if CodingKeys is synthesized, in declaration order).

I like both of those ideas. I’m messing with time zone heavy code today and assuming utc on decode I’m not sure is the right decision.

Released in v1.5.0