Strongloop EmbedsMany helper methods not found
Asked Answered
M

1

6

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?
Mariannamarianne answered 29/1, 2016 at 9:40 Comment(0)
H
1

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.

Hua answered 10/2, 2016 at 11:33 Comment(4)
I need to be able to .add multiple historyItems at once.Mariannamarianne
There is nothing preventing you to do so. I believe this natively supported by Persisted model. See apidocs.strongloop.com/loopback/#persistedmodel-create Optional data argument. Can be either a single model instance or an array of instances. I have edited my post, see if that solves your problem.Hua
Problem with that is, that I don't want to create the Node, it's already there, so I have to use it in findById and then add something...Mariannamarianne
POST api/Node/{NodeId}/Histories/ does not create a Node, just creates instances of History and initialize their relations to the node with id NodeId. Is this what you are looking for ?Hua

© 2022 - 2024 — McMap. All rights reserved.