I am not able to understand the difference between
Ctx.instance
and Ctx.data
in loopback 'before save ' callback.
The difference is described in our documentation, see Operation hooks >> before save. Cross-posting the relevant content here:
Depending on which method triggered this hook, the context will have one of the following sets of properties:
Full save of a single model
- (...)
instance
- the model instance to be saved. The value is an instance of Model class.Partial update of possibly multiple models
- (...)
data
- the (partial) data to apply during the updatecurrentInstance
- the affected instance.
When you call Customer.create({/*...*/})
, the hook is invoked with ctx.instance
set to the instance of your model with all properties populated from the object passed to create()
:
{
name: 'Miroslav',
isPreferred: false,
// etc.
}
When you call customer.patchAttributes({isPreferred: true})
, the hook is invoked with ctx.data
containing only the properties provided to patchAttributes()
.
{
isPreferred: true
}
You can learn more in the source code, see loopback-datasource-juggler/lib/dao.js
.