none
[Node.js][Mobile Apps] Easy Tables RRS feed

  • Question

  • Hi, please, when I create Easy Table (e.g. named usertable) and I select "Edit script", Visual Studio online is opened and I can edit usertable.js file. this file looks like:


    var table = module.exports = require('azure-mobile-apps').table();
    // table.read(function (context) {
    //     return context.execute();
    // });
    // table.read.use(customMiddleware, table.operation);

    when I want to do some stuff in insert function, where I should put that stuff, directly in usertable.js file? I'm confused as the keyword insert is underlined by green wave noting that local function is unused. Is there any chance to test these scripts without calling some code from the client side?

    function insert(item, user, request) {
        if (item.text.length > 10) {
            request.respond(statusCodes.BAD_REQUEST, 
                'Text length must be less than 10 characters');
        } else {
            request.execute();
        }
    }

    and one a little bit offtopic question, can I do something like relations through Easy Tables as I can do in traditional SQL approach? If yes, where does this logic sit? In scripts like above - insert, update, etc.?

    many thanks!



    Monday, May 30, 2016 8:20 AM

Answers

  • Hi Pavel,

    The syntax has changed from Mobile Services. You can find an example of the new syntax in samples (e.g. https://github.com/Azure/azure-mobile-apps-node/blob/master/samples/push-on-insert/tables/TodoItem.js) and other various pieces of  documentation (e.g. https://shellmonger.com/2016/04/18/30-days-of-zumo-v2-azure-mobile-apps-day-9-table-controller-operations/).

    Essentially, your code would look more like:

    table.insert(function (context) {
      if(context.item.text.length > 10) {
        contex.req.status(400).send("Text length must be less than 10 characters");
      } else {
        context.execute();
      }
    });

    Each of the table operations (read, insert, update, delete, undelete) pass a single parameter to the function - context. You can find a reference for the object at http://azure.github.io/azure-mobile-apps-node/global.html#context.

    Hope this helps!

    Dale 

    • Proposed as answer by Swikruti Bose Saturday, June 4, 2016 2:54 PM
    • Marked as answer by Pavel Sedina Saturday, June 4, 2016 10:42 PM
    Tuesday, May 31, 2016 9:35 PM
  • To test the scripts in Easy Tables, you can leverage the Rest APIs of Table Operations of Mobile Apps. You can use the HTTP requests tools like Postman to make the Rest APIs calls for the tables directly, to manage the data and also test the custom scripts in Easy Tables. Refer to https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-node-backend-how-to-use-server-sdk/#TableOperations for details.

    And it is possible to query SQL stmt in Easy Tables, you can refer to the answer on SO for the similar requirement with you.

    Any further concern, please feel free to let me know.

    • Marked as answer by Pavel Sedina Saturday, June 4, 2016 10:42 PM
    Tuesday, May 31, 2016 5:40 AM
    Moderator

All replies

  • To test the scripts in Easy Tables, you can leverage the Rest APIs of Table Operations of Mobile Apps. You can use the HTTP requests tools like Postman to make the Rest APIs calls for the tables directly, to manage the data and also test the custom scripts in Easy Tables. Refer to https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-node-backend-how-to-use-server-sdk/#TableOperations for details.

    And it is possible to query SQL stmt in Easy Tables, you can refer to the answer on SO for the similar requirement with you.

    Any further concern, please feel free to let me know.

    • Marked as answer by Pavel Sedina Saturday, June 4, 2016 10:42 PM
    Tuesday, May 31, 2016 5:40 AM
    Moderator
  • Hi Pavel,

    The syntax has changed from Mobile Services. You can find an example of the new syntax in samples (e.g. https://github.com/Azure/azure-mobile-apps-node/blob/master/samples/push-on-insert/tables/TodoItem.js) and other various pieces of  documentation (e.g. https://shellmonger.com/2016/04/18/30-days-of-zumo-v2-azure-mobile-apps-day-9-table-controller-operations/).

    Essentially, your code would look more like:

    table.insert(function (context) {
      if(context.item.text.length > 10) {
        contex.req.status(400).send("Text length must be less than 10 characters");
      } else {
        context.execute();
      }
    });

    Each of the table operations (read, insert, update, delete, undelete) pass a single parameter to the function - context. You can find a reference for the object at http://azure.github.io/azure-mobile-apps-node/global.html#context.

    Hope this helps!

    Dale 

    • Proposed as answer by Swikruti Bose Saturday, June 4, 2016 2:54 PM
    • Marked as answer by Pavel Sedina Saturday, June 4, 2016 10:42 PM
    Tuesday, May 31, 2016 9:35 PM
  • thanks a lot!
    Saturday, June 4, 2016 10:42 PM