npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ea-controls/ngrx-repository

v18.0.10

Published

Simplify ngrx boilerplate for managing arrays of objects.

Downloads

1

Readme

Repository

Simplify ngrx boilerplate for managing arrays of objects.

App often involves tables or collections of data. When using ngrx, developers typically create actions (add, remove, update) and reducers to handle these actions, along with selectors.

ngrx official documentation

Demo

Check out the demo on StackBlitz: Demo

Installation

npm i @ea-controls/ngrx-repository@latest

Configuration

Create an interface or class for your data structure (You can create a new user.adapter.ts and do it there):

export interface UserEntity {
    id: string;
    name: string;
    description: string;
    status: string;
}

Create an entity adapter for your model user.adapter.ts:

import { EntityAdapter } from '@ea-controls/ngrx-repository';

export const userAdapter = new EntityAdapter<UserEntity>("users", {...options});

Register your adapters in the module app.config.ts for standalone component:

import { provideStore, provideState } from '@ngrx/store';

export const appConfig: ApplicationConfig = {
  providers: [
    ...
    provideStore(), // <-- provideStore
    provideState(userAdapter.reducer()) // <-- Add userAdapter reducer
  ],
};

Add reducers and actions (Optional)

You can create your own actions and associations between actions and state changes (Reducers) user.adapter.ts

import { createAction, on } from "@ngrx/store";

//new action
export const removeFirstUser = createAction(`[users] custom action update by name`);

//In userAdapter you can send your associations between actions and state changes
export const userAdapter = new EntityAdapter<UserEntity>("users", {
    ons: [
        on(removeFirstUser, (state) => state.slice(1)),
    ]
});

Add custom selectors

EntityAdapter exposes feature for extending selectors user.adapter.ts

import { createSelector } from "@ngrx/store";

export const userAdapter = new EntityAdapter<UserEntity>("users");

export const getById = (id: string) => 
    createSelector(userAdapter.feature, users => items.find(i => userAdapter.getId(i) === id));

Now you can use it in your components

import { signal } from '@angular/core';
import { JsonPipe } from "@angular/common";
import { provideStore, provideState } from '@ngrx/store';
import { Store } from "@ngrx/store";

@Component({...})
export class App {
    constructor(private store: Store) { 
        this.store.select(getById).subscribe(user => console.log('User filtered by ID', user)); // <-- Using getById Selector
    }
}

Usage

Use ngrx pattern. Inject the store and dispatch actions using the adapter:

import { signal } from '@angular/core';
import { JsonPipe } from "@angular/common";
import { provideStore, provideState } from '@ngrx/store';
import { Store } from "@ngrx/store";

@Component({
  selector: 'app-root',
  standalone: true,
  template: `
    <button (click)="add()">Add</button>
    <button (click)="delete()">delete First</button>

    <div style="border: solid thin blue;">
        {{ selected() | json }}
    </div>

    <div style="border: solid thin red;">
    @for (item of data(); track item) {
    <ul>
        <li> {{ item | json }} </li>
    </ul>
    }
    </div>
  `,
  imports: [JsonPipe]
})
export class App {
  constructor(private store: Store) { }

    static id: number = 0;
    data = signal<UserEntity[]>([]);
    selected = signal<UserEntity | undefined>();

    ngOnInit(): void {
        this.store.select(userAdapter.feature).subscribe(data => this.data.set(data));
        this.store.select(userAdapter.selectById("2")).subscribe(data => this.selected.set(data));
    }

    add() {
        App.id = this.data().length;

        this.store.dispatch(userAdapter.addOne({
            id: `${App.id}`,
            name: `Edwin`,
            description: "test",
            status: "active"
        }));
    }

    delete() {
      let item = this.data()[0];
      this.store.dispatch(userAdapter.removeOne(item));
    }
}

Actions / Selectors

| Action/Selector | Description | Values | |------------------|---------------------------------------------|----------------| | addOne(...) | Add a new item to the collection | Entity object / onSuccess? / onFail? | | removeOne(...) | Remove an item from the collection | Entity object / onSuccess? / onFail? | | patchOne(...) | Update an object in the collection | Entity object / onSuccess? / onFail? | | setAll([...]) | Replace the entire collection with new data | Entity array | | removeById(id) | Remove an item from the collection by ID | string / onSuccess? / onFail? | | feature() | Default selector, returns entire collection | - | | selectById(id) | Selects an item from collection by ID | string |

onSuccess/onFail

Some actions receive extra callback information that allows you to run a statement upon success or failure.

Imagine you need to remove an item, and after it is removed, you want to perform another action. To achieve this, you can follow the approach below:

Note: By default, these callbacks are disabled. However, if you extend this library, you can provide them with new behavior using userAdapter.actions.before......

this.store.dispatch(userAdapter.addOne({...} as UserEntity, 
    (data) => { console.log('success operation', data); }, 
    (error) => { console.error('Error while removing', error) }
));

Customize ID Calculation

You can customize the ID calculation when creating the EntityAdapter. Use the following approach:

import { EntityAdapter } from '@ea-controls/ngrx-repository';

export const userAdapter = new EntityAdapter<UserEntity>("users", { getId: (input) => input.userId });

Note: The default ID calculation uses item.id.

Extensions

You can extend @ea-controls/ngrx-repository creating your own effects that manages STATE, for achieving this change {adapter}.launchBeforeActions = true and now implement new before actions

export const userAdapter = new EntityAdapter<UserEntity>("items");
userAdapter.launchBeforeActions = true;

Now addOne, removeOne and other methods should execute beforeActions, and you should execute action dispatchers manually

@Injectable()
export class AdapterExtensionEffect {

    constructor(private actions$: Actions, private httpClient: HttpClient) { }

    get$ = createEffect(() => {

        return this.actions$.pipe(
            ofType(userAdapter.getAll().type),
            switchMap(action => {
                return this.httpClient.get<any>('http://myUrl.com/users')
                    .pipe(
                        map((response) => userAdapter.setAll(response)),
                        //map((response) => userAdapter.actions.setAll({ data: response })),
                        catchError((error) => of(userAdapter._erroGetAll({ error })))
                    );

            }))

    });

    save$ = createEffect(() => {

        return this.actions$.pipe(
            ofType(userAdapter.actions.beforeAddOne.type),
            switchMap(action => {
                return this.httpClient.post<any>('http://myUrl.com/users', action.data)
                    .pipe(
                        tap(response => {
                            //Using onSuccess/onFail actions
                            action.onSuccess && action.onSuccess(action.data);
                        })
                        map(response => userAdapter.addOne(response)),
                        //map((response) => userAdapter.actions.addOne({ data: response })),
                        catchError((error) => of(userAdapter._erroAddOne({ error })))
                    );

            }))

    });
}

Additional

Explore more packages that extend this approach, allowing integration with different databases or APIs for data management.

  1. @ea-controls/ngrx-repository-webapi
  2. @ea-controls/ngrx-repository-pouchdb