Azure-Samples / azure-cosmos-java-sql-api-samples

Sample code for Azure Cosmos DB Java SDK for SQL API
MIT License
38 stars 70 forks source link

executeStoredProcedureArrayArg example does not work #16

Open deepub opened 3 years ago

deepub commented 3 years ago

I believe the SampleStoredProcedure.executeStoredProcedureArrayArg() method's objective is to demonstrate how to pass arrays as an argument to the stored procedure. However the sproc_args list isn't being inserted into the Cosmos container. Instead the javascript stored proc is simply creating a hard coded doc without accepting the array.

        sproc_args.add(new CustomPOJO("idA"));
        sproc_args.add(new CustomPOJO("idB"));
        sproc_args.add(new CustomPOJO("idC"));

        CosmosStoredProcedureResponse executeResponse = container.getScripts()
                .getStoredProcedure(sprocId+"ArrayArg")
                .execute(sproc_args, options);

The stored procedure ignores the input parameter array.

deepub commented 3 years ago

I was able to test stored procedure execution from the portal. The SP has an array parameter.

function createReplaceDocs(jsonArray) {

   var context = getContext();
   var container = context.getCollection();

   //validate if input is valid json
   if (typeof jsonArray === "string") {

      try {
         jsonArray = JSON.parse(jsonArray);
      } catch (e) {
         throw "Bad request, input to store procedure should be array of json string.";
      }
   } else {
      throw "Bad request, input to store procedure should be array of json string.";
   }

   var resultDocuments = [];

   jsonArray.forEach(function(jsonDoc) {

      if (jsonDoc.isUpdate != undefined && jsonDoc.isUpdate === true) {

         var accepted = container.replaceDocument(jsonDoc._self, jsonDoc, { etag: jsonDoc._etag },
             function (err, docReplaced) {
                if (err) throw new Error('Error' + err.message);
                resultDocuments.push(docReplaced);
             });

         if (!accepted) throw "Unable to update document, abort ";

      } else {

         var accepted = container.createDocument(container.getSelfLink(), jsonDoc,
             function (err, itemCreated) {
                if (err) throw new Error('Error' + err.message);
                resultDocuments.push(itemCreated);
             });

         if (!accepted) throw "Unable to create document, abort ";

      }

   });

   context.getResponse().setBody(resultDocuments)

}

The parameters passed in the portal include: PartitionKey = "001"/string Input parameter = [{"id": "A002", "storeNumber": "001", "docType": "Header"},{"id": "A003", "storeNumber": "001", "docType": "LineItem"},{"id": "A004", "storeNumber": "001", "docType": "LineItem"}]