How to cast property in nested objects to ObjectId with loopback and mongodb?

1,037 views
Skip to first unread message

Farid Neshat

unread,
Jan 13, 2015, 6:46:29 AM1/13/15
to loopb...@googlegroups.com
I originally have posted this question on [stackoverflow][1] but I got no response. Hoping I can get some help over here:

Let's say I have the following model definition:

    {
      "name": "Report",
      "idInjection": true,
      "trackChanges": true,
      "mongodb": {
        "collection": "report"
      },
      "properties": {
        "resource" : {"type": "String"},
        "date" : {"type": "Date"},
        "people" : [ {
            // Here's where I like to have an id property.
            "role" : {"type": "String"},
            "hours" : {"type": "Number"}
        } ],
        "name" : {"type": "String"}
      },
      "validations": [],
      "relations": {},
      "acls": [],
      "methods": []
    }

Now I want to have id property in each object in people array(to be accessed with like report.people[0].id) and it should be **casted** to ObjectId on inserts and updates. But well, loopback doesn't have an `ObjectId` type and the only way seems to be using relations but then how should the foreign key be?

Is there any way to have the id property casted to ObjectId on inserts and updates?

Raymond Feng

unread,
Jan 13, 2015, 11:37:14 AM1/13/15
to Farid Neshat, loopb...@googlegroups.com
Have you looked at http://docs.strongloop.com/display/LB/Embedded+models+and+relations?

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bryan Clark

unread,
Jan 14, 2015, 12:35:52 AM1/14/15
to loopb...@googlegroups.com
This issue also might have some answers for you as the docs are a bit out of date in this area.
https://github.com/strongloop/loopback-datasource-juggler/issues/336

I think you want something like this instead:

"relations": {
  "people": {
    "type": "embedsMany",
    "model": "People",
    "options": {
       "persistent": true
    }
  }
}

 Don't forget to define your People model somewhere.

 ~ Bryan
Reply all
Reply to author
Forward
0 new messages