Fullstack-2
Fullstack-2
1.5.1 @ngrx/store
Core of NgRx: Manages application state using a single immutable
state tree.
Actions: Describe what to do (e.g., ADD_ITEM).
Reducers: Describe how the state changes in response to actions.
Selectors: Retrieve slices of the state efficiently.
Example:
export const addItem = createAction('[Items] Add Item', props<{
item: string }>());
export const itemsReducer = createReducer([], on(addItem, (state, {
item }) => [...state, item]));
1.5.2 @ngrx/effects
Handles Side Effects: For asynchronous operations like API calls
or logging.
Key Feature: Keeps side effects outside reducers.
Example:
@Injectable()
export class ItemsEffects {
loadItems$ = createEffect(() =>
this.actions$.pipe(
ofType(loadItems),
switchMap(() => this.http.get('/api/items').pipe(map(items =>
loadItemsSuccess({ items }))))
)
);
constructor(private actions$: Actions, private http: HttpClient) {}
}
1.5.3 @ngrx/router-store
Integration with Angular Router: Synchronizes router state with
NgRx store.
Selectors: Access route-related data like parameters or query
strings.
Example:
const selectRouteParams = createSelector(selectRouterState,
(router) => router.state.params);
1.5.4 @ngrx/entity
Simplifies Entity State Management: Provides helpers for
managing collections.
Features:
o EntityAdapter: Create, update, and delete records in the
state.
Example:
export const adapter = createEntityAdapter<Item>();
export const initialState = adapter.getInitialState();
1.5.5 @ngrx/component-store
For Local State Management: Used for stateful Angular
components.
Example:
@Injectable()
export class CounterStore extends ComponentStore<{ count:
number }> {
constructor() {
super({ count: 0 });
}
readonly increment = this.updater(state => ({ count: state.count +
1 }));
}
1.5.6 @ngrx/signals
Reactive Signals Integration: Enables state and event management
with reactive signal APIs.
Useful for fine-grained reactivity in Angular components.
1.5.7 @ngrx/operators
Utility Operators: Enhance observables for NgRx state
management.
Examples: select, onErrorResumeNext.
1.5.8 @ngrx/data
Simplifies CRUD Operations: Provides entity-specific services.
Example:
entityService.add(entity); // Adds a new entity
1.5.9 @ngrx/component
Reactive Component Store: Supports component-based state and
reactivity.
class Example {
@Log
greet(name: string) {
return `Hello, ${name}`;
}
}
const obj = new Example();
obj.greet('TypeScript'); // Logs: Called: greet with args: [
'TypeScript' ]
app.use(express.json());
res.send('User is valid');
});
User.find().byName('Alice').exec(console.log);
4.1.3 Mongoose with TypeScript for Type Safety
Integrating TypeScript provides stronger type safety and better
tooling for Mongoose models.
o Example:
import { Schema, model, Document } from 'mongoose';