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

Add a toJSON bundled hook like mongoose #18

Closed
ekryski opened this issue Feb 26, 2016 · 6 comments
Closed

Add a toJSON bundled hook like mongoose #18

ekryski opened this issue Feb 26, 2016 · 6 comments
Labels

Comments

@ekryski
Copy link
Member

ekryski commented Feb 26, 2016

No description provided.

@daffl
Copy link
Member

daffl commented Feb 26, 2016

I'm wondering if we should start the common hook collection module right away and provide a transform hook that does something like

var transform = require('feathers-common-hooks/transform');

app.service('todos').after(transform('toJSON'));
app.service('todos').after(transform(function(data) {
 // Do something here
}));

Which in the first case will call hook.result.toJSON and in the second run the function.

@ekryski
Copy link
Member Author

ekryski commented Feb 26, 2016

Ya I'm liking that idea. Especially because we have to handle arrays and pagination sometimes I can see that as a potential pitfall if people have to implement the hooks themselves.

@ekryski
Copy link
Member Author

ekryski commented May 3, 2017

No longer required as of v2.0.0 since it returns regular JS objects by default.

@ekryski ekryski closed this as completed May 3, 2017
@subodhpareek18
Copy link

subodhpareek18 commented Jun 21, 2017

I think this is still required.

I have use case of few related models. Upon using include, by default the values returned are firstly flattened out:

{
   "role.roleId": "",
   "role.name": ""
}

instead of

{
  "role": {
    "roleId": "",
    "name": ""
  }
}

Just this would've been fine as it'd be easy to convert the first result to second. But the real issue comes when the included results are an array, in which case the above result doesn't contain all values, just one. (Let's say if roles had a bunch of permissions related to it, it'd just return one permission and not all)

So there is no option but to use { raw: false } on the models. Which of course breaks hooks like discard remove and many more I imagine.

@subodhpareek18
Copy link

Though of course a very simple solution exists:

all: [
  hook => ({
    ...hook,
    result: hook.result.toJSON()
  }),
  discard('password')
]

But I'd say a transform hook is still a good idea, because the explicit presence of it in the docs will really help people.

@eikaramba
Copy link
Contributor

eikaramba commented Jul 2, 2017

I just updated my dependencies and now my results are also flattened like above. Why is that the case? That does not make sense for me.

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

No branches or pull requests

4 participants