Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strongloop EmbedsMany helper methods not found

Some questions that I can't find the answer to in the documentation.

I'm trying to get a structure like this:

 Node: 
   id: '1sdf12asd123',
   name: 'node1',
   history: 
      [ ts: 234234234234,
        data: { 'foo': 'bar' }
      ],
      ...

So each individual node, has many history items. And I want to be able to push new data to that, without overwriting anything.

Now, I don't want to store everything under each node, but rather in a seperate document, so I think embedsMany would be suitable for this:

{
  "name": "Node",
  "plural": "Nodes",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
    "history": {
      "type": "embedsMany",
      "model": "History",
      "foreignKey": "HistoryId"
    }
  },
  "acls": [],
  "methods": {}
}

So History would be simply:

{
  "name": "History",
  "base": "PersistedModel",
  "idInjection": true,
  "properties": {
    "ts": {
      "type": "Date"
    },
    "data": {
      "type": "Object"
    }
  },
  "validations": [],
  "relations": {
    "node": {
      "type": "belongsTo",
      "model": "Node",
      "foreignKey": "NodeId"
    }
  },
  "acls": [],
  "methods": {}
}

I'm not sure the foreignKey part is right, but I've tried a lot of different combinations, and this one seems logical.

The History model is not public, so not exposed as an endpoint. And I want to utilize the relations as much as possible, rather than have a seperate endpoint.

Main issue here is i would like to use Nodes.history.add() like described here.

But I've tried about all the different methods from Remote methods to Operation Hooks, but I can't find the helper methods mentioned. There's no code example to be found about this.

Partly I think this is because the documentation is at times not very clear or assumes a certain degree of knowledge about how perhaps other API-frameworks work. And I've read about every page of documentation that is to be found. (One example is the Core concepts page links to the deprecated Model hooks page.)

What I'd like to know:

  1. Is the idea of using .add() right to push data on a Model, and have loopback manage where it should store it, so when I query Node I'd get back all the history items, unless I prevent that on the server side (because I wouldn't want that every request for the Node data itself.
  2. Is it a good idea to create a separate document for each History item, with the nodeId in it, or better to make one histoy item per node, and store history inside them? Main issue for me is, How do I push data, without overwriting anything, and perhaps utilizing a timeStamp as a key?
  3. Does the History Model need to know about their relation to Nodes with BelongsTo or could it be oblivious to it and still have Nodes .add() each History item to it?
like image 821
TrySpace Avatar asked Jan 29 '16 09:01

TrySpace


1 Answers

If I understand your problem correctly, you could do instead Node hasMany History. Then use all the methods generated from the relation :

To create a new History using the relation

POST api/Node/{NodeId}/Histories/ 

You should be able to create multiple instances of History with a single POST request by writing the JSON data appropriately

{
    {
       ts: 26283829879
    },
    {
       ts: 5335329923
    }
}

To get a single histories from a node

GET api/Node/{NodeId}/Histories/{HistoryId}/

You can also get all histories from a node, edit any history of a given node, etc.

Is this helpful ?

See StrongLoop docs for HasMany relations.

like image 187
Overdrivr Avatar answered Sep 19 '22 00:09

Overdrivr