ITADN

question about decoding collections of `SQLRow`

#186Closedjaredh159 创建于 2024-12-18
J
jaredh159commented
caveat: this might be a dumb question. I'm using SQLKit in a semi-low level ORM-ish thing I rolled myself with a limited scope, that's been working very well for a few years. I have a convenience decoding function defined like this: ```swift public extension SQLRow { func decode<M: DuetSQL.Model>(_: M.Type) throws -> M { try self.decode(model: M.self, prefix: nil, keyDecodingStrategy: .convertFromSnakeCase) } } ``` To decode my own models. Works great. In my actual postgres client, I decode rows like so: ```swift try rows.compactMap { row in try row.decode(M.self) } ``` I've noticed while doing some optimization on a few slower query/endpoints, that decoding models for certain large requests is a non-trivial part of the overall request time. I was wondering if the iteration and individual decoding could be improved by decoding in bulk. I wanted to write something like this: ```swift public extension Collection where Element == SQLRow { func decodeAll<M: DuetSQL.Model>(_: M.Type) throws -> [M] { ??? // <-- 🤔 } } ``` But (as indicated by the `???`) I can't figure out how to implement this. Is it possible? I poked around with `SQLRowDecoder` and `SQLQueryFetcher` but couldn't figure out how to hold it right. Would greatly appreciate any insight. Thanks so much for the library!
关闭于 2025-08-30 2 条评论