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
, andPostgresTime
conform toDecodable
. I think this is the cleanest way to get dates/times out. I like your suggestion of parameterizing the time zone on theDecoder
, if someone wants to decode to a FoundationDate
property. -
Add a second flavor of
Decoder
that can be used ifretrieveColumnMetadata
is false, by assuming the result set columns are inCodingKeys
order (or, ifCodingKeys
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