dynamoid icon indicating copy to clipboard operation
dynamoid copied to clipboard

Add write transaction

Open btree1970 opened this issue 5 years ago • 6 comments

Add transaction write feature to DynamoId.

  • Currently only works for adding multiple write operations

btree1970 avatar Aug 24 '20 05:08 btree1970

Looks pretty good 👍 .

The only issue I see is that transact method works just for the one model/table but DynamoDB's transaction can involve different tables in one call. I would like to have only one flexible API for write transactions. That's why I would like transact to work with several tables.

The only reason why transactions aren't added yet is that I am not sure what API should be.

I have following thoughts:

  • API should be high-level and operate model classes instead of table names
  • operations are hight-level and names match already existing methods like update_attributes/update_fields/upsert/...
  • callbacks should be called, at least some of them (e.g. for creation/deletion)

I considered following interface:

# Dyhamoid::Document.transaction or User.transaction

Dynamoid::Document.transaction do |txn|
  txn.create(User, attributes)
  txn.update(User, attributes)
  txn.update_fields(User, attributes, conditions)
  txn.upsert(User, attributes)
  txn.delete(User, id)
end

So please let me know what do you think about it.

andrykonchin avatar Aug 24 '20 14:08 andrykonchin

Looks pretty good 👍 .

The only issue I see is that transact method works just for the one model/table but DynamoDB's transaction can involve different tables in one call. I would like to have only one flexible API for write transactions. That's why I would like transact to work with several tables.

The only reason why transactions aren't added yet is that I am not sure what API should be.

I have following thoughts:

  • API should be high-level and operate model classes instead of table names
  • operations are hight-level and names match already existing methods like update_attributes/update_fields/upsert/...
  • callbacks should be called, at least some of them (e.g. for creation/deletion)

I considered following interface:

# Dyhamoid::Document.transaction or User.transaction

Dynamoid::Document.transaction do |txn|
  txn.create(User, attributes)
  txn.update(User, attributes)
  txn.update_fields(User, attributes, conditions)
  txn.upsert(User, attributes)
  txn.delete(User, id)
end

So please let me know what do you think about it.

Hey thanks for the response. I admit i did this PR with a specific business case in mind. I like your suggestion for the new api design. Will work on it over the next few days.

btree1970 avatar Aug 24 '20 21:08 btree1970

Great.

I believe that proposed above approach isn't as good and consistent as it could be so there is room for improvement. For instance txn.create(User, attributes) seems ugly to me but I don't see better alternative.

andrykonchin avatar Aug 25 '20 18:08 andrykonchin

@andrykonchin what's the status on transact write support, is it planned? any help needed?

dpsk avatar Jul 27 '21 08:07 dpsk

Transactions aren't supported right now. The main issue is that it isn't clear to me how the transactions API should look. Any opinion/proposition is welcomed.

andrykonchin avatar Jul 31 '21 13:07 andrykonchin

Have there been any more thoughts on this? Below is an alternate API idea using an array of arrays. An array of hashes could work too. Personally I prefer single table usage of DynamoDB so don't have a need to span tables but I see why it should be supported.

Dynamoid::Document.transaction([
  [User, :create, attributes],
  [User, :update, attributes],
  [User, :update_fields, attributes, conditions],
  [User, :upsert, attributes],
  [User, :delete, id]
])

ckhsponge avatar Sep 12 '23 05:09 ckhsponge