1.4.4 • Published 2 years ago

@zionapps/ngrx-core v1.4.4

Weekly downloads
205
License
-
Repository
-
Last release
2 years ago

@zionapps/ngrx-core

Core NgRx Library

Build status

Requirements

  • Angular < 6
  • NgRx < 6
  • RxJs < 6

How to Use this Library?

  1. File Structure
  2. Domain
  3. Actions
  4. Reducers
  5. Selectors
  6. Effects
  7. Module

1. File Structure

Inside the folder for each domain

  • app-message.actions.ts
  • app-message.domain.ts
  • app-message.module.ts
  • app-message.reducer.ts
  • app-message.selectors.ts
  • app-message.utils.ts

2. Domain Example

import {EntityState} from '@ngrx/entity';
import {
  INgRxCoreModuleConfig,
  INgRxCoreLastResult,
} from '@zionapps/ngrx-core';

export interface Item {
  id?: number;
  // Add additional properties
}

export interface ItemsState extends EntityState<Item> {
  currentId: number | null;
  lastResult: INgRxCoreLastResult | null;
  loaded: boolean;
  // Add custom properties
}

export const ItemsDomain: INgRxCoreModuleConfig = {
  autoSaveDelay: 100,
  endpoint: 'https://www.endpoint.com',
  idProperty: 'id',
  initialStateMap: {
    // Add custom properties or override defaults
  },
  storeName: 'Items',
  timeoutForRequests: 30000,
};

3. Actions Example

Use the standard approach

import { generateNgRxCoreActions, INgRxCoreActionSet } from '@zionapps/ngrx-core';
import { MyItem, MyItemsConfig} from './my-items.domain';

export const MyItemsActions: INgRxCoreActionSet<MyItem, number> = generateNgRxCoreActions<MyItem, number>(MyItemsConfig.pluralName);

Extend with more actions:

  • generateNgRxCoreEmptyPayloadAction()
  • generateNgRxCoreErrorPayloadAction()
  • generateNgRxCoreItemPayloadAction()
  • generateNgRxCoreListPayloadAction()
const {
  Type,
  ...ActionGenerators,
} = generateNgRxCoreActions<MyItem, number>(MyItemsConfig.pluralName);

const MyItemsType = {
  // Spread the original types
  ...Type,

  // Add additional action types
  Custom: generateNgRxCoreActionName(ItemsDomain.storeName, 'Custom'),
};

export const MyItemsActions = {
  Type: MyItemsType,

  // Spread the original actions
  ...ActionGenerators,

  // Add additional action generators
  Custom: generateNgRxCoreEmptyPayloadAction(ItemsType.Custom),

4. Reducer Example

Use the standard approach

import { generateNgRxCoreReducer} from '@zionapps/ngrx-core';
import { MyItem, MyItemsConfig, MyItemsState } from './my-items.domain';
import { myItemsAdapter } from './my-items.utils';

export const myItemsReducer: MyItemsState = generateNgRxCoreReducer<MyItem>(myItemsAdapter, MyItemsConfig);

Extend with more reducers

TBD

5. Selector Example

Use the standard approach

import { generateNgRxCoreSelectors, INgRxCoreSelectors } from '@zionapps/ngrx-core';
import { MyItem, MyItemsConfig } from './my-items.domain';
import { myItemsAdapter } from './my-items.utils';

export const MyItemSelectors: INgRxCoreSelectors<MyItem> = generateNgRxCoreSelectors<MyItem>(myItemsAdapter, MyItemsConfig.pluralName);

Extend with more selectors

TBD

6. Effects Example

import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { Store } from '@ngrx/store';
import { NgRxCoreAction, NgRxCoreEffects } from '@zionapps/ngrx-core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { MyItemsActions } from './my-items.actions';
import { MyItem, MyItemsConfig } from './my-items.domain';
import { MyItemsService } from './my-items.service';

@Injectable()
export class MyItemsEffects extends NgRxCoreEffects<MyItem, number> {
  constructor(protected actions$: Actions,
              protected service: MyItemsService,
              protected store: Store<any>) {
    super(actions$, MyItemsActions, MyItemsConfig, service, store);
  }

  @Effect()
  backgroundFetchAllRemote$: Observable<NgRxCoreAction> = this.getBackgroundFetchAllRemote$();

  @Effect()
  backgroundFetchOneRemote$: Observable<NgRxCoreAction> = this.getBackgroundFetchOneRemote$();

  @Effect()
  createManyRemote$: Observable<NgRxCoreAction> = this.getCreateManyRemote$();

  @Effect()
  createOneRemote$: Observable<NgRxCoreAction> = this.getCreateOneRemote$();

  @Effect()
  deleteOneRemote$: Observable<NgRxCoreAction> = this.getDeleteOneRemote$();

  @Effect()
  fetchAllRemote$: Observable<NgRxCoreAction> = this.getFetchAllRemote$();

  @Effect()
  fetchOneRemote$: Observable<NgRxCoreAction>  = this.getFetchOneRemote$();

  @Effect()
  loadAllFromDevice$: Observable<NgRxCoreAction> = this.getLoadAllFromDevice$();

  @Effect()
  loadStateFromDevice$: Observable<NgRxCoreAction> = this.getLoadStateFromDevice$();

  @Effect()
  saveAllToDevice$: Observable<NgRxCoreAction>  = this.getSaveAllToDevice$();

  @Effect()
  saveStateToDevice$: Observable<NgRxCoreAction> = this.getSaveStateToDevice$();

  @Effect()
  updateManyRemote$: Observable<NgRxCoreAction> = this.getUpdateManyRemote$();

  @Effect()
  updateOne$: Observable<NgRxCoreAction> = this.getUpdateOne$();
  
  // Custom Effect
  @Effect()
  custom$: Observable<NgRxCoreAction> = this.actions$.pipe(
    ofType(MyItemsActions.Type.LoadAllFromDeviceRequest),
    map((action: NgRxCoreAction) => {
      const { payload, source, token } = action;
      return MyItemsActions.SaveAllToDeviceRequest(source, payload, token);
    },
  );
}

Application Insights Reporting

1. Install Application Insights

npm install applicationinsights-js
npm install @types/applicationinsights-js

2. Use JavaScript in Project

Import Application Insights JS via the .angular-cli.json file

{
  ...
  "apps": [
    {
      ...
      "scripts": [
        "../node_modules/applicationinsights-js/dist/ai.0.js"
      ]
      ...
    }
  ...
  ]
}

3. Setup Environment

Ensure that the following environment variables are set for environment.prod.js. Tracking is off by default during development.

export const environment = {
  ...
  appInsightsInstrumentationKey: '{FILL_IN}',
  appVersion: '{FILL_IN}',
  produciton: true,
  ...
};

4. Create Module

import {NgModule} from '@angular/core';
import {EffectsModule} from '@ngrx/effects';
import {
  APPLICATION_INSIGHTS_CONFIG,
  NgRxCoreApplicationInsightsEffects,
  generateApplicationInsightsConfig,
} from '@zionapps/ngrx-core';
import {environment} from '../../../environments/environment';

@NgModule({
  declarations: [],
  imports: [
    EffectsModule.forFeature([NgRxCoreApplicationInsightsEffects]),
  ],
  providers: [
    NgRxCoreApplicationInsightsEffects,
    { provide: APPLICATION_INSIGHTS_CONFIG, useValue: generateApplicationInsightsConfig(environment)}
  ]
})
export class ApplicationInsightsModule {}
  1. Use Application Insights Module
@NgModule({
  ...
  imports: [
    ...
    ApplicationInsightsModule,
    ...
  ],
  ...
})
export class YourModule {}
1.4.4

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.3

3 years ago

1.2.0

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.1.0

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.2

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

0.36.3

4 years ago

0.36.2

4 years ago

0.36.1

4 years ago

1.0.0

4 years ago

0.36.0

4 years ago

0.35.0

4 years ago

0.33.13

4 years ago

0.33.12

4 years ago

0.33.10

4 years ago

0.33.9

4 years ago

0.33.8

4 years ago

0.33.7

4 years ago

0.33.6

5 years ago

0.33.5

5 years ago

0.33.3

5 years ago

0.33.2

5 years ago

0.33.1

5 years ago

0.33.0

5 years ago

0.32.2

5 years ago

0.32.1

5 years ago

0.32.0

5 years ago

0.31.3

5 years ago

0.31.2

5 years ago

0.31.1

5 years ago

0.31.0

5 years ago

0.30.0

5 years ago

0.28.3

5 years ago

0.28.2

5 years ago

0.29.1

5 years ago

0.29.0

5 years ago

0.28.1

5 years ago

0.28.0

5 years ago

0.27.0

5 years ago

0.26.0

5 years ago

0.25.0

5 years ago

0.24.1

5 years ago

0.24.0

5 years ago

0.23.0

5 years ago

0.22.1

5 years ago

0.22.0

5 years ago

0.21.8

5 years ago

0.21.7

5 years ago

0.21.6

5 years ago

0.10.21

5 years ago

0.10.20

5 years ago

0.21.5

5 years ago

0.21.4

5 years ago

0.21.3

5 years ago

0.21.2

5 years ago

0.21.1

5 years ago

0.21.0

5 years ago

0.20.21

5 years ago

0.20.20

5 years ago

0.20.19

5 years ago

0.20.18

5 years ago

0.20.17

5 years ago

0.20.16

5 years ago

0.20.15

5 years ago

0.20.14

5 years ago

0.20.13

5 years ago

0.20.12

5 years ago

0.20.11

5 years ago

0.20.10

5 years ago

0.20.9

5 years ago

0.20.8

5 years ago

0.20.7

5 years ago

0.20.6

5 years ago

0.20.5

5 years ago

0.20.4

5 years ago

0.11.6

5 years ago

0.10.19

5 years ago

0.10.18

5 years ago

0.20.3

5 years ago

0.20.2

5 years ago

0.20.1

5 years ago

0.20.0

5 years ago

0.11.5

5 years ago

0.11.4

5 years ago

0.11.3

5 years ago

0.11.2

5 years ago

0.11.1

5 years ago

0.10.17

5 years ago

0.10.16

5 years ago

0.10.15

5 years ago

0.10.14

5 years ago

0.11.0

5 years ago

0.10.13

5 years ago

0.10.12

5 years ago

0.10.11

5 years ago

0.10.10

5 years ago

0.10.9

5 years ago

0.10.8

5 years ago

0.10.7

5 years ago

0.10.6

5 years ago

0.10.5

5 years ago

0.10.4

5 years ago

0.10.3

5 years ago

0.10.2

5 years ago

0.10.1

5 years ago

0.10.0

5 years ago

0.9.55

5 years ago

0.9.54

5 years ago

0.9.53

5 years ago

0.9.52

5 years ago

0.9.51

5 years ago

0.9.50

5 years ago

0.9.43

5 years ago

0.9.42

5 years ago

0.9.41

5 years ago

0.9.40

5 years ago

0.9.39

5 years ago

0.9.38

5 years ago

0.9.37

5 years ago

0.9.36

5 years ago

0.9.35

5 years ago

0.9.34

5 years ago

0.9.33

5 years ago

0.9.32

5 years ago

0.9.31

5 years ago

0.9.30

5 years ago

0.9.29

5 years ago

0.9.28

5 years ago

0.9.27

5 years ago

0.9.26

5 years ago

0.9.25

5 years ago

0.9.24

5 years ago

0.9.23

5 years ago

0.9.22

5 years ago

0.9.21

5 years ago

0.9.20

5 years ago

0.9.19

5 years ago

0.9.18

5 years ago

0.9.17

5 years ago

0.9.15

5 years ago

0.9.14

5 years ago

0.9.13

5 years ago

0.9.11

5 years ago

0.9.10

5 years ago

0.9.9

5 years ago

0.9.7

5 years ago

0.9.6

5 years ago

0.9.5

5 years ago

0.9.4

5 years ago

0.9.3

5 years ago

0.9.2

5 years ago

0.9.1

5 years ago

0.8.6

5 years ago

0.8.5

5 years ago

0.8.4

5 years ago

0.8.3

5 years ago

0.8.2

5 years ago

0.8.1

5 years ago

0.8.0

5 years ago

0.7.2

5 years ago

0.7.1

5 years ago

0.7.0

5 years ago

0.6.0

5 years ago

0.3.9

5 years ago

0.3.8

5 years ago

0.3.7

5 years ago

0.3.6

5 years ago

0.3.5

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.12

5 years ago

0.2.11

5 years ago

0.2.10

5 years ago

0.2.9

5 years ago

0.2.8

5 years ago

0.2.7

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.12

5 years ago

0.1.11

5 years ago

0.1.10

5 years ago

0.1.9

5 years ago

0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.0

5 years ago