NgRx Entities: Namespaces with TypeScript

Note: This will be a series of posts around how I moved from custom built reducers and state management with NgRx to use the NgRx Entity library.


After working with NgRx for a while and writing custom reducers, I figured it was time to dive into adding the Entity library. My hesitancy at first was that I had already gone down the road of creating my own reducers and switching to use entities I felt would be a hill to climb — I was wrong.

A great reference book that goes deeper into this topic: Architecting Angular Applications with Redux, RxJS, and NgRx

Namespaces to the rescue

All of my store and states were based on a main interface state for the given part of the store. This was useful because my store only ever cared about the state as it got passed around. Now, with the introduction of entities, there turns out to be a lot of other goodies that the state of the store needed per item. To package all of these up, I ended up creating a namespace for each item as their own sub-store.

For example, let’s imagine we are storing books in our store.

Before

1
2
3
4
5
6
7
8
9
10
11
// books.state.ts

export interface BooksState {
books: Book[];
selectedBook: Book;
}

export const initialBooksState: BooksState = {
books: null,
selectedBook: null,
}

After

Adding entities ends up blowing up the state file, but it provides a ton of functionality that makes things a lot easier later on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// books.state.ts

export namespace BooksStore {
export interface State extends EntityState<Book> {
selectedBook: number | null;
}

export const adapter: EntityAdapter<Book>
= createEntityAdapter<Book>();

export const initialState: State = adapter.getInitialState({
selectedBook: null,
});

export const {
selectAll,
selectEntities,
selectIds,
selectTotal,
} = adapter.getSelectors();
}

A couple things to point out with the code above is that the new state interface extends the default entity state inferring the book interface. Hidden in here is the default collection state as that is the entity key array provided by the extension.

The adapter is also new, but that is where the magic of the entity library comes from. It allows me to set an initial state and also provides all the selectors needed to grab the items from the store. It also provides the methods for adding to the store in the reducer.

In use

Now with everything packaged up in the namespace of BookStore it makes it very simple to know what item you are using not only in shared context files, but also provides a similar API for each store.

1
2
3
4
// store/state/index.ts
export { BookStore } from './books.state.ts';
export { PagesStore } from './pages.state.ts';
...
1
2
3
4
5
6
// store/state/app.state.ts
const initialAppState: AppState = {
books: BooksStore.initialState,
pages: PagesStore.initialState,
...
}
1
2
3
4
5
6
7
8
9
// store/reducers/books.reducer.ts
const { adapter } = BooksStore;

const booksReducers = (
state = BooksStore.initialState,
action: BooksActions,
): BooksStore.State => {
...
}

Using the namespace setup allows for easy segmentation on what each item exported should do. It also allows for a shared API amongst other sub-stores so that the overhead of remembering what the state was named is less.

Filed under: Code