routes pre(s)
YoannMa opened this issue · comments
I see you using this sometimes :
const { credentials: { id: currentUserId } } = request.auth;
Might be a good idea to put it in a pre
instead ?
I do find myself using pres less often in this repo than is typical, as I try some new things out. Pres have also proven a little awkward when using transactions—have you had similar experiences?
As for the currentUserId
, what do you think about this approach? Would you still prefer a pre? https://github.com/devinivy/hapipal-realworld-example-app/blob/master/lib/routes/articles-feed.js#L20
I haven't used a pre
with a transaction and yes that would be tricky.
I thought about this method becoming a pre
so it could give a example for other peoples but indeed if you need a transaction, it would be cleaner to use an Helper method like you did 👌
Anyways, It's your call 😄
It's really two different issues. I don't need a transaction to get the current user's id, but I do need (want) one for fetching data that could be affected by any service method that mutates data during the course of the request.
For example, in articles-favorite
I display the article in the same transaction that I favorite it because the response includes my "favorited" status with that article, and I consider it a post-condition of this route that the article always appear "favorited."
I totally agree with you on that, and I did understand that you wouldn't need a transaction to just fetch the current user.
In the currentUserId
case, both a pre
or the Helper's method would work, I have no problem with the helper method.
Cool, thanks for the thoughts :)