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

Singular Resource Handling (read/write operations) #98

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

bilouw
Copy link
Collaborator

@bilouw bilouw commented Jun 22, 2021

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:

export default class User extends ApplicationRecord {
  static jsonapiType = 'users'
  static singularResource = true
}

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)

@richmolj
Copy link
Contributor

Thanks @bilouw ❤️ ! I think this looks good but @wadetandy might have a better idea?

@bilouw bilouw changed the title Singular Resource Handling (read/write operations) Singular Resource Handling (write operations) Jun 22, 2021
@bilouw
Copy link
Collaborator Author

bilouw commented Jun 24, 2021

Thanks for your feedback @richmolj ! I just added two features:

  1. Problem: We can't call .find() without id to fetch singular resource.

Solution: I made the id argument optional and id will not be merge in endpoint if it's present and if singularResource = true.

  1. Problem: Let's say we have a singular resource User with jsonApiType = 'users'. We want to call enpoint '/user' and not '/users'., without overriding the endpoint variable.

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 ?

@bilouw bilouw changed the title Singular Resource Handling (write operations) Singular Resource Handling (read/write operations) Jun 24, 2021
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>> {
Copy link
Contributor

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.

Copy link
Collaborator Author

@bilouw bilouw Jun 25, 2021

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about findSingle?

Copy link
Collaborator Author

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 ?

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.

3 participants