JackAdams / meteor-transactions

App level transactions for Meteor + Mongo
http://transactions.taonova.com/
MIT License
113 stars 16 forks source link

Add public function to manually add context to current transaction #41

Closed Data-Meister closed 8 years ago

Data-Meister commented 8 years ago

I think it would be useful to be able to explicitly set transaction context.

It makes sense for the context to apply to the whole transaction. A transaction is a "single action", and it's good to have context describing what that action is. But then it's counter-intuitive that you can only set context when you are adding an individual query. Why not set it explicitly on the level of the transaction itself?

The method you describe in the docs - selling an explicit context on the first query - is not neat in my case. I often add a list of queries to a transaction in a loop, so I would have to add context on the first iteration of the loop but not the others.

Anything which could be described as "a use case" for a piece of software could reasonably be recorded in a context object. Thus, the possibilities are almost infinite. In many cases adding context as you add each query makes sense. But in my case, and perhaps other developers it seems neater to just set the transaction context explicitly in one go.

I want to be able to do:

tx.start("TxnType")

... [ Do Queries ]

tx.setContext( context )

tx.commit()

Currently I'm doing:

tx._context = context

tx.commit()

I realise this is a hack and might break in future versions. So I hope you will consider this pull request :)

Data-Meister commented 8 years ago

P.S. I'll happily update the docs if you accept

JackAdams commented 8 years ago

Yes. You're right. Setting context needs some attention, but I'm wondering whether it might be neater to set context when starting the transaction, rather than via a public function. e.g.

tx.start('add comments', {context: {post_id: "dgt234rehe346ijhh"}})
  ...
tx.commit();

Just a thought. I guess we could support both methods of setting the context for a whole transaction.

Data-Meister commented 8 years ago

Absolutely I agree - setting context at the start of the transaction makes a lot of sense. There's natural synergy with the transaction description. I'll write some code for that now.

Maybe no harm in supporting both approaches though. I can imagine situations where you might want to set it at then end, or even set it a few times as you go along.

Data-Meister commented 8 years ago

Okay I've added the ability to specify context when starting a transaction, as you outlined above