Skip to content

Support Types that have a dynamic Postgres typeΒ #333

@fabianfett

Description

@fabianfett

The current PostgresEncodable:

/// A type that can encode itself to a postgres wire binary representation.
public protocol PostgresEncodable {
    // TODO: Rename to `PostgresThrowingEncodable` with next major release

    /// identifies the data type that we will encode into `byteBuffer` in `encode`
    static var psqlType: PostgresDataType { get }

    /// identifies the postgres format that is used to encode the value into `byteBuffer` in `encode`
    static var psqlFormat: PostgresFormat { get }

    /// Encode the entity into the `byteBuffer` in Postgres binary format, without setting
    /// the byte count. This method is called from the ``PostgresBindings``.
    func encode<JSONEncoder: PostgresJSONEncoder>(into byteBuffer: inout ByteBuffer, context: PostgresEncodingContext<JSONEncoder>) throws
}

requires that all instances of a type are encoded into the same PostgresDataType because of the static requirement. This is bad, specifically because supporting custom types (like Postgres enums) requires a dynamic OID.

For this reason @gwynne proposes adding a PostgresDynamicTypeEncodable and PostgresDynamicTypeThrowingEncodable, that use dynamic psqlType and psqlFormat. PostgresEncodable should then inherit from the PostgresDynamicTypeThrowingEncodable and PostgresNonThrowingEncodable from PostgresDynamicTypeEncodable. Love how we ended up at the place with NonThrowing and just Throwing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions