@datx/jsonapi-angular
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

@datx/jsonapi-angular

DatX is an opinionated data store. It features support for references to other models and first-class TypeScript support.

@datx/jsonapi-angular is a datx mixin that adds JSON API support for Angular applications.


Installation

npm install @datx/jsonapi-angular

Setup

Collection

Create a collection and provide it under APP_COLLECTION token:

import { Collection } from '@datx/core';
import { jsonapiAngular } from '@datx/jsonapi-angular';

export class AppCollection extends jsonapiAngular(Collection) {
  public static readonly types = [...];
}
import { APP_COLLECTION } from '@datx/jsonapi-angular';
import { AppCollection } from './collections/app.collection';

@NgModule({
  imports: [
    DatxModule.forRoot({
      baseUrl: 'https://my-api.com/',
    }),
  ],
  providers: [
    {
      provide: APP_COLLECTION,
      useValue: new AppCollection(),
    },
  ],
})
export class AppModule {}

Configure DatX

Provide DATX_CONFIG with your own values for the config:

import { APP_COLLECTION, DATX_CONFIG, setupDatx } from '@datx/jsonapi-angular';
import { AppCollection } from '.collections/app.collection';

@NgModule({
  provides: [
    {
      provide: APP_COLLECTION,
      useValue: new AppCollection(),
    },
    {
      provide: DATX_CONFIG,
      useFactory: (httpClient: HttpClient) => {
        return setupDatx(httpClient, {
          baseUrl: '/api/v1/',
        });
      },
      deps: [HttpClient],
    },
  ],
})
export class AppModule {}

Basic usage example

Create the base model:

import { IType, Model } from '@datx/core';
import { jsonapiAngular } from '@datx/jsonapi-angular';

export class BaseModel extends jsonapiAngular(Model) {
  public get id(): IType {
    return this.meta.id;
  }
}

Create specific domain models and add them to types in AppCollection

import { Attribute } from '@datx/core';
import { BaseModel } from 'src/app/base-model';

export class Artist extends BaseModel {
  public static endpoint = 'artists';
  public static type = 'project';

  @Attribute()
  public name!: string;
}
export class AppCollection extends jsonapiAngular(Collection) {
  public static readonly types = [Artist];
}

Create services for managing the models (one service per model):

import { Inject, Injectable } from '@angular/core';
import { CollectionService } from '@datx/jsonapi-angular';

@Injectable({
  providedIn: 'root',
})
export class ArtistsService extends CollectionService<Artist, AppCollection> {
  protected ctor = Artist;
}

Inject the service in your component or other services and use methods like getManyModels and getOneModel:

export class ArtistsComponent {
  public artists$ = this.artistsService.getAllModels();

  constructor(private readonly artistsService: ArtistsService) {}
}

That's it!

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
3.0.010latest
2.6.2-beta.10beta

Version History

VersionDownloads (Last 7 Days)Published
2.6.2-beta.10
2.6.10
3.0.010
3.0.0-beta.31
3.0.0-beta.10
2.6.00
2.5.10
2.5.00
2.5.0-beta.60
2.4.141
2.4.130
2.4.120
2.4.110
2.4.100
2.4.90
2.4.71
2.4.60
2.4.50
2.5.0-beta.50
2.5.0-beta.40
2.4.41
2.4.3-beta.11
2.4.3-beta.00
3.0.0-beta.00
2.5.0-beta.11
2.5.0-beta.00
2.4.21
2.3.20
2.3.10
2.3.00
2.2.31
2.2.20
2.2.00
2.1.10
2.1.00
2.0.01

Package Sidebar

Install

npm i @datx/jsonapi-angular

Weekly Downloads

19

Version

3.0.0

License

MIT

Unpacked Size

192 kB

Total Files

43

Last publish

Collaborators

  • danipavic
  • infinumcom
  • darkokukovec
  • jjanus
  • safo6m
  • fvoska