synchronous Everlive Query ?

8 posts, 1 answers
  1. Max
    Max avatar
    19 posts
    Member since:
    Jan 2015

    Posted 30 Jan 2015 Link to this post

    is there a way to do synchronous everlive requests using the Everlive SDK in cloudcode?

    as there is no library support in cloud code we are not able to create own promises neither use the REST API.

    promises would kinda work for me too as i need a function that several cloud codes have to call. i still would to have copy the same function into all, but at least the function would be separate of the actual requesting code.

    issuing an ajax request for the REST API  would enable synchronous, however it requires jQuery.
    (despite the problem of having to define API key and probably master key in cloud code which would also be bad i guess ...)

    synchronous request would there ease this for me, but i could not find any information about it

  2. Answer
    Anton Dobrev
    Admin
    Anton Dobrev avatar
    539 posts

    Posted 02 Feb 2015 Link to this post

    Hi Max,

    You can define a function which will be available in the current scope in the following way:
    function retrieveMyItems() {
        var el = Everlive.Sdk.$;
     
        var filter = {}; // user-defined filter here
        var places = el.data("Places");
     
        return places.get(filter);
    }

    Then use this function in the Cloud Code events (or in a Cloud function) like:

    function retrieveMyItems() {
        var el = Everlive.Sdk.$;
     
        var filter = {}; // user-defined filter here
        var places = el.data("Places");
     
        return places.get(filter);
    }
     
    Everlive.Events.beforeCreate(function(request, context, done) {
         retrieveMyItems().then(function(data) {
            var result = data.result;
            console.log(JSON.stringify(result));
            done();
        }, function(err) {
            console.log(JSON.stringify(err));
            done();
        });
    });

    I hope that this is helpful for your use case. You can add further functions and use the promises they return when resolving other promises.

    In addition, please take a look into the introduction article about who to use the JavaScript SDK in the Cloud Code (or Cloud Functions) here.

    Let me know if you have questions, I will be happy to help.

    Best regards,
    Anton Dobrev
    Telerik
     
    Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
     
  3. Anton Dobrev
    Admin
    Anton Dobrev avatar
    539 posts

    Posted 22 Jul 2015 Link to this post

    Hello Max,

    This is a quick announcement that you can now create your own promises in the Cloud Code for Data and Cloud Functions using the integrated RSVP library. In addition, the Underscore utility is also available for use within your custom code.

    More information is available in the documentation article here.

    Regards,
    Anton Dobrev
    Telerik
     
    Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
     
  4. Anthony
    Anthony avatar
    15 posts
    Member since:
    Apr 2014

    Posted 06 Oct 2015 in reply to Anton Dobrev Link to this post

    Hi Anton

    Could you perhaps provide an example on how to use the RSVP library with a cloud code asynchronous request?

    I am currently struggling to create a record for each item in a list, and catch any errors if they occur.
    After all record created successfully, I want to run another function.

    The below code wont work at all, but you will get an idea what I am trying to do.

    Everlive.CloudFunction.onRequest(function(request, response, done){
      var ​members= request.data.Members;
      var index;
            for (index = 0; index < members.length; ++index) {
              everliveApp.data('​Member').create({'Surname': members[index].Surname},{},
              function(data)​{
                  //when all records created successfully, do something else
                  runFunctionAfterAllSuccess();
                  done();}, 
              function(error){
                  done();
               });
    ).
    };

    });

  5. Anton Dobrev
    Admin
    Anton Dobrev avatar
    539 posts

    Posted 07 Oct 2015 Link to this post

    @Trevor

    Indeed, running an asynchronous code into a for loop in the cloud code layer is not advised. In the current case you can use the multiple create option in the JavaScript SDK and supply the members array to the create method.

    For instance:
    Everlive.CloudFunction.onRequest(function(request, response, done) {
      // ommitted for brevity
     
        for (index = 0; index < membersCount; ++index) {
            var currentMember = {
                'Surname': members[index].Surname
            };
            createMembersArray.push(currentMember);
        }
     
        everliveApp.data('​Member').create(createMembersArray).then(
            function(data)​ {
                //when all records created successfully, do something else
                runFunctionAfterAllSuccess();
                done();
            },
            function(error) {
                // error occurred during creating members
                done();
            });
    });

    If you need to make a lot of single item operations and await their result:
    Everlive.CloudFunction.onRequest(function(request, response, done){
      var rsvp = require('rsvp');
      var ​members= request.data.Members;
      var promises = [];
      for (var index = 0; index < members.length; ++index) {
        var createPromise = everliveApp.data('​Member').create({'Surname': members[index]);
        promises.push(createPromise);
      };
     
      rsvp.all(promises).then(function (res) {
        done();
      }, function (err) {
        done(err);
      })
    });

    Let me know if you have questions.

    Regards,
    Anton Dobrev
    Telerik
     
    Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
     
  6. Michael
    Michael avatar
    15 posts
    Member since:
    Jun 2015

    Posted 09 Oct 2015 in reply to Anton Dobrev Link to this post

    Anton Dobrev said:@Trevor
    rsvp.all(promises).then(function (res) {
        done();
      }, function (err) {
        done(err);
      })

    VERY HELPFULL

    thanks ;-)

  7. Anton Dobrev
    Admin
    Anton Dobrev avatar
    539 posts

    Posted 13 Oct 2015 Link to this post

    @Michael

    Glad that this example helped you.

    Let me know if you come up with some other useful scenarios or suggestions for such.

    Regards,
    Anton Dobrev
    Telerik
     
    Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
     
  8. Anthony
    Anthony avatar
    15 posts
    Member since:
    Apr 2014

    Posted 13 Oct 2015 Link to this post

    Thanks!! It helped me with my issue
Back to Top