Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to add multiple entities to the store using ngrx/data -> saveEntities. So far I got this:

@Injectable()
export class MaintenanceEntityService extends EntityCollectionServiceBase<Maintenance> {

  constructor(
    private serviceElementsFactory: EntityCollectionServiceElementsFactory,
    private entityCacheDispatcher: EntityCacheDispatcher) {
    super('Maintenance', serviceElementsFactory);
  }

  addBatch(maintenances: Maintenance[]) {
    const changes: ChangeSetItem[] = [
      cif.add('Maintenance', maintenances)
    ];
    const changeSet: ChangeSet = { changes, tag: 'Creating alert batch'};

    return this.entityCacheDispatcher.saveEntities(changeSet, `${environment.API}maintenances`).pipe(
      map(response => response)
    );
  }
}

My endpoint receives and return an object like this:

interface ChangeSet<T = any> {
  changes: ChangeSetItem[]
  extras?: T
  tag?: string
}

In the backend I insert all entities into the database and then before returning the same object I update ids, but once again in the Angular app, the store is not getting updated. Am I missing something?

https://ngrx.io/api/data/EntityCacheDispatcher#saveentities


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

Ok, the ChangeSet object has a property called ChangeSetItem with the next structure:

type ChangeSetItem = ChangeSetAdd | ChangeSetDelete | ChangeSetUpdate | ChangeSetUpsert;

interface ChangeSetAdd<T = any> {
  op: ChangeSetOperation.Add
  entityName: string
  entities: T[]
}

So I need to return the corresponding entityName from the backend so it can be added to the store: In my case: Maintenance

Not sure why the property is not being set in this line:

const changes: ChangeSetItem[] = [
      cif.add('Maintenance', maintenances)
    ];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...