Comments (4)
Just "driving past" but saw this when I went to search for "performance" (somehow).
I think it's meant for the backend, but definitely on the "user-data facing" side. I wouldn't consider that frontend.
I think the main niche this library is used for is handling incoming data (e.g. from users, API consumers, etc), which might come in as JSON, then need to be transformed to a class form (and then either validated with class-validator or with something else). So using it more for DTOs would be expected. It's probably expected your DB data is already validated or typesafe (SQL, Mongoose), or is being accessed as JSON (plain MongoDB driver) in which case you can use class-transformer + class-validator anyway.
(A less opinionated equivalent might be Zod. In other languages, there is a similar split in responsibilities with e.g. Java and Kotlin, where you might have one library handling serialisation of DTOs (e.g. Kotlinx Serialization, Jackson), and then another library (e.g. Spring Data) handling the ORM models. This handles roughly the same niche as Kotlinx, but without the AOT compilation benefits).
Basically, taking unknown, plain objects, and turning them into something richer with methods, state, or encapsulation as required.
Re getters and setters, makes sense that you can't use plainToInstance
to populate something with a getter, you would need a corresponding setter. The other way should work though, e.g. instanceToPlain or instanceToInstance. The getter has to be accessible from the "left hand side" of the operation basically.
I guess you can approach this in a number of ways and potentially make this quite complicated, like the below:
export class UserDTO {
@Transform(({ value, obj }) => value ?? obj.screenName.split(" ")[0])
firstName: string;
@Transform(({ value, obj }) => value ?? obj.screenName.split(" ")[1])
lastName: string;
@Expose()
get screenName() {
return `${user.firstName} {user.lastName}`;
}
}
export function fromUser(user: UserModel): UserDTO {
return plainToInstance(UserDTO, {
firstName: user.firstName,
lastName: user.lastName,
screenName: `${user.firstName} {user.lastName}`;
});
}
from class-transformer.
from class-transformer.
@ceigey thank you for your detailed answer, I aggree.
This library can be used at the backend or frontend, where you find it suitable. If you have good tests you should be fine.
Although, if you want class-transformer
to work nicely with an ORM, you will have to write some custom Transformers as the integration with other libraries is out of the scope of this library and will not implement anything related to any ORM.
from class-transformer.
Closing as answered. If you have any questions left feel free to comment on this issue.
from class-transformer.
Related Issues (20)
- Include @IsOptional values to the body if they are missing with default value HOT 2
- fix: Can't use instanceToPlain with Nitro.js event. HOT 4
- External Values are not stripped. HOT 4
- fix: plainToClass cannot handle URL values HOT 6
- feature: function support of options.discriminator.subTypes HOT 2
- plainToClassFromExist is Deprecated what is the replacement for generics? HOT 3
- Cannot transform string to number HOT 7
- question: Options in @Transform Decorator Do Not Reflect Those Passed to plainToInstance in class-transformer HOT 3
- question: Casting Json to the required datatype HOT 2
- instanceToPlain not working properly using nestjs HOT 4
- Transform decorator in class-transformer does not wait for asynchronous function and returns a Promise instead of the transformed value HOT 3
- fix: Expose not working correctly with toClassOnly HOT 2
- fix: bug in casting boolean properties from `@Query()` in Nest.js HOT 4
- fix: Unable to use "name" as a property in class-validator HOT 1
- fix: No Capability to BigInt? HOT 1
- fix: JSDoc comments do not automatically apply to overloads
- fix: incorrect property name in validation messages when using @Expose()
- fix:
- fix: Multiple Getter Calls in class-transformer
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from class-transformer.