Define Entity Mapping (TypeScript)
When working with Domain services, mapping between api namespace and domain namespace is often needed, for example :
- You want to map between an operation request body schema and a service / command input entity.
- You want to map between service / command output entity to an operation response body schema.
Schemas and Entities are usually similar but different object models, Object mapping makes it easy to convert one model to another.
Already you will find a generated file in your src-impl/util directory with name Mapper.ts to include mapping logic between entities and schemas.
Description
This How-To will provide some code snippets that would explain the usage of Mapper utility class to create a mapping logic to map between entities and schemas.
Mapper Class
Mapper class gives you access to Entity Factory to create instances of different entities, and also the same logger as the implementation files.
See below code snippet as an illustration for:
- Mapping between a customer need entity and a customer need schema.
- Mapping between a customer need schema and a customer need entity.
import { Cnrv1Schema as Schema } from 'solution-framework';
import { mappers, Context, ObjectSchemaObject, Entity } from 'solution-framework';
import { cnr_CustomerNeed } from 'solution-framework/dist/sdk/v1/namespace/entity/cnr_CustomerNeed';
/**
* This class can be used to implement mapping logic between schemas and entities,
* It has access to instanceOf operator, factory and logger.
*/
export class Mapper extends mappers.BaseMapper {
constructor(context: Context) {
super(context);
}
/**
* This method contains mapping logic to map between a CustomerNeed (Schema) and a CustomerNeed (Entity)
*/
public mapCustomerNeedSchemaToEntity(customerNeedSchema: Schema.CustomerNeed): cnr_CustomerNeed {
this.log.debug('mapCustomerNeedEntityToSchema()');
// Create entity instance using entity factory
const customerNeedEntity = this.factory.entity.cnr.CustomerNeed();
// Map schema properties to Entity Properties
customerNeedEntity.dealSubject = customerNeedSchema.subject;
customerNeedEntity.dealRef = customerNeedSchema.dealRef;
// Party is a nested entity so we need to also create an instance using factory
customerNeedEntity.party = this.factory.entity.cnr.Party();
customerNeedEntity.party.partyRef = customerNeedSchema.partyRef;
customerNeedEntity.party.partyName = customerNeedSchema.partyName;
return customerNeedEntity;
}
/**
* This method contains mapping logic to map between a CustomerNeed (Entity) and a CustomerNeed (Schmea)
*/
public mapCustomerNeedEntityToSchema(customerNeedEntity: cnr_CustomerNeed): Schema.CustomerNeed {
this.log.debug('mapCustomerNeedEntityToSchema()');
return {
existingAccountingUnitRef: customerNeedEntity.existingAccountId,
subject: customerNeedEntity.dealSubject,
dealRef: customerNeedEntity.dealRef,
partyRef: (customerNeedEntity.party) ? customerNeedEntity.partyRef : null,
customerNeedRef: customerNeedEntity.customerNeedRef
}
}
Notice how we imported an Api with prefix cnrV1 defined schemas from the generated solution framework.
import { Cnrv1Schema as Schema } from 'solution-framework';
Notice how we imported an Entity with identifer CustomerNeed in domain with prefix cnr from the generated solution framework.
import { cnr_CustomerNeed } from 'solution-framework/dist/sdk/v1/namespace/entity/cnr_CustomerNeed';
You have successfully implemented various forms of mapper functions.