How to access the properties of a model from a loopback connector

1,340 views
Skip to first unread message

Eric Chou

unread,
Dec 3, 2014, 4:27:42 PM12/3/14
to loopb...@googlegroups.com
Hi,
I am coding loopback connector for couchbase, and stuck at the topic "include".  As far as I can see, the "include" filter passes an array of strings which contains all the names of the models that are related to the current model. For example, here is the properties of the model "Ticket":

{
    "name": "Ticket",
    "base": "PersistedModel",
    "properties": {
        "id": {
            "type": "String",
            "id": true,
            "required": true,
            "index": true
        }
    },
    "validations": [],
    "relations": {
        "actions": {
            "type": "hasMany",
            "model": "Action",
            "foreignKey": "ticketID"
        }
    },
    "acls": [],
    "methods": []
}

It has one to many relations to the model "Action", the foreignKey is "ticketID". Therefore, when the filter looks like  {"include":["Action"]}, I need to be able to read the properties of the model "Ticket", find the "foreignKey": "ticketID" of the relation "actions" so that I could write couchbase N1QL statement to process the query.

Question is: how can I access the properties of a model due to its name?

Thanks in advance for any help

Eric

Raymond Feng

unread,
Dec 3, 2014, 4:49:20 PM12/3/14
to Eric Chou, loopb...@googlegroups.com
<ModelClass>.definition.properties.

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.

Eric Chou

unread,
Dec 4, 2014, 9:21:37 AM12/4/14
to Raymond Feng, loopb...@googlegroups.com
Hi, Raymond,
Thank you for this fast response.
There is a question regarding your answer: the "<ModelClass>", where is it?  For example, I have a method called:

CouchbaseDB.prototype.count = function (model, callback, where) {
...
}

The type of "model" is a string, I am not able to call its "definition" property. Could you tell me how to obtain that ModelClass?

Thanks again
Eric

Raymond Feng

unread,
Dec 4, 2014, 11:36:35 AM12/4/14
to Eric Chou, loopb...@googlegroups.com
If your connector extends the base connector, the properties can be accessed as:

var Connector = require('loopback-connector').Connector;

function CouchbaseDB(...) { ... Connector.call(this, couchbase', settings); }


...
util.inherits(CouchbaseDB, Connector);


var props = this._models[model].properties;

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.

Eric Chou

unread,
Dec 4, 2014, 2:59:46 PM12/4/14
to Raymond Feng, loopb...@googlegroups.com
That solution works very well for me. One more thing: am I able to retrieve other settings such as "relations"?
for example, in my guest.json:
{
  "name": "guest",
  "plural": "Guests",
  "properties": {
    "id": {
      "type": "string",
      "required": true,
      "id": true
    },
    "fname": {
      "type": "string",
      "required": true
    },
    "lname": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
       "Phones":{
          "type": "hasMany",
          "model": "phone",
          "foreignKey": "guestid"
       }
    },
  "acls": [],
  "methods": []
}


I tried to retrieve the definition of "relations" with using this code:

var relations = this._models[model].relations;

the variable "relations" actually has the type of "undefined".

Any idea why did that happen?

Thanks

Eric

Raymond Feng

unread,
Dec 4, 2014, 4:20:36 PM12/4/14
to Eric Chou, loopb...@googlegroups.com
<MyModelClass>.relations is the object that contains relation definitions by name.

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.

Eric Chou

unread,
Dec 5, 2014, 9:49:12 AM12/5/14
to Raymond Feng, loopb...@googlegroups.com
Yes, I finally figured out how to access the relations:

var relations = this._models[model].settings.relations;


Thank you for your help,really appreciate it

Eric

Raymond Feng

unread,
Dec 5, 2014, 11:18:43 AM12/5/14
to Eric Chou, loopb...@googlegroups.com
var relations = this._models[model].relations is a better choice. There are two ways to add relations to a model class:

- ‘relations’ property of the model JSON definition 
- Model.<relationType> methods

Both of them are normalized into model class’s relations property.

var relations = this._models[model]
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.
Reply all
Reply to author
Forward
0 new messages