0.1.0 • Published 5 years ago

dynamic-ngrx v0.1.0

Weekly downloads
78
License
-
Repository
-
Last release
5 years ago

About This Project

This library is for anyone who wants to use NGRX in their project, but doesn't want to go through the insane headache of managing all the actions, reducers, entities, selectors, effects and stores.

The goal of this project is to add all that functionality to your project with less than 5 lines of code (including config).

Table of Contents

Demo

Check it out in action on stackblitz

Installation

To install, run npm i dynamic-ngrx --save

The project also has the following dependencies @ngrx/effects @ngrx/entity @ngrx/router-store @ngrx/store @ngrx-devtools ngrx-store-logger

Quick Start

In your Angular AppModule

You'll need to add the dependency, create the config file, and pass it in as a parameter to .forRoot on the module

import { DynamicNgrxModule, DynamicStoreConfig } from 'dynamic-ngrx';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

const config: DynamicStoreConfig = {
  entities: [
    { entity: 'Todo', },
  ],
};
  • Import the configuration to the .forRoot method on the DynamicNgrxModule
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    DynamicNgrxModule.forRoot(config),
    BrowserAnimationsModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Usage in templates and services

In your templates and services, the main thing you'll need to do is import the DynamicFacadeService. You pass in the interface you're working with, as well as the entity name, and it will return a facade for that particular entity. You'll access all your required functions for the entity through the facade.

The example code below demonstrates how to access the entity observable, as well as dispatch two simple actions.

export class AppComponent {
  @ViewChild('todoInput') todoInput: ElementRef;

  todos$: Observable<Todo[]>;

  private todoFacade: DynamicFacade<Todo>;
  constructor(
    private dynamicFacadeService: DynamicFacadeService,
    private store: Store<any>,
  ) {
    this.todoFacade = this.dynamicFacadeService.getFacade<Todo>('Todo');
    this.todos$ = this.todoFacade.entities$;
  }

  addTodo(event): void {
    if (event.keyCode !== 13) { return; }
    const todo: Todo = {
      id: uuid(),
      text: this.todoInput.nativeElement.value,
    };
    this.store.dispatch(this.todoFacade.actions.addOne(todo));
    this.todoInput.nativeElement.value = '';
  }

  removeTodo(todo: Todo): void {
    this.store.dispatch(this.todoFacade.actions.removeOne(todo.id));
  }
}

Reference

Introduction

This library is well suited for anyone that wants to use NGRX to manage large amount of entities, and wants standard CRUD type functionality with those entities.

The main benefits of this library, our of the box, are:

  • Immediate connectivity of Redux Dev Tools
  • Detailed Action Logging
  • Offline Sync
  • Rapidly add new entities
  • Full CRUD functionality for all entities
  • All entity adaptors accessible
  • Easily connect with external data providers

DynamicNgrxModule

The DynamicNgrxModule accepts a config object <DynamicStoreConfig> upon import.

DynamicStoreConfig

PropertyTypeDescription
entitiesEntityConfig[]Array of entity configs
syncEntitiesstring[]If you have additional entities managed by NGRX that are not being managed by Dynamic NGRX, add them here and it will ensure the data gets persisted (i.e. auth or personal account settings)
enableOfflineSyncbooleanDefaults to false. If you set this to true, all the Dynamic NGRX entities will be synchronized to localstorage
enableLoggingbooleanDefaults to true

EntityConfig

PropertyTypeDescription
entitystringThe name of the entity as you want it to exist in your state. Capitalization is important here
entityKeystringIf the key in your entity is something other than id
dataServiceAngular ServiceAn Angular Injectable() service that extends the <DefaultDataService>. The data service you add will access your remote server settings. If this is left blank, then Dynamic NGRX will bypass any remote connections, and store the data immediately to the state
persistbooleanDefaults to true. Set to false if you don't want this entity to persists to localstorage

DefaultDataService

The default data service handles the effects for every entity, unless otherwise specified. As described in EntityConfig, you can extend the default data service to integrate with an external API to handle the actions.

more readme to come ...

Known Issues

The main issue I'm aware of at this time is the library throws an error when you try to build it using --prod or --aot. I have spent many hours trying to troubleshoot and resolve the error, so I would love some help figuring out why this is failing. In the mean time, if you build your project with any other settings, it should work just fine.

Roadmap

  • Convert offline mode to work with IndexedDB instead of Localstorage
  • Depending on popularity, testing
0.1.0

5 years ago

0.0.29

5 years ago

0.0.26

5 years ago

0.0.25

5 years ago

0.0.24

5 years ago

0.0.23

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago