Can postgres-kit encode Swift Decimal type to a Postgres numeric column type?

Viewed 48

I'm using vapor/postgres-kit (and by extension vapor/sql-kit) to insert some Codable conformant Swift objects into a PostgreSQL table. Some of the Swift object properties are of type Decimal, which are being used to represent monetary values accurately where Float or Double do not otherwise work reliably. The Postgres columns that store these Swift Decimal types are of the Postgres type numeric, which is a great match for accuracy/precision.

The default Encodable conformance of Decimal results in a Swift String, which then arrives in the SQL statement as a quoted value, which causes Postgres to throw an error because it's not expecting a string value for a numeric column type.

As far as I can see from looking around inside the SQLQueryEncoder class, there's nowhere to hook in to instead produce a value of something like SQLLiteral.numeric(String), which is a type that already exists in sql-kit.

My question is, can this be achieved somehow or do I need to abandon generating Inserts/Updates using Codable conformance in favour of manually forming queries in order to customise how the Decimal types are handled?

Thanks in advance for any advice!

0 Answers
Related