0

How would I go about making a custom remoteMethod that updates/pushes, not overrides, a property that is an array. So basically push data to an array property of a model.

I can't find any clear examples of this in the documentation, except for an .add() helper method, but that requires an embedsOne or some other relation.

But what if I have a single Model in my whole app and would just want to push some data to an id. So ending up with an endpoint like:

POST /Thing/{id}/pushData

And the body to POST would be:

  {
     id: "foo",
     data: "bar"
   }

(Or preferably without id, and have the id autoInserted, since it's an array, and I don't need an id for each item, the data part should be searchable with filters/where)

So far I have:

  Thing.remoteMethod (
      'pushData',
      {
        isStatic: false,
        http: {path: '/pushData', verb: 'post'},
        accepts: [
          { arg: 'data', type: 'array', http: { source: 'body' } }
        ],
        returns: {arg: 'put', type: 'string'},
        description: 'push some Data'
      }
  );

  Thing.prototype.pushData = function(data, cb) { 
    data.forEach(function (result) {
      // ??
    });
    cb(null, data)
  };

And as far as I can see, the default endpoints only allow single instances to be added, but I want to update in bulk.

6
  • By "updates/pushes", do you mean you want to use the array as a queue and use the rest api to put and pop data from it ? Something like that right ? (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…) Commented Feb 8, 2016 at 9:18
  • @Overdrivr, thing is, I don't want' to have to overwrite the whole array, because that would probably be very slow when it comes to thousands of requests. I'd like to use some native loopback functionality like .add(), although I have no idea how loopback does this internally, but I'm assuming it's more efficient than, overwriting a thousand-length array... Commented Feb 8, 2016 at 9:32
  • Have you benchmarked it ? You need to make sure you really have a problem regarding performances otherwise you will spend time trying to fix things that aren't broken in the first place. Commented Feb 8, 2016 at 10:11
  • However, if I understand correctly, you want to add a thousand items to your array in one request rather than making a thousand request that add 1 item. Is this correct ? Commented Feb 8, 2016 at 10:12
  • @Overdrivr, yes exactly Commented Feb 11, 2016 at 7:58

1 Answer 1

1

You have made your method non-static, which is good.

Now, if your array property is called MyArray, I would try something along the lines of :

  Thing.remoteMethod (
      'pushData',
      {
        isStatic: false,
        http: {path: '/pushData', verb: 'post'},
        accepts: [
          { arg: 'data', type: 'array', http: { source: 'body' } }
        ],
        returns: {arg: 'put', type: 'string'},
        description: 'push some Data'
      }
  );

  Thing.prototype.pushData = function(data, cb) {
    thingInstance = this; 
    data.forEach(function (result) {
       thingInstance.MyArray.push(result.data);
    });

    cb(null, data)
  };

Since your remote method is non-static, you should able to access the instance using this. I have a doubt whether or not you can directly access properties that way by writing this.someProperty, please try it and let me know if that doesn't work.

Then to create in bulk just make a standard POST request to your remote

POST /Thing/{id}/pushData

just write your JSON like this

{
   {
       data: "bar"
   },
   {
       data: "foo"
   },
   //etc
}

This should add two elements to the array property.

Let me know if this is helpful

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.