Skip to content

Commit 4e53ef1

Browse files
Merge pull request #2243 from marcpicaud/patch-3
docs(mikroorm): add a caveat about serialization
2 parents 7954b80 + 89c7247 commit 4e53ef1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

content/recipes/mikroorm.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,27 @@ object.
182182
> still need CLI config with the full list of entities. On the other hand, we can
183183
> use globs there, as the CLI won't go thru webpack.
184184
185+
186+
#### Serialization
187+
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.
189+
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+
@Property({ hidden: true }) // Equivalent of class-transformer's `@Exclude`
196+
hiddenField = Date.now();
197+
198+
@Property({ persist: false }) // Similar to class-transformer's `@Expose()`. Will only exist in memory, and will be serialized.
199+
count?: number;
200+
201+
@ManyToOne({ serializer: value => value.name, serializedName: 'authorName' }) // Equivalent of class-transformer's `@Transform()`
202+
author: Author;
203+
}
204+
```
205+
185206
#### Request scoped handlers in queues
186207

187208
> info **info** `@UseRequestContext()` decorator was added in v4.1.0

0 commit comments

Comments
 (0)