Rich mobile applications are driven by data. Data can be produced and consumed by mobile devices, browsers, cloud services, legacy applications, databases, and other backend systems.

LoopBack mobilizes data through models that represent business data and behavior. LoopBack exposes models to mobile apps through REST APIs and client SDKs. You need to interact with the model differently, depending on the location and type of data. In this 5-part blog series, I’ll explain some of the most important recipes for working with LoopBack models:

  • Part 1: Open models – for free-form data.

  • Part 2: Models with schema definitions such as relational databases.

  • Part 3: Model discovery with relational databases – consuming existing data from a relational database.

  • Part 4: Models by instance introspection – Consuming JSON data from NoSQL databases or REST APIs.

  • Part 5: Model synchronization with relational databases – keeping your model synchronized with the database.

  • The source code is available at https://github.com/strongloop/loopback-sample-recipes.

    Let’s start with the simplest one: open models.

    I’m mobile developer. Can LoopBack help me save and load data transparently? I don’t need to worry about the backend or define the model up front, because my data are free-form.

    For free-form data, use an open model that allows you to set any properties on model instances.

    The following code creates an open model and exposes it as a REST API:

    ```js var loopback = require('loopback'); var app = loopback(); // Create an instance of LoopBack // Create an in memory data source var ds = loopback.createDataSource('memory'); // Create a open model that doesn't require predefined properties var FormModel = ds.createModel('form'); // Expose the model as REST APIs app.model(FormModel); app.use(loopback.rest()); // Listen on HTTP requests app.listen(3000, function () { console.log('The form application is ready at http://127.0.0.1:3000'); }); ```js

    Notice the call to ds.createModel() with only a name to create an open model.

    To try it out, enter the following command:

    curl -X POST -H "Content-Type:application/json" -d '{"a": 1, "b": "B"}' \
    http://127.0.0.1:3000/forms
    

    This command POSTs some simple JSON data to the LoopBack /forms URI.

    The output that the app returns is a JSON object for the newly-created instance.

    {
    "id": "52389f5f7d365dd52a000005",
    "a": 1,
    "b": "B"
    }
    

    The id field is a unique identifier you can use to retrieve the instance:

    curl -X GET http://127.0.0.1:3000/forms/52389f5f7d365dd52a000005
    

    Note: Your ID will be different as it is generated by the database. Please copy it from the POST response.

    Try submitting a different form:

    curl -X POST -H "Content-Type:application/json" -d '{"a": "A", "c": "C", "d": true}' http://localhost:3000/forms
    

    Now you see the newly created instance as follows:

    {
    "id": "5238c1e492f7b69535000001",
    "a": "A",
    "c": "C",
    "d": true
    }
    

    For the complete list of REST APIs that LoopBack scaffolds for a model, please read:

    http://wiki.strongloop.com/display/DOC/REST+API

    If you have StrongLoop Suite (http://strongloop.com/strongloop-suite/downloads/)  installed, you can build and run this project from scratch in 5 commands:

    $ slc lb project free-form
    $ cd free-form
    $ slc lb model form
    $ slc install
    $ slc run app
    

    Now open a browser and point it to http://localhost:3000/explorer.

    You get an explorer to try the forms APIs right away. The UI is pretty straghtforward, feel free to play with it. For more information, check out:

    http://wiki.strongloop.com/display/DOC/LoopBack+API

    The open model is simple and flexible. It works well for free-form style data because the model doesn’t constrain the properties and their types. But for other scenarios, a predefined model is preferred to validate the data and ensure it can be exchanged among multiple systems.

    Next week, we’ll talk about models with schema definitions.

    What’s next?

  • What’s in the upcoming Node v0.12 release? Six new features, plus new and breaking APIs.
  • Ready to develop APIs in Node.js and get them connected to your data? Check out the Node.js LoopBack framework. We’ve made it easy to get started either locally or on your favorite cloud, with a simple npm install.
  • Need <a href="http://strongloop.com/node-js-support/expertise/"]]>training and certification</a> for Node? Learn more about both the private and open options StrongLoop offers.