30. September 2021
$defineProperty()
defineProperty()
@adonisjs/[email protected] added $defineProperty()
$defineProperty()
supports two strategies:
inherit
, which can be used to to take property of parent model and apply to current modeldefine
, which can be used to define some value on current model
For example
// Get 'primaryKey' from parent model. In case parent model doesn't have it
// default to 'id'
this.$defineProperty('primaryKey', 'id', 'inherit')
// Set secondaryKey to 'uuid', doesn't matter what parent model has it set to
this.$defineProperty('secondaryKey', 'uuid', 'define')
It’s widely used internally, but can also used to define static properties on model that need to be serialized.
Especially useful in constructors and boot
methods