Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ngx-repository

EndyKaufman55MIT0.7.0TypeScript support: included

Custom repository service for Angular9+, for easy work with the REST backend, with switch on fly from REST backend to the MOCK backend with save and use all CRUD operations

repository-service, class-transformer, class-validator, dynamic, mock, rest, fake-http-client, angular, ngx, angular9

readme

Greenkeeper badge Build Status npm version

Custom repository service for Angular9+, for easy work with the REST backend, with switch on fly from REST backend to the MOCK backend with save and use all CRUD operations

How it work

Installation

npm i --save ngx-repository

Demo - Demo application with ngx-repository.

Stackblitz - Simply sample of usage on https://stackblitz.com

Usage

app.module.ts

import { NgxRepositoryModule } from 'ngx-repository';
import { UsersGridComponent } from './users-grid.component';

@NgModule({
  imports: [
    ...
    NgxRepositoryModule,
    ...
  ],
  declarations: [
    ...
    UsersGridComponent,
    ...
  ],
  ...
})
export class AppModule {}

user-model.ts

import { IModel } from 'ngx-repository';
import { IsNotEmpty, IsOptional } from 'class-validator';
import { plainToClassFromExist } from 'class-transformer';

export class UserModel implements IModel {
    @IsOptional()
    id: number;
    @IsNotEmpty()
    username: string;
    password: string;
    constructor(data?: any) {
        plainToClassFromExist(this, data);
    }
}

users-grid.component.ts

import { Component, OnInit } from '@angular/core';
import { DynamicRepository, Repository } from 'ngx-repository';
import { UserModel } from './user-model';
import { Observable } from 'rxjs';

@Component({
  selector: 'users-grid',
  template: `
<button (click)="create()"> Create </button>
<ul>
  <li *ngFor="let item of items$ | async">
    <span *ngIf="editedUser?.id!==item?.id">
      {{item.username}}
      <button (click)="startEdit(item)"> Edit </button>
      <button (click)="delete(item) "> Delete </button>
    </span>
    <span *ngIf="editedUser?.id===item?.id">
      <input [(ngModel)]="editedUser.username" />
      <button (click)="save(editedUser)"> Save </button>
      <button (click)="cancel()"> Cancel </button>
    </span>
  </li>
</ul>
  `
})
export class UsersGridComponent implements OnInit {
  public editedUser: UserModel;
  public repository: Repository<UserModel>;
  public items$: Observable<UserModel[]>
  private mockedItems = [
    {
        'username': 'user1',
        'password': 'password1',
        'id': 1,
    }, {
        'username': 'user2',
        'password': 'password2',
        'id': 2,
    }, {
        'username': 'user3',
        'password': 'password3',
        'id': 3,
    }, {
        'username': 'user4',
        'password': 'password4',
        'id': 4,
    }
  ];
  constructor(
    private dynamicRepository: DynamicRepository
  ) {
    this.repository = this.dynamicRepository.fork<UserModel>(UserModel);
  }
  ngOnInit() {
    this.repository.useMock({
        items: this.mockedItems,
        paginationMeta: {
            perPage: 2
        }
    });
    /* For real backend
    this.repository.useRest({
      apiUrl: environment.apiUrl,
      paginationMeta: {
        perPage: 2
      }
    });*/
    this.items$ = this.repository.items$;
  }
  startEdit(user: UserModel) {
    this.editedUser = this.repository.clone(user);
  }
  cancel() {
    this.editedUser = undefined;
  }
  save(user: UserModel) {
    this.repository.save(user).subscribe();
    this.editedUser = undefined;
  }
  create() {
    this.repository.create(new UserModel({
      username: 'new user'
    })).subscribe();
  }
  delete(user: UserModel) {
    this.repository.delete(user.id).subscribe();
  }
}

app.component.ts

...
<users-grid></users-grid>
...

License

MIT

changelog

0.7.0 (2020-03-20)

Features

  • update deps, update to Angular 9 (4d78fb1)

0.6.3 (2019-01-12)

Bug Fixes

  • Fix error detect url for work with mockdata over fakehttpclient (e64a7b2)

0.6.2 (2018-12-30)

Bug Fixes

  • Remove deleteNestedFactoryModel from globalEventResolver (72d95b9)

0.6.1 (2018-12-10)

Bug Fixes

  • Add interface and implements from it (#87) (4c72cde)
  • Update dependencies and format sources (b7a3e88)

0.6.0 (2018-11-17)

Features

  • deps: Update dependencies (91f19e1)

0.5.0 (2018-10-21)

Features

  • inifinity-pagination: Add support infinity page switching (2a21654)

0.4.2 (2018-09-16)

Bug Fixes

  • deps: Update dependencies (c0ba0d4)

0.4.1 (2018-06-06)

Bug Fixes

  • Add whitelistedNonPeerDependencies in ng-package.json (00e7b05)

0.4.0 (2018-06-04)

Features

  • Update to Angular 6 and Rx 6 (8b8d2dd)

0.3.6 (2018-04-25)

Bug Fixes

  • rest-provider: Change to work with clonned filter, for remove link to original filter variable (c004323)

0.3.5 (2018-04-14)

Bug Fixes

  • fake-http-client: Remove no used @Injectable() decorator (62cb552)
  • module: Remove import HttpClientModule from module, for correct work on ssr (49f36dd)

0.3.4 (2018-04-10)

Bug Fixes

  • rest-provider: Remove undefined or empty filters (dabe991)

0.3.3 (2018-04-08)

Bug Fixes

  • rest-provider: Revert usage useDefault, disable only in "action" method (921ee40)

0.3.2 (2018-04-08)

Bug Fixes

  • rest-provider: Fix up reset filters on loadAll (d2263b8)
  • rest-provider: Support filter varibales curPage, perPage, searchText (ac900c9)

0.3.2 (2018-04-07)

Bug Fixes

  • rest-provider: Fix up reset filters on loadAll (d2263b8)
  • rest-provider: Support filter varibales curPage, perPage, searchText (ac900c9)

0.3.1 (2018-03-21)

Bug Fixes

  • Add IFakeHttpClientOptions (c3d4d01)

0.3.0 (2018-03-21)

Features

  • rest-options: Add IRestOptions for tune work with rest (41ca3db)
  • rest-provider-action-options: Remove requestLoadAllPaginationQuery, requestLoadAllSearchQuery, requestLoadAllSearchQuery and add requestQuery and responsePaginationMeta (393c320)

0.2.0 (2018-03-19)

Features

  • options: Add globalEventResolver and globalEventIsActiveResolver for manage start and resolve global events (9c2515a)

0.1.4 (2018-03-19)

Bug Fixes

  • Correct work with pagination over FakeHttpClient (99a6112)

0.1.3 (2018-03-18)

Bug Fixes

  • rest.provider: Add merge options with prev options (0b252b4)

0.1.2 (2018-03-18)

Bug Fixes

  • Add import for ErrorObservable (ea4c678)
  • rest-provider-action-options: Add paginationMeta attribute to requestLoadAllPaginationQuery (d3f2e7a)

0.1.1 (2018-03-14)

Bug Fixes

  • errors: Change throw error to Observable.throw(error) (74ddb3b)

0.1.0 (2018-03-14)

Bug Fixes

  • provider-action-options: Add useFakeHttpClient options, and replace take(1) to first() (2aa2f13)
  • rest-provider-action-options: Change type of requestOptions to function (d51cf63)

Features

  • refactor: Remove all Promise, change its to Observable (ff05cec)

0.0.4 (2018-03-01)

Bug Fixes

  • deps: Update dependencies (9ec567d)

0.0.3 (2018-02-28)

Bug Fixes

  • deps: Add missing dependencies to module package.json (8771725)
  • refactor: Change wrong words usage "request" to "response" (7be893e)