Loopback-datasource-juggler: Embedded relations don't invoke operation hooks

Created on 4 Mar 2015  路  8Comments  路  Source: loopbackio/loopback-datasource-juggler

My code:

{
  "name": "Step",
  "base": "PersistedModel",
  "strict":true,
  "properties": {
    "pos": {
      "type": "number",
      "required": true
    },
    "text": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "image": {
      "type": "embedsOne",
      "model": "Image",
      "options": {
        "persistent":true
      }
    }
  },
  "acls": [],
  "methods": []
}
{
  "name": "Image",
  "base": "PersistedModel",
  "properties": {
    "name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}
Image.observe('access', function imageProcessing(ctx, next) {
        console.log('access') 
}

Image.observe('before save', function imageProcessing(ctx, next) {
        console.log('before save image') 
}

Not show logs in console.

bug stale

Most helpful comment

I can confirm that on my mongodb based setup, 'before save' event is not firing on the embedded model when I call from the API Explorer POST /Model/{id}/embedded

unless - I define it as persistent in the embedded model options

All 8 comments

Thank you for reporting the issue. For a better context, can you reproduce the same problem when using the "old" model hooks like "beforeCreate"?

I changed all data model. I using mysql right now. Sorry :/

@bajtos
Any Idea when this issue will be fixed? I"m having the same problem. When I try to create a new model that has an embedded relation it is not calling the "create" method set in the connector.
Please help!

There is a partial solution proposed in #516. Could you please try it out and let us know how it works for you?

@bajtos What should be this solution?

I can confirm that on my mongodb based setup, 'before save' event is not firing on the embedded model when I call from the API Explorer POST /Model/{id}/embedded

unless - I define it as persistent in the embedded model options

+1

@fabien , I'm trying to understand persistent. In documentation, it says persistent - denote if the embedded items should be persisted, default to false. But when I set it to be 'false', I can see the embedded document is saved to mongo within the parent document. What does persistent control?

Was this page helpful?
0 / 5 - 0 ratings