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:
- 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 thehistory
items, unless I prevent that on the server side (because I wouldn't want that every request for the Node data itself. - 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 atimeStamp
as a key? - Does the
History
Model need to know about their relation toNodes
withBelongsTo
or could it be oblivious to it and still haveNodes
.add()
each History item to it?
.add
multiple historyItems at once. – Mariannamarianne