Record Instance Update
- Dominant language
- Ruby
- Stars
- 318
- Forks
- 43
- Avg merge
- 19h
- Merged PRs (30d)
- 1
Description
Right now `save` (or `save!`) encapsulates the selection between a put and an update, with the ability to force put. I recently wanted a "find or create" method, and naively did:
```
record = MyRecord.find(key: 123)
record ||= MyRecord.new(key: 123)
...
record.save! #=> Conditional write exception
```
I end up receiving the conditional write exception because some other process is trying to create the same record.
To fix this, I moved to using an update:
```
MyRecord.update(key: 123, value: 'some new data')
```
However, we have a GSI which is dynamically generated from the values which `update` never takes into account (see issue #58). Thus, the preferred solution would be:
```
MyRecord.new(key: 123, value: 'some new data').upsert!
```
To achieve this I copied the `else` statement from `ItemOperations#_perform_save` which builds an update request for dirty columns and creates the record if it does not already exist; hence `upsert`.
Contributor guide
Assessment
This issue has not been assessed yet.