-
Notifications
You must be signed in to change notification settings - Fork 3
Mapper configuration
It is a set of configuration for the entire mapper object.
It can be used using the withConfiguration
function after mapper initialization.
e.g.
const mapper = new Mapper()
.withConfiguration(
configuration =>
configuration
.shouldRequireExplicitlySetProperties(true)
.shouldIgnoreSourcePropertiesIfNotInDestination(true)
);
The methods of the mapper configuration are:
shouldRequireExplicitlySetProperties(value: boolean)
shouldIgnoreSourcePropertiesIfNotInDestination(value: boolean)
shouldAutomaticallyMapArrays(value: boolean)
shouldAutomaticallyApplyImmutability(value: boolean)
maxDepth(value: number)
Default: false
The shouldRequireExplicitlySetProperties
method lets you define if defined properties ONLY should be mapped.
e.g.
class SourceClass {
a: string;
b: string;
}
class DestinationClass {
a: string;
b: string;
}
// Map signature require to recognized types at runtime
const sign = {
source: Symbol('s'),
destination: Symbol('d')
};
const sourceObject = new SourceClass();
sourceObject.a = 'a';
sourceObject.b = 'b';
const mapper = new Mapper()
.withConfiguration(conf => conf.shouldRequireExplicitlySetProperties(true));
mapper.createMap<SourceClass, DestinationClass>(sign, DestinationClass)
.forMember('a', opt => opt.mapFrom(source => source.a));
const destinationObject = mapper.map<SourceClass, DestinationClass>(sign, sourceObject);
console.log(destinationObject.a); // 'a'
console.log(destinationObject.b); // undefined
The property b of the source is not copied because it is not explicitly specified in the map ruleset.
Default: false
The shouldIgnoreSourcePropertiesIfNotInDestination
method lets you define if properties present in source object should be mapped in destination object.
Notice that a source property is defined as present if it is not undefined.
Default: true
The shouldAutomaticallyMapArrays
method lets you define if the mapper should automatically map arrays.
i.e.
A call to the map()
method of the mapper class can map a source object if it is a single object or an array.
Notice that this method works detecting if the source object is an array.
Notice also that the map()
method return type is a single destination class, even if it is an array of destination objects.
To get the right return type you should call the mapMany()
method.
Default: false
The shouldAutomaticallyApplyImmutability
method lets you define if the mapper should automatically apply immutability for everything.
It is disabled by default due to performance reasons:
- to automatically apply immutability, the mapper checks if the new mapped value is an object or an array each time a property is mapped
- to apply immutability, a spread operator is used, so the reallocation of the new object/array is slower than using its reference
Default: -1
The maxDepth
methods lets you define at which level of depth a nested mapping execution should stop.