I am new to NestJS as a server.
I have a table in DB that I am querying, and the column name is precedence, and it is boolean meaning in DB just 0 or 1.
I have this code that I understood maps the data to DTO.
forMember(
(destination) => destination.precedence,
mapFrom((source) => source.precedence),
),
but instead of 1 I need it to be true and instead of 0 I need it to be false
Can it be done? in the mapper?
CodePudding user response:
// You can convert a falsy or truthy value to a boolean with a double negation
console.log(!!0)
console.log(!!1)
// And then you can convert your boolean to string if you need to
console.log((!!0).toString())
console.log((!!1).toString())
CodePudding user response:
Yes, it can be done in the mapper. You can use the mapFrom function to modify the value before mapping it to the destination property.
Here's an updated example that maps 0 to false and 1 to true:
forMember( (destination) => destination.precedence, mapFrom((source) => source.precedence === 1), ),
The value from the source.precedence is compared to 1, and if it is equal, the destination.precedence is set to true. If it's not equal, it will be set to false.
Hope this helps.
CodePudding user response:
you just have to check if source.precedence
is equal to '1' then return true
else return false
forMember(
(destination) => destination.precedence,
mapFrom((source) => source.precedence === 1 ? true : false ), // this will return false also if the value is different from 0 and 1
),