updateEntitiesIds weird behavior #177
Replies: 11 comments
-
We have a guard in place that assures that the new entity id does not already exist. It would make sense to add a guard that assures that the entity id you want to update actually exists. Following the logic above, it could throw an error if the entity id does not exist to make the develop explicitly aware of a potential issue in the app. However, I'd like to hear more from you about a potential use-case to ignore the non-existing entity rather than to throw. @strigefleur , could you please describe your use-case? |
Beta Was this translation helpful? Give feedback.
-
I wonder how you have a case where you pass an id that doesn't exist. |
Beta Was this translation helpful? Give feedback.
-
The entity is created on the frontend, whereas the real entity id is database-side step. So until the backend process the entity I have -1 as temp id. Backend is requested with a complex object that may or may not contain that kind of temp entity, it responds with another complex object that always contains related entity with it's real id. So I was going to shorten the code and reassign the id in any case - skipping the explicit check for it's existence in the store: if the id was -1 it's updated to real, if it was real, nothing changes. |
Beta Was this translation helpful? Give feedback.
-
do you mind showing some pseudo-code? |
Beta Was this translation helpful? Give feedback.
-
// object with temporary id
interface Foo {
id: number;
// rest
}
// update object that is sent to backend
interface FooBarUpdateRequest {
foo?: Foo;
// rest
}
// response object that backend responses with
interface FooBarUpdateResponse {
foo: Foo;
// rest
}
// we create some entity with temp id
addNewFoo() {
store.update({
addEntities({
id: -1,
somedata: '',
})
})
}
// some service logic here
// foo is sent to backend only if it's temporary
const hasTempFoo = store.pipe... entities.some(x => x.id === -1)...
const request: FooBarUpdateRequest = {
foo: hasTempFoo ? foo : undefined;
// rest
};
http.post<FooBarUpdateResponse>('ourawesomeurl', request).pipe(
tap((update) => {
store.update(
// rest
// here case 1 is when temporary id exists, we update it to new id
// case 2 is when no temporary id exists - it means the store already has same id inside entities
updateEntitiesIds(-1, update.foo.id)
)
})
) |
Beta Was this translation helpful? Give feedback.
-
Currently I workaround it by simply doing full check before update: const storeHasTempId = store.pipe... entities.some(x => x.id === -1)...
if (storeHasTempId) {
store.. updateEntitiesIds(-1, update.foo.id)
} else {
//...
} so it's up to you to decide what is the correct behavior in that case. Anyway it should not create |
Beta Was this translation helpful? Give feedback.
-
We can ignore them, not throw an error |
Beta Was this translation helpful? Give feedback.
-
Errors are an effective way to communicate to the developer about unexpected behavior. This actually made me look at the current implementation of the const idThatDoesNotExist = 5;
store.update(updateEntities(idThatDoesNotExist, entity => ({ ...entity, name: 'name' }))); And it is a good thing that it would throw an error. The developer would see it and would know what the issue is. It's much worse if the Error is not thrown... a potential bug is then introduced in the application and the developer is not aware of it. Case in point, take a look what's going to happen in the following code const idThatDoesNotExist = 5;
store.update(updateEntities(idThatDoesNotExist, { name: 'name' })); Even though there's no entity with id 5 and there's nothing to update, the Elf would insert a new entity to the store: I'd argue that we should throw an error in both cases, the |
Beta Was this translation helpful? Give feedback.
-
I have no idea how these cases arise. I've never experienced it. There is no need to protect each line of code in the library. |
Beta Was this translation helpful? Give feedback.
-
In my experience, these situations are rare now. However, when I was starting out as a developer they would be more often. Mostly it's a result of some sort of a race condition. Imagine calling the functionality to I'd be curious to hear opinions from the authors of libraries out there. I'll try soliciting feedback on Twitter. Although I don't have a big following there. |
Beta Was this translation helpful? Give feedback.
-
Which @ngneat/elf-* package(s) are the source of the bug?
entities
Is this a regression?
No
Description
If you try to feed
updateEntitiesIds
with non-existing (or just negative, idk)id
it would updateentities
collection by addingundefined: {}
object and nothing toids
collection.I expect it to ignore update if the
id
provided doesn't exist. In my case it looked likewhere I wanted to update temporary entity (if there's any) with real
id
.Please provide a link to a minimal reproduction of the bug
No response
Please provide the exception or error you saw
No response
Please provide the environment you discovered this bug in
No response
Anything else?
No response
Do you want to create a pull request?
No
Beta Was this translation helpful? Give feedback.
All reactions