JackAdams / meteor-transactions

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

Use JSON.stringify to package updates #19

Closed rjsmith closed 9 years ago

rjsmith commented 9 years ago

This PR is an attempt to solve the problem of saving update commands into the Transactions collection that contain '$' modifiers, like $each, as illustrated in this Jasmine test:

   it ('can be updated with $addToSet modifier using $each', function () {
      // SETUP
      var newBars = [{bar: 4}, {bar: 5}];

      // EXECUTE
      tx.start('update foo');
      fooCollection.update(
        {_id: insertedFooDoc._id},
        {
          $addToSet: {
            foo: {$each: newBars}
          }
        },
        {tx: true});
      tx.commit();

      // VERIFY
      var fooCursor = fooCollection.find(
        {foo: {bar: 4}});
      expect(fooCursor.count()).toBe(1);      
      var recoveredFoo = fooCursor.fetch()[0];

      // Check transaction
      var txDoc = tx.Transactions.findOne({_id: recoveredFoo.transaction_id});
      expect(txDoc.items.updated[0].update).toEqual({ command: '$addToSet', data: [ { key: 'foo', value: { json: '{"$each":[{"bar":4},{"bar":5}]}' } } ] });
    })

It saves objects and arrays as JSON - stringified strings, in a {json: } sub-object, and then uses JSON.parse() if the packaged command value has a json field, when unpackaging the serialised command ready for execution.

This should work when undo-ing Transaction documents created before this commit (they won't have the json sub-object, so will just read it straight out as an object).

This change has been successfully tested in my own meteor-transactions test repo here: https://github.com/rjsmith/meteor-transactions-bug-repo, in particular this test

rjsmith commented 9 years ago

Related to issue #14

rjsmith commented 9 years ago

Added initial testapp for issue #21 including tests covered by this PR

JackAdams commented 9 years ago

Thanks, Richard. This is a pretty significant step in the evolution of this package.

JackAdams commented 9 years ago

Okay. There have been a number of bugfixes and improvements since the last release. You've verified that most of the changes are working with jasmine tests. I've done some acceptance testing in my app. I'm about ready to release a new version. Thoughts?

rjsmith commented 9 years ago

As far as I am concerned, that would be great. As the MIT license says:

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND ...

JackAdams commented 9 years ago

Hahaha... done! Just released 0.6.17.