entitiesPropsFactory: return CRUD functions similar to propsFactory #141
Replies: 4 comments 12 replies
-
It's a nice idea, but there are two advantages:
|
Beta Was this translation helpful? Give feedback.
-
getting back to this idea as I thought of a potentially different approach. What if we allowed the following: import {
entitiesPropsFactory,
addEntities,
selectAll,
setEntities,
updateEntities,
selectEntity
} from '@ngneat/elf-entities';
const { todoEntitiesRef, withTodoEntities, todoCrudFactory } = entitiesPropsFactory('todo');
const { addTodoEntities, selectAllTodo, setTodoEntities, updateTodoEntities, selectTodoEntity } = todoCrudFactory({
addEntities,
selectAll,
setEntities,
updateEntities,
selectEntity
}); The const ref; // the reference to the todoEntitiesRef in the closure
const name; // the name passed into entitiesPropsFactory... in this example "todo"
function todoCrudFactory(args) {
const result = {};
if (args.addEntities) {
result[`add${name}Entities`] = (entities, options) => {
return args.addEntities(entities, Object.assign(options, { ref }));
}
}
} The above code is super rough draft. |
Beta Was this translation helpful? Give feedback.
-
So I'm working on this feature and I'm thinking about how the I think it'd be better if we did something like: const featuredUpdatersMap = {
selectAll: `selectAll${feature}`,
selectAllApply: `selectAll${feature}Apply`,
updateEntities: `update${feature}Entities`
// ... etc
}
function crudFactory(updaters) {
const res = {};
for (const key in updaters) {
res[featuredUpdatersMap[key]] = // ... something. This issue is discussed further
}
return res;
} This is short and sweet (never mind the absence of types). The biggest part would be the But now, the second issue... we need to override the function so that it's called with the correct The way I propose to solve this is by binding the function crudFactory(updaters) {
const res = {};
for (const key in updaters) {
res[featuredUpdatersMap[key]] = updaters[key].bind({ ref });
}
return res;
} We would have to update all functions to account for this. For example, here's the change in the // previous code:
const { ref: { entitiesKey, idsKey } = defaultEntitiesRef } = options;
// new code
const ref = options.ref ?? this.ref ?? defaultEntitiesRef;
const { entitiesKey, idsKey } = ref; What do you think about this? |
Beta Was this translation helpful? Give feedback.
-
To be honest, I don't think it's worth it for now. |
Beta Was this translation helpful? Give feedback.
-
propsFactory
returns convenience functions to update the value in the store:I wonder if something similar would make sense for
entitiesPropsFactory
:using
itemEntitiesRef
with all crud functions and selectors works, but having the functions above available would make code a bit easier to read:Beta Was this translation helpful? Give feedback.
All reactions