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.
.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...