I'm using Akita entity data stores to cache data on the front end of an Angular application.
In my service I have some functions something like this:
setPropertyOfEntity(entityId, newValue): void {
store.update(entityId, {propertyName: newValue});
this.saveEntity(store.getEntity(entityId));
}
saveEntity(entity) {
httpClient.put(url, entity).subscribe((savedEntity) -> {
store.upsert(savedEntity.id, savedEntity)
});
}
Now, understandably, the Akita store kicks out a whole lot of events as the entity is updated in the store (property gets set, upsert after save).
I feel I want to update the store after save in case of side effects on the entity done by serverside logic (which does happen in this case).
What is the Akita-way when it comes to keeping FE/BE in sync like this?
I guess I could get the entity from the store, set value on it, then call the save function but that seems counter to what seems to be the Akita way of using store.update() to update properties on objects.
As Akita retuns immutables from its getEntity() function I end up with something that just feels wrong (assign(), not using akita builtin store.update())....
setPropertyOfEntity(entityId, newValue): void {
let storeEntity = store.getEntity(entityId);
let mutableEntity = Object.assign(new Entity(), storeEntity)
mutableEntity.property = newValue;
this.saveEntity(mutableEntity);
}