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

feat: Add indexes to Entity for improved performance #237

Merged
merged 2 commits into from Jan 17, 2020
Merged

feat: Add indexes to Entity for improved performance #237

merged 2 commits into from Jan 17, 2020

Conversation

ntucker
Copy link
Collaborator

@ntucker ntucker commented Jan 16, 2020

Fixes #232.

Motivation

Sometimes you want to request data via different parameters.
Say you have user data. It has both an id and a username. When someone visits their profile you want to have the url have the username. Thus it would be best to look it up via that username. So you add an endpoint that supports that.

const user = useResource(UserResource.detailShape(), { username: 'bob' });

Unfortunately, because username is not the primary key, this cannot be inferred from entity cache alone. This means if we visited a user list page, then went to that user's profile page - we would have to wait while the new request finishes. This is quite silly, as a request with the primary key would instantly resolve (one of the unique benefits of Rest Hooks).

Solution

export class UserResource extends Resource {
  readonly id: number | undefined = undefined;
  readonly username: string = '';
  readonly email: string = '';
  readonly isAdmin: boolean = false;

  pk() {
    return this.id?.toString();
  }

  static urlRoot = 'http://test.com/user/';

  static indexes = ['username'];
}

Now you can:

const user = useResource(UserResource.detailShape(), { username: 'bob' });

And get results before the fetch completes!

Note: Inferred results are always considered stale and will revalidate when useResource().

@kmartinezmedia
Copy link
Contributor

🚀

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.

RFC: Improve performance with arbitrary indexes
2 participants