What is @ngrx/schematics?
@ngrx/schematics is a collection of Angular schematics for generating NgRx files such as actions, reducers, effects, and more. It helps streamline the process of setting up and maintaining NgRx in Angular applications.
What are @ngrx/schematics's main functionalities?
Generate Store
This command generates a new NgRx store with the specified state and sets it up in the root module. It creates necessary files and boilerplate code for the store.
ng generate @ngrx/schematics:store State --root --module app.module.ts
Generate Actions
This command generates a new set of actions for a specified feature (e.g., User). It creates an actions file with predefined action types and classes.
ng generate @ngrx/schematics:action User
Generate Reducer
This command generates a new reducer for a specified feature (e.g., User). It creates a reducer file with initial state, reducer function, and necessary imports.
ng generate @ngrx/schematics:reducer User
Generate Effects
This command generates a new effect for a specified feature (e.g., User). It creates an effects file with boilerplate code for handling side effects in NgRx.
ng generate @ngrx/schematics:effect User
Other packages similar to @ngrx/schematics
@angular/cli
@angular/cli is the official Angular CLI tool that provides a wide range of commands for generating and managing Angular projects. While it does not specifically focus on NgRx, it can be extended with custom schematics to include NgRx-related functionality.
nx
Nx is a set of extensible dev tools for monorepos, which helps you develop like Google, Facebook, and Microsoft. It provides powerful CLI commands for generating and managing Angular applications, including support for NgRx. Nx offers more advanced features for managing large-scale projects compared to @ngrx/schematics.
schematics-utilities
schematics-utilities is a utility library for Angular schematics that provides helper functions for creating and modifying Angular projects. It can be used to create custom schematics, including those for NgRx, but requires more manual setup compared to @ngrx/schematics.
16.0.0-beta.0 (2023-04-27)
Bug Fixes
- data: allow 0 to be a valid key (#3830) (e50126d), closes #3828
- effects: run user provided effects defined as injection token (#3851) (cdaeeb6), closes #3848
- store: correctly infer action group events defined as empty object (#3833) (dc78447)
- store-devtools: correctly import state when feature is set to true (#3855) (0df3419), closes #3636
Features
- component-store: add selectSignal method for interop with Angular Signals (#3861) (195d5ac)
- component-store: add tapResponse signature with observer object (#3829) (3a5e5d8)
- effects: accept ObservableInput as concatLatestFrom argument (#3838) (34dd28c)
- router-store: add @nrwl/angular data persistence operators (#3841) (4482751), closes #3777
- router-store: remove deprecated getSelectors method (#3816) (351a75e), closes #3815
- schematics: replace
any
type with unknown
type (#3827) (0ea2933) - schematics: Use createActionGroup in schematics (#3791) (f6ce20f)
- store: add createMockStore migration (#3810) (ded4240)
- store: add selectSignal method for interop with Angular Signals (#3856) (999dcb6)
- store: preserve the event name case with createActionGroup (#3832) (482fa5d)
- store: remove deprecated createFeature method (#3825) (fd8f347), closes #3814
- store: remove forbidden chars and empty str checks from createActionGroup (#3857) (e37c57b)
- convert entity and router selectors interfaces to types (#3853) (73eb55c)
- store: remove getMockStore in favor of createMockStore (#3835) (8d0ed8e)
- store: use strict projectors for createFeature selectors (#3799) (bafd121)
- update to Angular v16.0.0-next.6 release (#3831) (d7e03df)
BREAKING CHANGES
- store: The event name case is preserved when converting to the action name by using the
createActionGroup
function.
BEFORE:
All letters of the event name will be lowercase, except for the initial letters of words starting from the second word, which will be uppercase.
const authApiActions = createActionGroup({
source: 'Auth API',
events: {
'LogIn Success': emptyProps(),
'login failure': emptyProps(),
'Logout Success': emptyProps(),
logoutFailure: emptyProps(),
},
});
// generated actions:
const { loginSuccess, loginFailure, logoutSuccess, logoutfailure } = authApiActions;
AFTER:
The initial letter of the first word of the event name will be lowercase, and the initial letters of the other words will be uppercase. The case of other letters in the event name will remain the same.
const { logInSuccess, loginFailure, logoutSuccess, logoutFailure } = authApiActions;
- store: The
createFeature
signature with root state is removed in favor of a signature without root state.
An automatic migration is added to remove this signature.
BEFORE:
interface AppState {
users: State;
}
export const usersFeature = createFeature<AppState>({
name: 'users',
reducer: createReducer(initialState /* case reducers */),
});
AFTER:
export const usersFeature = createFeature({
name: 'users',
reducer: createReducer(initialState /* case reducers */),
});
- router-store: The deprecated
getSelectors
function has been removed from the @ngrx/router-store
package.
BEFORE:
The @ngrx/router-store package exports the getSelectors
function.
AFTER:
The @ngrx/router-store package no longer exports the getSelectors
function. A migration has been provided to replace existing usage
- schematics: NgRx Schematics do not use
any
types to define actions, these are replaced with the unknown
type.
BEFORE:
Schematics used the any
type to declare action payload type.
AFTER:
Schematics use the unknown
type to declare action payload type.
- store: The
getMockStore
function is removed in favor of createMockStore
BEFORE:
import { getMockStore } from '@ngrx/store/testing';
const mockStore = getMockStore();
AFTER:
import { createMockStore } from '@ngrx/store/testing';
const mockStore = createMockStore();
- store: Projectors of selectors generated by createFeature are strongly typed.
BEFORE:
Projector function arguments of selectors generated by createFeature are not strongly typed:
const counterFeature = createFeature({
name: 'counter',
reducer: createReducer({ count: 0 }),
});
counterFeature.selectCount.projector;
// type: (...args: any[]) => number
AFTER:
Projector function arguments of selectors generated by createFeature are strongly typed:
const counterFeature = createFeature({
name: 'counter',
reducer: createReducer({ count: 0 }),
});
counterFeature.selectCount.projector;
// type: (featureState: { count: number; }) => number
<a name="15.4.0"></a>