Hi there! I'm currently migrating from class-transformer to typia and nestia, and things are going pretty well so far!
However, I’ve run into a scenario where I used to rely on class-transformer in nestjs to transform fields before serializing them. This feature is particularly useful when my database schema differs from my API response.
For example, in my database, I store numbers as raw strings. Before sending the data to the frontend, I transform these strings into numbers with decimals. Here’s an example of how I used to handle this with class-transformer:
export class Foo {
@ApiProperty({ type: number }) // User sees a number in the response
@Transform(({ value }) => rawToNumber(value))
bar: string; // Stored as a string in the database, but returned as a number to the user
constructor(_bar: string) {
this.bar = _bar;
}
}
Is there a way to achieve similar functionality with typia or nestia?
Currently, I’m doing it like this, but I have many fields to transform, and this approach creates a lot of repetitive code:
interface FooTypia {
bar: number;
}
const DBData = {
bar: '10',
};
const response: FooTypia = {
bar: rawToNumber(DBData.bar),
};
Question
Hi there! I'm currently migrating from
class-transformer
totypia
andnestia
, and things are going pretty well so far!However, I’ve run into a scenario where I used to rely on
class-transformer
innestjs
to transform fields before serializing them. This feature is particularly useful when my database schema differs from my API response.For example, in my database, I store numbers as raw strings. Before sending the data to the frontend, I transform these strings into numbers with decimals. Here’s an example of how I used to handle this with
class-transformer
:Is there a way to achieve similar functionality with
typia
ornestia
?Currently, I’m doing it like this, but I have many fields to transform, and this approach creates a lot of repetitive code:
Thank you in advance!