How to PUT elements of collection into BaaS via Apigee proxy

Not applicable

Hi,

I am new to Apigee and I have what I presume is probably a newbie question, so please bear with me.

I have an incoming payload in a request body. The payload is a JSON array of elements that I want to PUT into a BaaS collection. I need to PUT each element, as the next time the payload is received, I want to overwrite the element if it is already there by "name" (businessKey in my sample code).

I started down the NodeJS route (see below) for my first go at it. However, I am facing a couple of issues and I have a couple of questions:

  1. I can’t simply have a NodeJS client to the BaaS can I? I need to start a node server. Is that correct? The below snippet doesn't seem to work as just a node target.
  2. Does NodeJS always need to be a target, as there is no policy for "Node"?
  3. If I use plain Javascript, is there a way to use JQuery?
  4. Is there another path I should be going down here?
var express = require('express');
var usergrid = require('usergrid');
var config = require('./config');
var dataClient = new usergrid.client({
  orgName: 'my-org',
  appName: 'MyAppName'
});
var input_data = JSON.parse(context.getVariable("request.content"));
for (var i = 0; i < input_data.length; i++) {
  var currentElement = input_data[i];
  currentElement["name"] = currentElement.businessKey;
  var options = {
  endpoint: "mycollection/" + currentElement.name,
  method: "PUT",
  body: currentElement
  }
  dataClient.request(options, function (error, result) {
// TODO
  if (error) {
  //error
  } else {
  //success
  }
  });
}

Any help/guidance would be greatly appreciated,

Simon

0 1 350
1 REPLY 1

Former Community Member
Not applicable

Hi @Simon Pink my answers are in order of your questions:

  1. Yes you have to start a node server. You can think of it as a programmable target. In that essence the proxy routes to that target back-end, hence it gets instantiated and is then listening for requests to arrive & then process them. Its not a policy which get applied to every request, but instead is a target to which control gets passed to eventually.
  2. Yes it has to be a target. For programmatic control at the policy level you should use the JavaScript policy
  3. Not really. JQuery is a client side framework, that typically runs inside a browser. In the case of the JavaScript policy, its executing server side javascript.
  4. You are already on the right path 🙂 Here is a link with some additional details.

Let us know if you have additional questions.