Skip to content

lazy data serialization #53

@tanner0101

Description

@tanner0101

Currently PostgresData values are serialized to the expected data format upon initialization. For example:

import Foundation
import PostgresNIO

let date = PostgresData(date: Date())

Upon initialization, date contains TIMESTAMPTZ formatted bytes ready for querying. The Date supplied is discarded.

While this approach is simple, it has a draw back. Take the following query:

SELECT $1::JSON as "date"

If we attempted to pass the date data from above to this query, we would get a warning followed by an error:

2019-10-18T15:12:42-0400 warning: bind $1 type (TIMESTAMPTZ) does not match expected parameter type (JSON)
2019-10-18T15:12:42-0400 error: invalid byte sequence for encoding "UTF8": 0x00 (report_invalid_encoding)

It makes sense that TIMESTAMPTZ should not be convertible to JSON, however, the original Date object we had earlier does know how to convert to JSON. It can do so via Codable and the JSONEncoder.

If PostgresData could support lazy serialization of Swift types, then we could support passing a Date to this query dynamically.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions