feat: Add schema.Collection#2593
Merged
Merged
Conversation
🦋 Changeset detectedLatest commit: 62d48bb The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
| } else { | ||
| if (typeof schema.denormalizeOnly === 'function') { | ||
| localCacheKey[pk] = schema.denormalizeOnly(entityCopy, unvisit); | ||
| localCacheKey[pk] = schema.denormalizeOnly(entityCopy, args, unvisit); |
Check warning
Code scanning / CodeQL
Prototype-polluting assignment
ntucker
force-pushed
the
collections
branch
3 times, most recently
from
April 20, 2023 17:37
f190842 to
dcda13e
Compare
ntucker
force-pushed
the
collections
branch
2 times, most recently
from
April 20, 2023 20:29
8bbe78e to
6c0f8ae
Compare
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## master #2593 +/- ##
==========================================
- Coverage 98.06% 97.98% -0.08%
==========================================
Files 131 135 +4
Lines 2165 2287 +122
Branches 328 357 +29
==========================================
+ Hits 2123 2241 +118
Misses 19 19
- Partials 23 27 +4
☔ View full report in Codecov by Sentry. |
ntucker
force-pushed
the
collections
branch
2 times, most recently
from
April 21, 2023 01:48
4c6bcf1 to
23d2d7f
Compare
ntucker
force-pushed
the
collections
branch
2 times, most recently
from
April 24, 2023 00:16
23de592 to
34a5e08
Compare
ntucker
force-pushed
the
collections
branch
5 times, most recently
from
April 24, 2023 03:40
890c1ff to
7eadbab
Compare
fix: changes to
Collaborator
Author
|
Will update with insertions in a follow up PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2566, #1548, #2602, #2553, #2554, #428, #398, #358 .
Motivation
Atomic mutations are one of the core values of Rest Hooks. However, while update was in the first pre-release, and delete quickly added for version 1, create has always been a bit awkward.
Often creating a new element you are on a view where you want to see it added to a list somewhere.
One of the challenges here is that there can be many distinct lists you view. Furthermore, where something is added to a list isn't always the same.
Previously
https://resthooks.io/docs/concepts/atomic-mutations#create
Problems:
TODO
Solution
Collections: Entities but for Arrays and Values instead of classes.
They literally are entities, so they get normalized as such. They just have different lifecycle implementations.
Inspiration: Backbone collections
Usage
This will add to these if they are already in store (but only if they already exist):
Collection
construction
Takes Array or Values as first arg. Second arg is options object.
Options:
Must provide argsKey or nestKey to compute a serializable object to be used in PK table. argsKey takes ...args and nestKey takes (parent,key) as inputs.
createCollectionFilteris optional and can be used to override the default filter algorithm. This is used by the "adders" as they inherit from the collection.addWith(merge, createCollectionFilter)
This is the general
collection extendermethod. All specializations simply call this method. This is also used in the paginator as it has a custom algorithm.mergeoverrides theEntity.merge()algorithm. This is used to determine where new values are placed (end, beginning, somewhere in the middle?)createCollectionFilteris optional and will override the inherited function from the Collection itself.push/unshift/assign
These all call addWith() with variations on merge to place newly created objects at the beginning or end respectively for Arrays.
assignis for Values() and simply merges (there is no concept of order in Values)RestEndpoint adder specializations
For a RestEndpoint containing a collection schema, the extenders
push/unshift/assigncan be used as convenience to create an appropriate endpoint. They use the parent schema, but us their respectiveaddWith()variations. Additionally this sets themethodto "POST".Pagination
.paginated() now detects whether there is a collection or array. With collections it will use a custom extender. Arrays will work via update.
new .paginated('cursor') - 99% of paginations simply extract one field from the args. Let the user specify that field name rather than writing a whole function.
Normalizr changes
argsare now part of both normalization and denormalization. Of course these will default to an empty array in case users don't need args. However, they will need to be passed on my implementations such as schema.normalize(). This shouldn't be a problem as schema implementations without it wont be expecting it anyway.pk()now takes a fourth argument - args.next/createResource
searchParamsonly applies to getList and create.bodyonly applies to update,create,partialUpdate