You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/techniques/serialization.md
+33-1Lines changed: 33 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,7 +44,7 @@ findOne(): UserEntity {
44
44
}
45
45
```
46
46
47
-
> **Warning** Note that we must return an instance of the class. If you return a plain JavaScript object, for example, `{{ '{' }} user: new UserEntity() {{ '}' }}`, the object won't be properly serialized.
47
+
> **Warning** Note that we must return an instance of the class. If you return a plain JavaScript object, for example, `{{ '{' }} user: new UserEntity() {{ '}' }}`, the object won't be properly serialized.
48
48
49
49
> info **Hint** The `ClassSerializerInterceptor` is imported from `@nestjs/common`.
50
50
@@ -60,6 +60,38 @@ When this endpoint is requested, the client receives the following response:
60
60
61
61
Note that the interceptor can be applied application-wide (as covered [here](https://docs.nestjs.com/interceptors#binding-interceptors)). The combination of the interceptor and the entity class declaration ensures that **any** method that returns a `UserEntity` will be sure to remove the `password` property. This gives you a measure of centralized enforcement of this business rule.
62
62
63
+
64
+
#### Using the SerializeOptions to transform plain objects to the class instance.
65
+
66
+
You could enforce transformations at the controller level by stating `@SerializeOptions({{ '{' }} type {{ ':' }} <CLASS> {{ '}' }})` to transform all responses to the instance. Doing so will ensure that decorators on the class will always be applied, even if plain objects are returned. This allows for terser code, without the added verbosity of instantiating the class or calling `plainToInstance` repeatedly.
67
+
68
+
In this example, even though a plain js object was returned in both conditional statements, they will all be converted into `UserEntity` with annotated class-validator or class-transformer decorators applied.
69
+
70
+
```typescript
71
+
@UseInterceptors(ClassSerializerInterceptor)
72
+
@Get()
73
+
@SerializeOptions({ type : UserEntity })
74
+
findOne(@Query() { id }: { id: number }): UserEntity {
75
+
if (id===1) {
76
+
return {
77
+
id: 1,
78
+
firstName: 'Kamil',
79
+
lastName: 'Mysliwiec',
80
+
password: 'password',
81
+
};
82
+
}
83
+
84
+
return {
85
+
id: 2,
86
+
firstName: 'Kamil2',
87
+
lastName: 'Mysliwiec2',
88
+
password: 'password2',
89
+
};
90
+
}
91
+
```
92
+
93
+
> info **Hint** By stating the expecting controller return type, we can take advantage of Typescript to check if the returned plain object conforms to the DTO/entity shape. `plainToInstance`' does not hint if the plain object conforms to the the DTO/entity class instance you are transforming it into (somewhat equivalent to typecasting). This could lead to bugs in your application.
94
+
63
95
#### Expose properties
64
96
65
97
You can use the `@Expose()` decorator to provide alias names for properties, or to execute a function to calculate a property value (analogous to **getter** functions), as shown below.
0 commit comments