vapor / fluent-mysql-driver

🖋🐬 Swift ORM (queries, models, relations, etc) built on MySQL.
MIT License
77 stars 52 forks source link

Add custom JSON encoder / decoder support #189

Closed tanner0101 closed 4 years ago

tanner0101 commented 4 years ago

Adds support for configuring which JSON coders MySQL uses when serializing nested data to the database (#189).

// Setup custom JSON coders that use unix timestamps
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .secondsSince1970
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .secondsSince1970

// Configure MySQL database.
app.databaes.use(.mysql(
    configuration: ..., 
    encoder: MySQLDataEncoder(json: encoder), 
    decoder: MySQLDataDecoder(json: decoder)
))

Note that for dates specifically, you can use @Timestamp formats to configure formatting per field.

tanner0101 commented 4 years ago

Adds changes from https://github.com/vapor/mysql-kit/pull/285

tanner0101 commented 4 years ago

These changes are now available in 4.0.0-rc.2.2