0
votes

I have to create a Rest API to a directional graph model using strongloop loopback. I have a collection of nodes and a collection of relationships relating the nodes. The relationship collection stores the nm relations.

In the models/node.json I have included the following relations

  "relations": {
    "incomingNodes": {
      "type": "hasMany",
      "model": "node",
      "through": "relationship",
      "foreignKey": "endNode"
    },
    "outgoingNodes": {
      "type": "hasMany",
      "model": "node",
      "through": "relationship",
      "foreignKey": "startNode"
    }
}

In the models/relationships.json:

  "relations": {
    "startNode": {
      "type": "belongsTo",
      "model": "node",
      "foreignKey": "nodeId"
    },
  "endNode": {
      "type": "belongsTo",
      "model": "node",
       "foreignKey": "nodeId"
    }
  }

The explorer is populated as expected, but if I create a relation using the Rest API

/nodes/{id}/incomingNodes/rel/{fk}

and look into the database, things are getting weird. The following data is written:

{
  "id": "55ff1072b88c93a82ae527ea", <--id of the new relationship
  "nodeId": "55fef9a156468fe629d94ac8" <-- id of the {fk}
}

I would expect that something like this:

{
  "id": "55ff1072b88c93a82ae527ea", <--id of the new relationship
  "startNode": "55fef9a156468fe629d94ac8", <-- id of the {fk}
  "endNode": "55fef9a156468fe629d94ac8" <-- id of the {id}

}

I've tried all kinds of combinations, but just can't get it working.

Please help

1
Foreign keys in relationships.json are incorrect. Try same keys as in node.json. "foreignKey": "startNode" and "foreignKey": "endNode" instead of "foreignKey": "nodeId". - A.Z.
I've tried that one. :-( , Finally I've decided to give up, and implement the logic in code using remote methods. I think that the problem is that I have 2 directed relations pointing from the same model to the same model. - marcelde
It would be an improvement if I could control the relations a bit more in stead of relying on "magic". By this I mean that you have the possibility to configure the FK explicit. In my case: Node.id <-> Relationship.startId (trough) Relationship.endId <-> Node.Id . - marcelde

1 Answers

0
votes

A HasManyThrough relation needs 2 things:

  • foreignKey to identify the origin (model declaring the relation) in the through model,
  • keyThrough to identify the target of the relation in the through model.

A BelongsTo relation needs the key to use in the model declaring the relation to identify the target.

Now, assuming the following schemas

Node (id)
Relationships (id, startNodeId, endNodeId)

you need to add keyThrough in models/node.json relations:

"relations": {
  "incomingNodes": {
    "type": "hasMany",
    "model": "node",
    "through": "relationship",
    "foreignKey": "endNodeId",
    "keyThrough": "startNodeId"
  },
  "outgoingNodes": {
    "type": "hasMany",
    "model": "node",
    "through": "relationship",
    "foreignKey": "startNodeId",
    "keyThrough": "endNodeId"
  }

and use startNodeId and endNodeId in models/relationships.json:

"relations": {
  "startNode": {
    "type": "belongsTo",
    "model": "node",
    "foreignKey": "startNodeId"
  },
  "endNode": {
    "type": "belongsTo",
    "model": "node",
    "foreignKey": "endNodeId"
  }
}

Note that i used endNodeId and startNodeId for property names to preserve your relations names in relationships.json, since you can't have both a relation and a property using the same name in a model.