Stamplay / stamplay-js-sdk

:rocket: JavaScript SDK of Stamplay cloud platform
https://stamplay.com
MIT License
88 stars 18 forks source link

Preventing parameters of ".fetch()" to overwrite currentQuery #12

Closed chiefGui closed 9 years ago

chiefGui commented 9 years ago

Hey guys!

I was unable to do the retrieve the posts I expected of the following operation:

let posts = new Stamplay.Cobject('posts').Collection

posts
  .equalTo('blog', 'blogname')
  .fetch({ populate_owner: true })
  .then(() => {
    // do something!
  })

Basically, the SDK was requesting that query to the following address:

https://myapp.stamplayapp.com/api/cobject/v1/posts?populate_owner=true

But, what's the point?

The endpoint seemed weird to be, since it wasn't passing the .equalTo() condition I previously specified. By then, I decided to investigate and ended figuring out that the problem was that the parameters of the .fetch() method were being overwritten the currentQuery mounted by the .compile() method, which is the responsible for creating recursively the parameters specified by methods like the .equalTo(). In others word, if you are passing any parameter to .fetch() method, such as { populate: true }, your filtering breaks and priorize any .fetch() argument.

With this PR, the final endpoint of that same query is:

https://myapp.stamplayapp.com/api/cobject/v1/posts?populate_owner=true&blog=blogname

So, it worked! :smile: And sorry for not writing any test—seems this scenario isn't being covered as far as I could realize.

Finally, it is backward compatible! :ok_hand:

A temporary version of this release I launched here for testing purposes.

Thank you guys! Hope it helps!