ngxs-labs / data

NGXS Persistence API
https://ngxs-labs.github.io/data
81 stars 18 forks source link

@Computed parameterized #629

Closed Beef-Tech closed 3 years ago

Beef-Tech commented 3 years ago

Hi,

I'm using an NgxsDataEntityCollectionsRepository to store multiple entities. Each entities can be grouped by some properties. For example : { currency: 'EUR', group: 'Program1', receivedAmount: 20
}, { currency: 'USD', group: 'Program1', receivedAmount: 5
}, { currency: 'EUR', group: 'Program2', receivedAmount: 15
}
Can I simply get sum-ed receivedAmount for group 'Program1' for example ?

I was thinking of using @Computed, maybe I'm wrong ?

Thanks.

splincode commented 3 years ago

Hello

@Computed()
public get sum(): number {
 const snapshot = this.snapshot;
 return snapshot.ids.reduce((sum: number, id: number) => {
      return sum + snapshot.entities[id]. receivedAmount;
 }, 0);
}
Beef-Tech commented 3 years ago

Thanks, But if I would like to group by a given parameter like "program", how Can I do ?

Thanks.

splincode commented 3 years ago

Move to https://github.com/Angular-RU/angular-ru-sdk/issues/863