Skip to content

Getting started

Mihael Safaric edited this page Sep 27, 2024 · 3 revisions

The following examples describe the basic ngx-hal setup with the setup for CRUD operations on User resource.

Datastore service needs to be provided, usually it is provided in the application's top-most module.

import { NgModule } from '@angular/core';
import { DatastoreService } from 'ngx-hal';

@NgModule({
  declarations: [...],
  imports: [...],
  providers: [
    {
      provide: DatastoreService,
      useClass: HalDatastoreService, // your own class which extends DatastoreService
      deps: [
        HttpClient
      ]
    }
  ]
})
export class AppModule { }

Example of your DatastoreService class:

import { HttpClient } from '@angular/common/http';
import { DatastoreService, DatastoreConfig } from 'ngx-hal';

@DatastoreConfig({
  paginationClass: MyHalPagination, // your own class which extends Pagination
})
export class HalDatastoreService extends DatastoreService<MyHalPagination> {
  constructor(public httpClient: HttpClient) {
    super(httpClient);
  }
}

Your models must extends HalModel. Model properties must be decorated with one of the following decorators: Attribute, HasOne, HasMany

import { HalModel } from 'ngx-hal';

export class User extends HalModel<MyHalPagination> {
  @Attribute()
  public name: string;
}

You want to have a dedicated service which will handle all the external operations for the resource. Such service must extend ModelService,

import { Injectable } from '@angular/core';
import { ModelService } from 'ngx-hal';
import { HalDatastoreService } from './hal-datastore.service';
import { User } from '../../models/user.model';

@Injectable()
export class UserService extends ModelService<User> {
  constructor(datastore: HalDatastoreService) {
    super(datastore, User);
  }
}

TODO describe available operations on UserService

Recommendation

To minimize vendor lock-in and to make future library updates easier, it is recommended to create a wrapper classes around ngx-hal constructs. Apart a wrapper around Datastore service (HalDatastoreService in the example above), it is recommended to create the following:

  • a wrapper around HalModel and HalDocument classes

    class MyHalModel extends HalModel<MyHalPagination, HalDatastoreService> {}
    class MyHalDocument<T extends MyHalModel> extends HalDocument<T, MyHalPagination> {}
  • a wrapper around model service

    class HalModelService<T extends MyHalModel> extends ModelService<T, MyHalPagination> {}