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
+32-16Lines changed: 32 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,22 +44,6 @@ findOne(): UserEntity {
44
44
45
45
> **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.
46
46
47
-
> info **Hint** You could enforce transformations at the controller level by stating `@SerializeOptions({{ '{' }} type {{ ':' }} UserEntity {{ '}' }})` to transform all responses to the instance of the dto without calling `plainToInstance`. Doing so will ensure that decorators on the `UserEntity` 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.
48
-
49
-
```typescript
50
-
@UseInterceptors(ClassSerializerInterceptor)
51
-
@Get()
52
-
@SerializeOptions({ type : UserEntity })
53
-
findOne(): UserEntity {
54
-
return {
55
-
id: 1,
56
-
firstName: 'Kamil',
57
-
lastName: 'Mysliwiec',
58
-
password: 'password',
59
-
};
60
-
}
61
-
```
62
-
63
47
> info **Hint** The `ClassSerializerInterceptor` is imported from `@nestjs/common`.
64
48
65
49
When this endpoint is requested, the client receives the following response:
@@ -74,6 +58,38 @@ When this endpoint is requested, the client receives the following response:
74
58
75
59
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.
76
60
61
+
62
+
#### Using the SerializeOptions to transform plain objects to the class instance.
63
+
64
+
You could enforce transformations at the controller level by stating `@SerializeOptions({{ '{' }} type {{ ':' }} <CLASS_INSTANCE> {{ '}' }})` 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.
65
+
66
+
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.
67
+
68
+
```typescript
69
+
@UseInterceptors(ClassSerializerInterceptor)
70
+
@Get()
71
+
@SerializeOptions({ type : UserEntity })
72
+
findOne(@Query() { id }: { id: number }): UserEntity {
73
+
if (id===1) {
74
+
return {
75
+
id: 1,
76
+
firstName: 'Kamil',
77
+
lastName: 'Mysliwiec',
78
+
password: 'password',
79
+
};
80
+
}
81
+
82
+
return {
83
+
id: 2,
84
+
firstName: 'Kamil2',
85
+
lastName: 'Mysliwiec2',
86
+
password: 'password2',
87
+
};
88
+
}
89
+
```
90
+
91
+
> info **Hint** By stating the return type, we take advantage of Typescript to check if the plain js object conforms to the class shape.`plainToInstance`'s second argument does not check if the object has the same shape as the class instance (somewhat equivalent to typecasting), and could lead to bugs in your application.
92
+
77
93
#### Expose properties
78
94
79
95
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