Skip to content
This repository was archived by the owner on Jun 16, 2021. It is now read-only.

Latest commit

 

History

History
356 lines (208 loc) · 10.8 KB

mapper.md

File metadata and controls

356 lines (208 loc) · 10.8 KB

redux-data-service > Mapper

Class: Mapper

This class implements the transform and normalize methods on the IMapper interface, to provide a default mechanism to transform a model instance into a ready-to-serialize object, and to normalize a raw data object back into a model instance. Any nested relationships will be side-loaded by dispatching an action to the related service.

Type parameters

R

Hierarchy

Mapper

Implements

Index

Constructors

Properties

Accessors

Methods


Constructors

constructor

new Mapper(ModelClass: IModelFactory<T>): Mapper

Defined in Mapper/Mapper.ts:20

Parameters:

Name Type
ModelClass IModelFactory<T>

Returns: Mapper


Properties

ModelClass

● ModelClass: IModelFactory<T>

Defined in Mapper/Mapper.ts:20


Accessors

fields

getfields(): any

Defined in Mapper/Mapper.ts:30

Returns: any


relationships

getrelationships(): any

Defined in Mapper/Mapper.ts:26

Returns: any


Methods

<Protected> isRelationship

isRelationship(key: any): any

Defined in Mapper/Mapper.ts:34

Parameters:

Name Type
key any

Returns: any


<Protected> loadRelatedModel

loadRelatedModel(model: IModel<T>, relatedModelData: any, relationship: IFieldRelationship): Promise<IModel<any>>

Defined in Mapper/Mapper.ts:228

Given the relatedModelData of a single item, normalize the data using the relationship's own mapper, converting it into a Model instance, then dispatch that related Model to its data service and return the Model.

Information about the parent model will be stored on the child model so we can look up the parent later.

Parameters:

Name Type
model IModel<T>
relatedModelData any
relationship IFieldRelationship

Returns: Promise<IModel<any>>


normalize

normalize(data: Partial<R>): Promise<IModel<T>>

Defined in Mapper/Mapper.ts:129

Creates a new IModel by normalizing the given raw data. If a nested relationship was included in the payload, it will be side-loaded.

Parameters:

Name Type Description
data Partial<R> -

Returns: Promise<IModel<T>>


normalizeField

normalizeField(data: Partial<R>): (Anonymous function)

Defined in Mapper/Mapper.ts:110

Returns a function, which when called, converts a single field on the provided raw data into its object equivalent if the given IFieldType implements the optional "normalize" method.

That function then returns a Promise which resolves with a tuple of the field name and the normalized value.

For example, an ISO date string will be converted into a Date object when given a DateField.

Parameters:

Name Type
data Partial<R>

Returns: (Anonymous function)


normalizeQueryResponse

normalizeQueryResponse(__namedParameters: object): Promise< IQueryResponse & object>

Defined in Mapper/Mapper.ts:170

Supports converting a raw query response object from the API into an IQueryResponse object.

Parameters:

__namedParameters: object

Name Type Description
data data -
items R[]

Returns: Promise< IQueryResponse & object>


<Protected> processNestedRelationship

processNestedRelationship(model: IModel<T>, nestedData: any, relationship: IFieldRelationship): Promise< string | string[]>

Defined in Mapper/Mapper.ts:209

Process the nestedData for the given relationship.

  • If it is a BelongsTo relationship, its data is normalized into a Model instance, added to its store and its id is returned.
  • If it is a HasMany relationship, the above is done for each nested object and the ids of the models are returned.

Parameters:

Name Type
model IModel<T>
nestedData any
relationship IFieldRelationship

Returns: Promise< string | string[]>


transform

transform(model: * IModel<T> | Partial<T>*): Promise<Partial<R>>

Defined in Mapper/Mapper.ts:72

Transforms the given Model into a plain javascript object based on the Model's fieldTypes. Each fieldType with serialize = false will be excluded.

Parameters:

Name Type Description
model IModel<T> | Partial<T> -

Returns: Promise<Partial<R>>


transformField

transformField(model: * IModel<T> | Partial<T>*): (Anonymous function)

Defined in Mapper/Mapper.ts:47

Returns a function, which when called, transforms the given fieldName on the provided model into its serialized equivalent if the given IFieldType implements the optional "transform" method.

That function will then return a Promise that resolves to provide a tuple of the fieldName and the value. The Promise is necessary for the rare circumstance that we need to perform the transform asynchronously.

For example, a Date object will be converted into an ISO Date string when given a DateField.

Parameters:

Name Type
model IModel<T> | Partial<T>

Returns: (Anonymous function)


transformList

transformList(models: IModel<T>[]): Promise<R[]>

Defined in Mapper/Mapper.ts:97

Transforms a given list of Models into an array of items of R

Parameters:

Name Type Description
models IModel<T>[] -

Returns: Promise<R[]>


transformPatch

transformPatch(model: * IModel<T> | Partial<T> | any*): Promise<any>

Defined in Mapper/Mapper.ts:85

Calls transform on the model and the model.original then creates a JSON patch to update the original to the updated

Parameters:

Name Type
model IModel<T> | Partial<T> | any

Returns: Promise<any>


<Protected> transformRelatedModel

transformRelatedModel(relatedModel: IModel<any>): Promise<object>

Defined in Mapper/Mapper.ts:198

Transform the given relatedModel using its own mapper.

Parameters:

Name Type
relatedModel IModel<any>

Returns: Promise<object>


<Protected> transformRelationship

transformRelationship(fieldValue: * IModel<any> | IModel<any>[]*, relationship: IFieldRelationship): Promise<object>

Defined in Mapper/Mapper.ts:183

Transform the given model or array of models depending on the relationship type.

Parameters:

Name Type
fieldValue IModel<any> | IModel<any>[]
relationship IFieldRelationship

Returns: Promise<object>