-
Notifications
You must be signed in to change notification settings - Fork 68
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
Singular Resource Handling (read/write operations) #98
base: master
Are you sure you want to change the base?
Conversation
Thanks @bilouw ❤️ ! I think this looks good but @wadetandy might have a better idea? |
Thanks for your feedback @richmolj ! I just added two features:
Solution: I made the id argument optional and id will not be merge in endpoint if it's present and if singularResource = true.
Solution: The url() function (model.ts) should singuralize model name (/user instead of /users) to build endpoint if singularResource = true. What do you think about that @richmolj ? |
src/scope.ts
Outdated
@@ -65,7 +65,7 @@ export class Scope<T extends SpraypaintBase = SpraypaintBase> { | |||
return this._buildCollectionResult(response) | |||
} | |||
|
|||
async find(id: string | number): Promise<RecordProxy<T>> { | |||
async find(id?: string | number): Promise<RecordProxy<T>> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the functionality of this whole PR is good, but as it's an uncommon case I don't love the type change of making id
optional to find here, as for 99% of projects a missing id is incorrect and will result in confusing errors that the type system could easily catch. Perhaps we can come up with a parallel method that is only used for singular resources? findOnly()
load
, i'm not loving those but I'm open to other ideas.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My idea was to let the singularResource variable determine the way the find() function behave. But i understand your point, i thought about making a parallel method too. does get()
suit you for the method name, or is it too generic ?
Also, with my changes calling find('someId') will construct an endpoint url without id if singularResource = true
, so maybe another method is not necessary and we could just use the method like this: find('whatever')
leaving the id of find() mandatory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about findSingle
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm agree with this name. I just made the change, can this be merge ?
Hi Everyone !
Problem: If you try to save a Spraypaint model, it will automatically add the ID of the resource at the end of the url. With Singular Resource in rails, we don't need id to update a resource.
Solution:
I want to improve Spraypaint to be able to use it with Singular Resource.
For now, i do some tricks to handle singular resource reading.
Today, i just want to unlock write operations.
Instead of: PATCH /api/user/:id -> PATCH /api/user
So i added an singularResource attribute to Spraypaint models. If this attributes equal true, then making an update will call the endpoint without id in url.
If i have more time, i will use this singularResource attribute in the future to fully handle Singular Resource in Spraypaint.
It's my first PR ever, so tell me if i'm doing a terrible thing :). (or if my idea is good or bad)