Serializing
By default, all entities are monkey patched with toObject()
and toJSON
methods:
When you serialize your entity via JSON.stringify(entity)
, its toJSON
method will be
called automatically. You can provide custom implementation for toJSON
, while using
toObject
for initial serialization:
Do not forget to pass rest params when calling
toObject(...args)
, otherwise the results might not be stable.
#
Hidden PropertiesIf you want to omit some properties from serialized result, you can mark them with hidden
flag on @Property()
decorator:
#
Shadow PropertiesThe opposite situation where you want to define a property that lives only in memory (is
not persisted into database) can be solved by defining your property as persist: false
.
Such property can be assigned via one of Entity.assign()
, em.create()
and
em.merge()
. It will be also part of serialized result.
This can be handle when dealing with additional values selected via QueryBuilder
or
MongoDB's aggregations.
#
Property SerializersAs an alternative to custom toJSON()
method, we can also use property serializers.
They allow to specify a callback that will be used when serializing a property: