Based on ngrx docs we should have 3 entity collection services.

const userService = EntityCollectionServiceFactory
const companyService = EntityCollectionServiceFactory
const addressService = EntityCollectionServiceFactory

Or later based on ngrx docs we may have 3 own services which extend EntityCollectionServiceBase<T>.

const userService: UserEntityService;
const companyService: CompanyEntityService;
const addressService: AddressEntityService;

For ngrx-entity-relationship both will work.

What we should keep in mind is that @ngrx/data is built on services. Therefore, our selectors for related entities should be provided via services too.

So, let's simply create a service that injects the services as dependencies, and defines root selectors which return entities with relationships. The approach is the same as for @ngrx/store and @ngrx/entity, we need rootEntity and relatedEntity functions from ngrx-entity-relationship, but instead of defining selectors in files, we should define them in the service.

@Injectable({providedIn: 'root'})
export class UserSelectorService {
// important dependencies
private store: Store,
private user: UserEntityService,
private company: CompanyEntityService,
private serviceFactory: EntityCollectionServiceFactory,
) {}
// a root selector for single users
public readonly selectUser =
// @ngrx/data service of the User entity.
// user.company
// @ngrx/data service of the Company entity.
// the key in the user's model
// that points to the company id.
// the key in the user's model
// where the company entity will be set.
// company.address
// creating @ngrx/data service of the Address entity.
// the key in the company's model
// that points to the address id.
// the key in the company's model
// where the address entity will be set.

Besides single users, we can select an array of users. For that, we need rootEntities.

export class UserSelectorService {
// ...
// a root selector for arrays of users
public readonly selectUsers = rootEntities(
// simply pass here a select for a single entity.
// ...

Now, to use the selectors, we need to inject the created service with root selectors in desired components:

export class MyComponent {
store: Store,
userSelectors: UserSelectorService,
) {
this.user$ = store.select(
this.users$ = store.select(
['1', '2'],


Last updated on by MG