Blob columns #66
-
What's the recommended approach for columns using a BLOB type? Taking inspiration from the UUID implementation, I tried this: extension MyValue {
public struct BytesRepresentation: QueryRepresentable {
public var queryOutput: MyValue
public init(queryOutput: MyValue) {
self.queryOutput = queryOutput
}
}
}
extension MyValue.BytesRepresentation: QueryBindable {
public var queryBinding: QueryBinding {
let data = try! JSONEncoder().encode(queryOutput)
return .blob([UInt8](data))
}
}
extension MyValue.BytesRepresentation: QueryDecodable {
public init(decoder: inout some QueryDecoder) throws {
let bytes = try [UInt8](decoder: &decoder)
let data = Data(bytes)
let spec = try JSONDecoder().decode(MyValue.self, from: data)
self.init(queryOutput: spec)
}
}
extension MyValue.BytesRepresentation: SQLiteType {
public static var typeAffinity: SQLiteTypeAffinity {
[UInt8].typeAffinity
}
} Then I can use it with the @Table
structure TableValue {
@Column(as: MyValue.BytesRepresentation)
var value: MyValue
} I don't love using the force unwrap, but you could use |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
@seanmrich Can you explain what you're trying to achieve here? The library comes with a
Do you mean the force extension MyValue.BytesRepresentation: QueryBindable {
public var queryBinding: QueryBinding {
do {
return try .blob([UInt8](JSONEncoder().encode(queryOutput)))
} catch {
return .invalid(error)
}
}
} |
Beta Was this translation helpful? Give feedback.
@seanmrich Can you explain what you're trying to achieve here? The library comes with a
Codable.JSONRepresentation
type for doing this kind of thing. Are you trying to do JSONB, instead?Do you mean the force
try!
? TheQueryBinding
type has aninvalid
case for error handling: