Skip to content

Commit b6207cd

Browse files
committed
docs(mikroorm): update caveat about serialization
1 parent d66c171 commit b6207cd

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

content/recipes/mikroorm.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,24 @@ object.
185185

186186
#### Serialization
187187

188-
> warning **Note** MikroORM wraps every single entity relation in a `Reference<T>` or a `Collection<T>` object, in order to provide better type-safety. This doesn't play well with class-transformer and the [serialization technique described in the NestJS docs](/techniques/serialization).
188+
> warning **Note** MikroORM wraps every single entity relation in a `Reference<T>` or a `Collection<T>` object, in order to provide better type-safety. This will make [Nest's built-in serializer](/techniques/serialization) blind to any wrapped relations. In other words, if you return MikroORM entities from your HTTP or WebSocket handlers, all of their relations will NOT be serialized.
189189
190-
In order to achieve serialization with MikroORM, the recommended way is to use the [built-in serialization API](https://mikro-orm.io/docs/serializing).
190+
Luckily, MikroORM provides a [serialization API](https://mikro-orm.io/docs/serializing) which can be used in lieu of `ClassSerializerInterceptor`.
191+
192+
```typescript
193+
@Entity()
194+
export class Book {
195+
196+
@Property({ hidden: true }) // Equivalent of class-transformer's `@Exclude`
197+
hiddenField = Date.now();
198+
199+
@Property({ persist: false }) // Similar to class-transformer's `@Expose()`. Will only exist in memory, and will be serialized.
200+
count?: number;
201+
202+
@ManyToOne({ serializer: value => value.name, serializedName: 'authorName' }) // Equivalent of class-transformer's `@Transform()`
203+
author: Author;
204+
}
205+
```
191206

192207
#### Request scoped handlers in queues
193208

0 commit comments

Comments
 (0)