I was trying to implement some form of repsertBy: repsert where the key is a provided Unique, in the line of getBy, upsertBy etc.
My approach: implement it on top of upsertBy. Now upsertBy takes a unique constraint, a record, and a list of changes to apply in case of a unique collision. To implement repsertBy I'd like that list of changes to be “assign all fields to the new value”.
repsertBy :: (MonadIO m, PersistRecordBackend record backend)
=> Unique record -> record
-> ReaderT backend m (Entity record)
repsertBy unique record = upsertBy unique record [ entifyField =. value | … ]
And there I'm stuck.
I can generate the list of values by calling toPersistValue on the record's toPersistFields. But where can I get the EntityFields from?
I'd have expected them to be available somewhere from the entity definition at èntityDef, but haven't found any as of now. I've tried comparing with actual backends' implementations of replace and upsert, but only found SQL-level string banging.
I'm currently spelling out the fields by hand, but at some point I'm going to add one in the entity and forget to update it in the repsertBy. Is there any way to access the EntityFields?