• Stars
    star
    972
  • Rank 45,445 (Top 1.0 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Angular with ngRx and experimental ngrx-data helper

Angular ngrx-data

This repository is now merged with @ngrx into @ngrx/data. You can find the ngrx/data docs here and the github repository with ngrx/data in it here

Please read the note above

prettier

Zero Ngrx Boilerplate

You may never write an action, reducer, selector, effect, or HTTP dataservice again.

NgRx helps Angular applications manage shared state in a "reactive" style, following the redux pattern.

But to use it properly requires both a deep understanding of redux/ngrx and a lot of boilerplate code.

Ngrx-data is an ngrx extension that offers a gentle introduction to ngrx/redux without the boilerplate.

Try it! See the Quick Start for instructions on adding NgRx and ngrx-data to your app.

Why use ngrx-data?

Many applications have substantial domain models with 10s or 100s of entity types such as Customer, Order, LineItem, Product, and User.

In plain ngrx, to create, retrieve, update, and delete (CRUD) data for every entity type is an overwhelming task. You're writing actions, action-creators, reducers, effects, dispatchers, and selectors as well as the HTTP GET, PUT, POST, and DELETE methods for each entity type.

In even a small model, this is a ton of repetitive code to create, maintain, and test.

The ngrx-data library is one way to stay on the ngrx path while radically reducing the "boilerplate" necessary to manage entities with ngrx.

It's still NgRx

This is a library for ngrx, not an ngrx alternative.

It's easy to combine standard ngrx with ngrx-data. It's easy to take control when you need it and hand control back to ngrx-data when you're done.

Every entity has its own actions. Every operation takes its unique journey through the store, reducers, effects, and selectors. You just let ngrx-data create these for you.

You can add custom store properties, actions, reducers, selectors, and effects. You can override any ngrx-data behavior for an individual entity type or for all entities. You can make your own calls to the server and update the cached entity collections with the results using ngrx-data cache-only actions.

You can see the ngrx machinery at work with the redux developer tools. You can listen to the flow of actions directly. You can intercept and override anything ... but you only have to intervene where you want to add custom logic.

Learn about it

For a hands-on experience, try the QuickStart in the tutorial git repo, ngrx-data-lab, which guides you on the few, simple steps necessary to migrate from a typical service-based Angular app, to an app that manages state with ngrx-data.

This ngrx-data repository has the main documentation and its own sample app.

The sample app in the src/client/app/ folder presents an editor for viewing and changing Heroes and Villains.

The following reduced extract from that demo illustrates the essential mechanics of configuring and using ngrx-data.

You begin with a description of the entity model in a few lines of metadata.

// Metadata for the entity model
export const entityMetadata: EntityMetadataMap = {
  Hero: {},
  Villain: {}
};

// Help ngrx-data pluralize entity type names
// because the plural of "Hero" is not "Heros"
export const pluralNames = {
  Hero: 'Heroes' // the plural of Hero
};

You register the metadata and plurals with the ngrx-data module.

@NgModule({
  imports: [
    NgrxDataModule.forRoot({
      entityMetadata: entityMetadata,
      pluralNames: pluralNames
    })
  ]
})
export class EntityStoreModule {}

Your component accesses each entity data through an EntityCollectionService which you can acquire from the ngrx_data EntityServices.

In the following example, the HeroesComponent injects EntityServices and asks it for an EntityCollectionService registered under the Hero entity name.

The component uses that service to read and save Hero entity data in a reactive, immutable style, without reference to any of the ngrx artifacts.

import { EntityCollectionService, EntityServices } from 'ngrx-data';
import { Hero } from '../../core';

@Component({
  selector: 'app-heroes',
  templateUrl: './heroes.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class HeroesComponent implements OnInit {
  heroes$: Observable<Hero[]>;
  heroesService: EntityCollectionService<Hero>;

  constructor(entityServices: EntityServices) {
    this.heroesService = entityServices.getEntityCollectionService('Hero');
    this.heroes$ = this.heroesService.entities$;
  }

  ngOnInit() {
    this.getHeroes();
  }

  getHeroes() {
    this.heroesService.getAll();
  }

  addHero(hero: Hero) {
    this.heroesService.add(hero);
  }

  deleteHero(hero: Hero) {
    this.heroesService.delete(hero.id);
  }

  updateHero(hero: Hero) {
    this.heroesService.update(hero);
  }
}

As you explore ngrx-data and its documentation, you'll learn many extension points and customizations that tailor the developer experience to your application needs.

QuickStart

Try the Quick Start to experience NgRx and ngrx-data in your app.

Explore this repository

This repository contains the ngrx-data source code and a demonstration application (the "demo app") that exercises many of the library features.

The key folders in this repository are:

  • docs --> the docs for the library and the demo
  • lib ---> the ngrx-data library source code that we publish to npm
  • src/app ---> the demo app source
  • server ---> a node server for remote data access

Learn more in the docs

Install and run

The demo app is based on the Angular CLI. You may want to install the CLI globally if you have not already done so.

npm install -g @angular/cli

Then follow these steps:

  1. Clone this repository

    git clone https://github.com/johnpapa/angular-ngrx-data.git
    cd angular-ngrx-data
  2. Install the npm packages

    npm install
  3. Build the ngrx-data library

    npm run build-setup
  4. Serve the CLI-based demo app

    ng serve -o

Refer to the troubleshooting section if you run into installation issues.

Run the library tests

The ngrx-data library ships with unit and E2E (end-to-end) tests to validate functionality and guard against regressions.

These tests also demonstrate features of the library that are not covered in the demo app. They're worth reading to discover more advanced techniques.

Run this CLI command to execute the unit tests for the library.

ng test

Run the sample app E2E (end-to-end) tests.

npm run e2e

We welcome PRs that add to the tests as well as those that fix bugs and documentation.

Be sure to run these tests before submitting a PR for review.

Monitor the app with Redux DevTools

The demo app is configured for monitoring with the Redux DevTools.

Follow these instructions to install them in your browser and learn how to use them.

Debug the library in browser dev tools

When running tests, open the browser dev tools, go to the "Sources" tab, and look for the library files by name.

In chrome, type [Command-P] and letters of the file name.

When running the app (e.g., with ng serve), the browser dev tools give you TWO choices for a given TypeScript source file, both claiming to be from .ts.

The one you want for library and app files ends in /lib/src/file-name.ts and /src/client/app/path/file-name.ts respectively.

Hope to solve the two file problem.

Build the app against the source

The demo app is setup to build and run against the ngPackagr artifacts in dist/ngrx-data, the same artifacts delivered in the npm package.

Re-build the library npm run build-lib or npm run build-setup or npm run build-all to update these artifacts.

This approach, while safe, can be inconvenient when you're evolving the library code because "Go to definition" takes you to the d.ts files in dist/ngrx-data rather than the source files in lib/src.

If you want to "Go to definition" to take you to the source files, make the following *temporary changes to the TypeScript configuration.

  1. Replace the paths target in the root tsconfig.json so that the IDE (e.g., VS Code) looks for ngrx-data in src/lib.

      "paths": {
        "ngrx-data": ["lib/src"]
      },
  2. Replace that same setting in the config at src/tsconfig.json.

  3. Replace that same setting in src/client/tsconfig.app.json. Now ng build references src/lib when it builds the demo app.

Remember to restore the tsconfig settings when you're done. Do not commit those changes!

Use a real database

The demo app queries and saves mock entity data to an in-memory database with the help of the Angular In-memory Web API.

The "Remote Data" toggle switch in the header switches to the remote database.

The app fails when you switch to the remote database.

Notice how the app detects errors and pops up a toast message with the failed ngrx Action.type.

The error details are in the browser's console log.

You must first set up a database and launch a web api server that talks to it.

The src/server folder has code for a local node web api server, configured for the demo app.

TODO: Explain how to build and run the server. Explain how to build and serve the mongo db

Bad/surplus npm scripts

We know there are a great many npm script commands in package.json, many (most?) of which don't work.

They're on our list for future cleanup. Please don't create issues for them (although PRs that fix them are welcome).

Troubleshooting

Installation

  1. If you are on Windows and run into this error during npm install: "snyk couldn't patch the specified vulnerabilities because gnu's patch is not available", refer to this issue for the fix. In short, your Git installation is not correct or C:\Program Files\Git\usr\bin (typically) is not added to your system environment variable %PATH%.

Problems or Suggestions

Open an issue here

More Repositories

1

angular-styleguide

Angular Style Guide: A starting point for Angular development teams to provide consistency through good practices.
23,968
star
2

lite-server

Lightweight node server
JavaScript
2,298
star
3

ng-demos

variety of angular demos
JavaScript
1,705
star
4

vscode-peacock

Subtly change the color of your Visual Studio Code workspace. Ideal when you have multiple VS Code instances, use VS Live Share, or use VS Code's Remote features, and you want to quickly identify your editor.
TypeScript
1,014
star
5

generator-hottowel

Yo generator that creates an Angular app via HotTowel
JavaScript
837
star
6

angular-tour-of-heroes

Angular - Tour of Heroes - The Next Step after Getting Started
TypeScript
822
star
7

vscode-angular-snippets

Angular Snippets for VS Code
TypeScript
567
star
8

gulp-patterns

Playground for Gulp Recipes
JavaScript
502
star
9

HotTowel-Angular

HotTowel with Angular (for NuGet)
JavaScript
238
star
10

ngrx-data-lab

Sample app that can be expanded to use ngrx-data
TypeScript
216
star
11

pwa-angular

PWA Example
TypeScript
204
star
12

vue-getting-started

This project is seen in demos including the Pluralsight course "Vue: Getting Started" to help represent a fundamental app written with Vue. The heroes and villains theme is used throughout the app.
Vue
191
star
13

heroes-angular

Tour of Heroes app written with Angular
TypeScript
161
star
14

pluralsight-gulp

Starter Code for Pluralsight Course "JavaScript Build Automation with Gulp.js"
JavaScript
161
star
15

vscode-winteriscoming

Dark theme with fun and bright foreground colors
CSS
157
star
16

angular-event-view-cli

Angular Demo with a Little bit of a lot of features
TypeScript
154
star
17

vue-typescript

Vue.js with TypeScript (OLD - in process of updating)
Vue
145
star
18

hottowel-angular-typescript

As seen at //Build 2015 presented by Erich Gamma, Chris Dias and John Papa.
JavaScript
145
star
19

heroes-vue

Tour of Heroes app written with Vue
Vue
144
star
20

HotTowel

John Papa's ASP.NET MVC SPA Template (Durandal)
CSS
144
star
21

heroes-react

Tour of Heroes app written with React
JavaScript
133
star
22

shopathome

Choose from Angular, React, Svelte, and Vue applications with an Azure Functions API, that deploys to Azure Static Web Apps
TypeScript
133
star
23

PluralsightSpaJumpStartFinal

Source code for the SPA JumpStart Pluralsight course at http://jpapa.me/spajsps
JavaScript
133
star
24

vscode-angular-essentials

Dockerfile
132
star
25

hello-worlds

Hello world apps for angular, react, svelte, and vue
TypeScript
128
star
26

heroes-angular-serverless

TypeScript Node/Express 👉TypeScript Serverless ➕Angular
TypeScript
125
star
27

angular2-force

ngConf 2016 live coding demo
JavaScript
115
star
28

angular-preload-and-interceptors

TypeScript
104
star
29

vscode-cloak

Cloak allows you to hide/show environment keys, to avoid accidentally sharing them with everyone who sees your screen.
TypeScript
102
star
30

angular-first-look-examples

angular first look for pluralsight
HTML
90
star
31

angular-first-look-hosted

Hosted Code from Pluralsight Course "Angular First Look"
HTML
86
star
32

angular-cosmosdb

Cosmos DB, Express.js, Angular, and Node.js app
TypeScript
81
star
33

CodeCamper

JavaScript
71
star
34

pluralsight-vscode-samples

VS Code samples for Pluralsight course on Code
JavaScript
70
star
35

docker-angular-cli

Dockerfile and image for node plus angular CLI
Dockerfile
69
star
36

awesome-angular-workshop

Angular: The Awesome Parts - Workshop
TypeScript
68
star
37

angular.breeze.storagewip

Save Work in Progress to Local Storage for Angular and Breeze apps
JavaScript
66
star
38

Pluralsight-CC-Angular-Breeze-Extra

Supporting files for the Pluralsight "SPA with Angular and Breeze" course by John Papa.
JavaScript
64
star
39

one-with-angular

TypeScript
61
star
40

node-ts

Simple Node app Written with TypeScript
TypeScript
59
star
41

node-hello

Hello World for Node.js
JavaScript
59
star
42

http-interceptors

The Web apps in this monorepo make HTTP requests and require uniform consistency in how they are executed and handled. This monorepo demonstrates the same app written with Angular and with Svelte. Each app uses HTTP interceptors. The Angular app uses HttpClient and its interceptors while the Svelte app uses Axios and its interceptors.
TypeScript
58
star
43

angular-what-if

TypeScript
57
star
44

express-to-functions

TypeScript Node/Express 👉TypeScript Serverless ➕ Angular
TypeScript
54
star
45

vikings

TypeScript
53
star
46

ngrx-demo

NgRx demo
TypeScript
51
star
47

vue-guide

Super Simple Vue Samples
HTML
51
star
48

typescript-async

Creating Asynchronous Code with TypeScript
TypeScript
48
star
49

ng-patterns-testing

JavaScript
48
star
50

vscode-angular1-snippets

vscode-angular1-snippets
41
star
51

vscode-pwa

VS Code Extension for PWA Tools
TypeScript
38
star
52

angular-lazy-load-demo

Lazy loading Angular components
TypeScript
32
star
53

github-templates

31
star
54

angular-2-first-look-launcher

deprecated
JavaScript
30
star
55

heroes-node-api

node api for the heroes apps
JavaScript
30
star
56

heroes-svelte

Tour of Heroes app written with Svelte
Svelte
30
star
57

kis-requirejs-demo

Keep It Simple RequireJS Demo. Shows simple demo of require.js before and after
JavaScript
30
star
58

heroes-vue-node-api

As seen in Vue Conf 2019
Vue
28
star
59

toastr-bower

toastr's bower repo
CSS
24
star
60

star-wars-api

Star Wars API
JavaScript
23
star
61

vue-workshop

Vue Fundamentals Workshops
Vue
23
star
62

vue-simple

This project was created to help represent a fundamental app written with Vue. The heroes and villains theme is used throughout the app.
Vue
23
star
63

typescript-fundamentals

HTML
21
star
64

vscode-read-time

TypeScript
19
star
65

vue-intro

Vue.js app using Vue's CLI
Vue
18
star
66

angular-rxjs-shared-examples

rxjs examples
TypeScript
17
star
67

serverless-thank-you

Say thank you to everyone who takes the time to create and discus or a pull request in your Github repository, using Azure Functions
TypeScript
17
star
68

swa-workshop

TypeScript
16
star
69

cloud-coding-with-codespaces

Live demo using Angular, github.dev, codespaces, copilot, azure static web apps, and devcontainers
TypeScript
13
star
70

vscode-azure-functions-tools

Azure Functions Tools for VS Code - DEPRECATED
12
star
71

vue-cli-preset-all-javascript

Vue CLI Preset for All JavaScript Prompts
12
star
72

vscode-presenter-pro

11
star
73

vue-ts

simple repro of master details with vue 3, composition api, and typescript
Vue
10
star
74

ios-play

iOS Playground
Swift
8
star
75

security-strategy-essentials

JavaScript
8
star
76

telekinesis

"Think" code
JavaScript
7
star
77

house-bot

Home automation with AI, LUIS, Serverless
JavaScript
7
star
78

johnpapa

7
star
79

first-serverless-api

Create your first serverless API endpoints with Azure Functions
JavaScript
7
star
80

innersource

6
star
81

Angular-NuGet

NuGet Repo for Angular Packages
JavaScript
6
star
82

angular-expiring-http-cache

TypeScript
5
star
83

ng-ai-hackathon

TypeScript
5
star
84

starwars-ios

Swift
5
star
85

one-with-angular-api

JavaScript
5
star
86

shopping-for-codespaces

CSS
4
star
87

svelte-intro

JavaScript
3
star
88

glimpse.toastr

JavaScript
3
star
89

typescript-hello-world

Simple hello world project for running TypeScript with Node.js
TypeScript
3
star
90

react-book-repo

3
star
91

pluralsight-ng-testing

pluralsight-ng-testing
CSS
2
star
92

skills-copilot-codespaces-vscode

My clone repository
1
star
93

dotfiles

1
star
94

aggregator-app

serverless function with api aggregator with azure
JavaScript
1
star
95

.github

1
star
96

vscode-import-bug

referencing https://github.com/microsoft/TypeScript/issues/35591
TypeScript
1
star