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 mixin schema.Entity() #2449

Merged
merged 1 commit into from
Feb 25, 2023
Merged

feat: Add mixin schema.Entity() #2449

merged 1 commit into from
Feb 25, 2023

Conversation

ntucker
Copy link
Collaborator

@ntucker ntucker commented Feb 22, 2023

Fixes #416 .

Motivation

Provide more flexible (composition) ways of utilizing the useful Entity code.

Solution

https://resthooks.io/rest/api/schema.Entity

class User {
  username = '';
  id = '';
  createdAt = new Date(0);
}

Extending

class UserSchema extends schema.Entity(User) {
  pk(value) { return value.id }

  static schema = {
    createdAt: Date,
  }
}

Options

const UserSchema = schema.Entity(User, {
  pk() {
    return this.id;
  },
  key: 'User',
  schema: { createdAt: Date },
});
const UserSchema = schema.Entity(User, {
  pk: 'id',
  key: 'User',
  schema: { createdAt: Date },
});

@vercel
Copy link

vercel bot commented Feb 22, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
rest-hooks ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 25, 2023 at 0:57AM (UTC)

@codecov
Copy link

codecov bot commented Feb 22, 2023

Codecov Report

Base: 97.62% // Head: 97.66% // Increases project coverage by +0.04% 🎉

Coverage data is based on head (117f625) compared to base (f5b82f2).
Patch coverage: 100.00% of modified lines in pull request are covered.

❗ Current head 117f625 differs from pull request most recent head dde29b5. Consider uploading reports for the commit dde29b5 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2449      +/-   ##
==========================================
+ Coverage   97.62%   97.66%   +0.04%     
==========================================
  Files         123      124       +1     
  Lines        2024     2060      +36     
  Branches      302      306       +4     
==========================================
+ Hits         1976     2012      +36     
  Misses         27       27              
  Partials       21       21              
Impacted Files Coverage Δ
...ckages/rest-hooks/src/selectors/useDenormalized.ts 100.00% <ø> (ø)
packages/endpoint/src/schemas/Entity.ts 96.77% <100.00%> (-1.45%) ⬇️
packages/endpoint/src/schemas/EntitySchema.ts 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@ntucker ntucker mentioned this pull request Feb 22, 2023
2 tasks
@ntucker ntucker force-pushed the schema.entity branch 4 times, most recently from 69c9567 to 55b0b8a Compare February 23, 2023 22:38
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.

Use an existing class for a resource
1 participant