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

Reduce toJSON implementation: use the power of bookshelf #9423

Merged
merged 1 commit into from
Feb 14, 2018

Conversation

kirrg001
Copy link
Contributor

@kirrg001 kirrg001 commented Jan 25, 2018

no issue

pick('shallow', 'baseKey', 'include', 'context')

We don't have to specify allowed options i think - no security risk.
But we will re-add validation, but then with the official way: use filterOptions. One of the next PR's.


We return all fetched relations (pre-defined with withRelated) by default.
You can disable it with shallow:true.

no issue

- `omitPivot` does remove pivot elements from the JSON obj
- `shallow` allows you to not return relations
- no need to remember `this.include` anymore
  - this was only remembered to select the relations
- make use of `serialize`, see http://bookshelfjs.org/docs/src_base_model.js.html#line260
@ErisDS
Copy link
Member

ErisDS commented Jan 26, 2018

The toJSON implementation hasn't been updated in ages - so bookshelf has undoubtably added features to fix some of the issues we had by now.

I don't remember a lot of why things are the way they are anymore though - so I'm a little cautious over:

a) removing the pick - this is stylistic, I prefer when passing options around to use clone and or pick to avoid issues in future:

  • clone for stopping pass by reference bugs
  • pick means you can be sure forever exactly what properties are considered used - which is great when in 4 years you need to refactor this code again 😉
    b) why passing include is not needed anymore in certain cases? - this is probably code evolution 😁 and nothing to worry about.

Otherwise this looks 👍

@kirrg001
Copy link
Contributor Author

kirrg001 commented Jan 26, 2018

a) removing the pick - this is stylistic, I prefer when passing options around to use clone and or pick to avoid issues in future:

#9425 clones the options object and makes use of permitted options (permitted options were already in place).
#9425 also adds it for toJSON (even thought it's not really required for toJSON, no security risk, because we don't request the database - but i thought consistency is good.

why passing include is not needed anymore in certain cases?

This PR only removes the use of include for the .forge function, because the fn initialize no longer remembers this.include. Why?

if (_.includes(self.include, fullKey)) {

fullKey was always the name of the relation e.g. tags
And we double checked if include contained tags - but that does not make sense, you asked for it 😁

@ErisDS
Copy link
Member

ErisDS commented Feb 13, 2018

Maybe baseKey was to do with nested relations? It might be that bookshelf or bookshelf-relations has superseded the need for this.

Things like /posts/?include=users.roles or /tags/?include=count.posts

If you're happy that all the usecases still work, then I'm happy for this to be merged!

@kirrg001
Copy link
Contributor Author

/tags/?include=count.posts

count.posts is a virtual include and this is handled by defining a serialize fn in our include-count plugin.

/posts/?include=users.roles

The post model has no relation to users, only to author.

author.roles works though, but currently not allowed in the API layer.

The whole support of fetching nested relation is a bookshelf feature and unrelated to the our old toJSON implementation. Bookshelf-relation is not responsible for fetching data, only for updates.


I would merge this and see if we encounter any problems 👍

@kirrg001 kirrg001 merged commit 9ede590 into TryGhost:master Feb 14, 2018
@ErisDS ErisDS removed their assignment Jun 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants