aws / aws/aws-record-ruby

Record Instance Update

Open
#59 4 comments 1 reaction 0 assignees View on GitHub
feature-request
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.