Replies: 4 comments
-
Council notes
|
Beta Was this translation helpful? Give feedback.
-
Problem with using This means pk might look like this So if we are to keep parent the same, then is no obvious way to detect that we want to use the args rather than the parent for the instanceKey parameter. This would look like: class User extends IDEntity {
name = '';
username = '';
email = '';
todos: Todo[] = [];
static key = 'User';
static schema = {
todos: new schema.Collection(new schema.Array(Todo), (parent, key) => ({
userId: parent.id,
})),
};
}
const userTodos = new schema.Collection(
new schema.Array(Todo),
(parent, key, args) => ({
args[0].userId,
}),
); Instead we could make the second argument an object and use key to determine what the function should take: class User extends IDEntity {
name = '';
username = '';
email = '';
todos: Todo[] = [];
static key = 'User';
static schema = {
todos: new schema.Collection(new schema.Array(Todo), {
nestKey: (parent, key) => ({ userId: parent.id })
}),
};
}
const userTodos = new schema.Collection(
new schema.Array(Todo),
{
argsKey: ({ userId }) => ({ userId }),
},
); |
Beta Was this translation helpful? Give feedback.
-
InsertionsShould .push really push to every collection it adds? Perhaps it should only push to exact urlParams match, and others it should run insertion? Insertion should be defined by collection. Must provide PK parameters to know exactly how to run it |
Beta Was this translation helpful? Give feedback.
-
Implemented in #2593, released in [email protected] and [email protected] |
Beta Was this translation helpful? Give feedback.
-
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.
Use case patterns
Previously
https://resthooks.io/docs/concepts/atomic-mutations#create
Problems:
Solution
Inspiration: Backbone collections
Data
Endpoints
Resources will have sane defaults making most use cases not need to specify. So we're going to zoom in and manually create endpoints so it's clear how they interface.
Usage
This will add to these if they are already in store (but only if they already exist):
Collection
Collection's second argument is a key computation. By default it is the identity method. This is called with either
...args
when used in an endpoint, or(parent, key)
when used in another entity.Store shape
Unions
Since unions don’t have keys like entities, we can have an optional argument to set collection key if it can’t grab it from the schema sent.
Ergonomic shortcuts
RestEndpoint push/unshift/insert
Since REST patterns often keep similarity between create and list endpoints, we can add special extenders. This let's us simply call
getTodos.push
instead of having to create a new endpoint for create.Before:
After:
Resources
Resource.create will simply exist as Resource.getList.push(). However, you can still use those manually:
Collection.insert cmp argument
Can be a function, or a string referring to the key that should be compared. Or an Array referring to the keys that should be compared (order matters)
Open questions
Should we make Array a collection as well? Mostly concerned about the
[Todo]
type definitions.parent => ({ [ParentEntity.key.lowerFirst() + 'Id']: ParentEntity.pk(parent) })
I'm thinking maybe Collection should just be a type of entity. So it's stored in the same lookup table and most code paths won't need to change.
Beta Was this translation helpful? Give feedback.
All reactions