Skip to content

DatastoreService

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

DatastoreService is the main service used by the library. This service should be extended by your own datastore service which will be configured to your needs.

constructor(public http: HttpClient) { }

Properties

  • networkConfig: NetworkConfig

    • global network configuration
    • default values here
    • can be set via DatastoreConfig decorator or by setting networkConfig directly on DatastoreService
  • storage: HalStorage

    • local storage where models are saved
    • see HalStorage
  • httpParamsOptions

  • modelTypes: Array<typeof HalModel>

    • list of models that use string identificators when defining relationships, see more in the Guide
    • available from ngx-hal@3

Methods

buildUrl

buildUrl(model?: HalModel): string;
  • returns URL composed of baseUrl, endpoint, and model endpoint (if model is passed)

createHalDocument

createHalDocument<T extends HalModel>(rawResource: RawHalResource, modelClass: ModelConstructor<T> | ModelConstructorFn<T>, rawResponse?: HttpResponse<any>): HalDocument<T>;
  • returns an instance of HalDocument class or an instance of a class provided as a substitute for HalDocument class
  • HalDocument class resolution strategy:

findOne

findOne<T extends HalModel>(
  modelClass: ModelConstructor<T, P>,
  modelId: string,
  includeRelationships: Array<string | RelationshipRequestDescriptor> = [],
  requestOptions: RequestOptions = {},
  customUrl?: string,
  subsequentRequestsOptions: RequestOptions
): Observable<T> {
  • modelClass

    • class of a model which will be fetched
  • includeRelationships: Array<string>

  • requestOptions: RequestOptions

    • optional
    • additional request options
    • those options will be used only for the main request, custom options for relationship calls can be set via subsequentRequestsOptions
  • customUrl: string

    • optional
    • if provided, customUrl will be used when making an HTTP call
  • subsequentRequestsOptions: RequestOptions

    • optional
    • when specified, the specified request options will be used for the relationship calls which do not have request options specified through includeRelationships parameter
    • if the specific relationship has request options specified in includeRelationships, subsequentRequestsOptions will be ignored for that API call

find

find<T extends HalModel<P>>(modelClass: ModelConstructor<T, P>, params: object | { [param: string]: string | string[] } | HttpParams, includeMeta: boolean, includeRelationships: Array<string | RelationshipRequestDescriptor>, requestOptions: RequestOptions, customUrl?: string, subsequentRequestsOptions?: RequestOptions, storePartialModels?: boolean): Observable<HalDocument<T, P> | Array<T>>;
  • modelClass

    • class of the models which are being fetched
  • params: object | { [param: string]: string | string[] } | HttpParams

    • optional
    • parameters which will be send in the reqest
    • requestOptions.params is ignored in favor of params (TODO: change that in future versions of the lib)
  • includeMeta: boolean

    • optional
    • if omitted or falsy, Observable<Array<Model>> is returned
    • if truthy, Observable<HalDocument<Model>> is returned (see HalDocument)
  • includeRelationships: Array<string>

  • requestOptions: RequestOptions

    • optional
    • additional request options
    • those options will be used only for the main request, custom options for relationship calls can be set via includeRelationships or subsequentRequestsOptions
    • requestOptions.params is ignored in favor of params (TODO: change that in future versions of the lib)
  • customUrl: string

    • optional
    • if provided, customUrl will be used when making an HTTP call
  • subsequentRequestsOptions: RequestOptions

    • optional
    • when specified, the specified request options will be used for the relationship calls which do not have request options specified through includeRelationships parameter
    • if the specific relationship has request options specified in includeRelationships, subsequentRequestsOptions will be ignored for that API call
  • storePartialModels: boolean

    • optional
    • default value: false
    • forces storing fetched models to the local store
    • see HalStorage
  • customUrl: string

    • optional
    • if provided, customUrl will be used when making an HTTP call

request

  • makes a custom HTTP request
  • GET method
    • it expects the response to be a valid HAL document
    • it creates a proper HAL model extracted from the response and saves it in the local store
request<T extends HalModel<P>>(method: string, url: string, requestOptions: RequestOptions, modelClass: ModelConstructor<T, P>, singleResource: false): Observable<HalDocument<T>>;
request<T extends HalModel<P>>(method: string, url: string, requestOptions: RequestOptions, modelClass: ModelConstructor<T, P>, singleResource: true): Observable<T>;
request<T extends HalModel<P>>(method: string, url: string, requestOptions: RequestOptions, modelClass: ModelConstructor<T, P>, singleResource: boolean, includeNetworkConfig: boolean = true): Observable<HalDocument<T, P> | T>;
  • method: string

    • HTTP method, supported values: GET
  • url: string

    • request URL
      • if includeNetworkConfig is false, request will be made to {URL}
      • (default) if includeNetworkConfig is true, request will be made to networkConfig.host/networkConfig.endpoint/{URL}
  • requestOptions: RequestOptions

    • additional request options
  • modelClass

    • class of a model which will be fetched
  • singleResource: boolean

    • set to true if single resource is expected in the response, false otherwise
    • when true, T will be returned, HalDocument<T> otherwise
  • includeNetworkConfig: boolean

    • see url parameter

save

save<T extends HalModel<P>>(model: T, modelClass: ModelConstructor<T, P>, requestOptions?: RequestOptions, options: CustomOptions = {}): Observable<T>;
  • makes PUT request if model is already saved, POST request otherwise
  • model attributes and request options (ones passed as an argument and the global ones) are passed in the request
  • payload is generated by calling generatePayload on a passed model
  • request headers are generated by calling generateHeaders on a passed model
  • if newly created resource is passed in the response of POST request then a new model is returned
  • if newly created resource is not passed in the response of POST request then the existing model is returned
    • if Location header is set then the model URL is updated (it affects the following model properties: selfLink, uniqueModelIdentificator, id, isSaved)
  • options
    • specificFields: Array
      • optional
      • if provided, only the listed fields are taken in consideration while generating a payload (if they are changed)
    • buildUrlFunction
      • function used for generating the final request URL
      • optional
      • default: model.endpoint
    • transformPayloadBeforeSave
      • optional
      • the function gets a generated payload as an argument and the value returned from the function will be the payload which will be sent in the request

update

update<T extends HalModel<P>>(model: T, requestOptions?: RequestOptions, options: CustomOptions = {}): Observable<T>;
  • make a PATCH request which changed properties only
  • options
    • specificFields: Array
      • optional
      • if provided, only the listed fields are taken in consideration while generating a payload (if they are changed)
    • buildUrlFunction
      • function used for generating the final request URL
      • optional
      • default: model.endpoint
    • transformPayloadBeforeSave
      • optional
      • the function gets a generated payload as an argument and the value returned from the function will be the payload which will be sent in the request

delete

delete<T extends HalModel<P>>(model: T, requestOptions?: RequestOptions, options?: CustomOptions: Observable<void>;
  • makes DELETE request
  • request options (ones passed as an argument and the global ones) are passed in the request
  • options
    • buildUrlFunction
      • function used for generating the final request URL
      • optional
      • default: model.defaultUrlBuildFunction
  • the model is removed from the local storage

head

head<T extends HalModel<P>>(url: string, requestOptions?: RequestOptions): Observable<any>;
  • makes HEAD request
  • request options (ones passed as an argument and the global ones) are passed in the request

fetchModelRelationships

fetchModelRelationships<T extends HalModel<P>>(model: T, relationshipNames: RelationshipRequestDescriptor | Array<RelationshipRequestDescriptor>): Observable<T>;
  • fetches one or more relationships of passed model
  • returns the original model which is passed through arguments
  • the original model is fetched from the local store (HTTP call won't be made)
  • requestOptions
    • optional
    • options which will be passed to all requests

createModel

createModel<T extends HalModel<P>>(modelClass: ModelConstructor<T, P>, recordData: object = {}): T;
  • locally creates an instance of a model class which is passed through the parameters, modelClass (it does not tigger saving of newly created model)
  • model properties and relationships can be passed via recordData

Additional info

DatastoreService can be configured via DatastoreConfig decorator.