Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for JSONB, other nested data structures #45

Open
CharlieIGG opened this issue Feb 22, 2019 · 7 comments
Open

Support for JSONB, other nested data structures #45

CharlieIGG opened this issue Feb 22, 2019 · 7 comments

Comments

@CharlieIGG
Copy link

My backend server is using PostgreSQL as a DB.
For a specific field in my model, I have a JSONB column.

When I call:

myResourceInstance.update({ 
  jsonField: {stuff: 1234}
})

Then the update reaches my server without this parameter (basically it updates the model instance without any actual changes).

If I instead send some other primitive data type as the value of jsonField then I can see it come through in the request's parameters.

Any ideas how to make this work, without moving away from the JSON:API standard in my server?

@nicklandgrebe
Copy link
Owner

I made some specs to reproduce but they passed. Can you post the resource class definition?

@CharlieIGG
Copy link
Author

This is the real thing:

import resourceLibrary from './library'

class Car extends resourceLibrary.Base {
    static define() {
        this.attributes('year', 'prices')
        this.belongsTo('engine')
        this.belongsTo('make')
        this.belongsTo('model')
    }
}

export default resourceLibrary.createResource(Car);

As a temporary workaround for this I've decided to call

myResourceInstance.update({ 
  jsonField: JSON.stringify({stuff: 1234})
})

And setup a before_save filter on my Rails app that parses that JSON before attempting to save it.

@nicklandgrebe
Copy link
Owner

update only sends changed fields to the server. If an attribute is not added in attributes then it won't be sent:

this.attributes('year', 'prices', 'jsonField')

@CharlieIGG
Copy link
Author

@nicklandgrebe my example didn't match the real-world definition of the resource itself. My bad.

In the real example prices is the JSONB field. And as you can see it was already declared.

@nicklandgrebe nicklandgrebe reopened this Feb 28, 2019
@nicklandgrebe
Copy link
Owner

If instead of using update you use assignAttributes, what does changedFields return?

@CharlieIGG
Copy link
Author

@nicklandgrebe I'll test it out either today or tomorrow and get back to you. 👍

@CharlieIGG
Copy link
Author

CharlieIGG commented Mar 12, 2019

Finally had a chance to test this out.

if I call:

car.assignAttributes({
        prices: update,
})

I get:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants