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:
.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.push
data, without overwriting anything, and perhaps utilizing a timeStamp
as a key?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?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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With