Attachments Strongloop EmbedsMany not found

Some questions that I can not find the answer in the documentation.

I am trying to create a structure like this:

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

So, each separate node has many elements of history. And I want to be able to push add new data without overwriting anything.

Now I do not want to store everything under each node, but rather in a separate document, so I think embededsMany is 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 the story will be simple:

 { "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 if part of foreignKey is right, but I tried many different combinations, and that seems logical.

The History model is not publicly available, so it cannot be displayed as an endpoint. And I want to use the relationship as much as possible, and not have a separate endpoint.

The main problem is here: I would like to use Nodes.history.add() as described here .

But I tried all different methods from Remote methods to Operation Hooks , but I can not find the mentioned helper methods. There is no sample code here.

In part, I think this is due to the fact that the documentation is sometimes not very clear or requires a certain degree of knowledge about how other API structures work. And I read about every page of documentation that needs to be found. (For example, the Core concepts page links to the obsolete Model hooks page.)

What I would like to know:

  • The idea of ​​using .add() right to pass data to the Model, and have a loopback where it should be stored, so when I request Node, I would return all history elements if I prevented this on the server side (because I do not want every request for Node data itself.
  • Is it a good idea to create a separate document for each History element, with a nodeId in it, or is it better to make one histoy element per node and save the history inside them? The main problem for me is: "How do I push data without overwriting anything, and possibly using timeStamp as a key?
  • Should the history model know about their relationship to Nodes with BelongsTo or can it be ignored and still have Nodes .add() every element of the story?
+6
source share
1 answer

If I understand your problem correctly, you can do Node hasMany History instead. Then use all methods created from the relationship:

To create a new story using a relationship

 POST api/Node/{NodeId}/Histories/ 

You should be able to create multiple instances of History with a single POST request, correctly writing JSON data

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

To get one story from node

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

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

This is useful?

See StrongLoop docs for HasMany relationships .

+1
source

All Articles