Last time, we looked at how you can mobilize data through LoopBack with open models, which works well for free-form style data.  This time around, we are looking at models with schema definitions.

I want to build a mobile application that will interact with some backend data. I would love to see a working REST API and mobile SDK before I implement the server side logic.

In this case, we’ll define a model first and use an in-memory data source to mock up the data access. You’ll get a fully-fledged REST API without writing a lot of server side code.

var loopback = require('loopback');

var ds = loopback.createDataSource('memory');

var Customer = ds.createModel('customer', {
    id: {type: Number, id: true},
    name: String,
    emails: [String],
    age: Number},
    {strict: true});

The snippet above creates a ‘Customer’ model with a numeric id, a string name, an array of string emails, and a numeric age. Please also note we set the ‘strict’ option to be true for the settings object so that LoopBack will enforce the schema and ignore unknown ones.

For more information about the syntax and apis to define a data model, check out:

http://docs.strongloop.com/loopback-datasource-juggler/#loopback-definition-language-guide

You can now test the CRUD operations on the server side. The following code creates two customers, finds a customer by ID, and then finds customers by name to return up to three customer records.

// Create two instances
Customer.create({
    name: 'John1',
    emails: ['john@x.com', 'jhon@y.com'],
    age: 30
    }, function (err, customer1) {
        console.log('Customer 1: ', customer1.toObject());
        Customer.create({
            name: 'John2',
            emails: ['john@x.com', 'jhon@y.com'],
            age: 30
        }, function (err, customer2) {
            console.log('Customer 2: ', customer2.toObject());
            Customer.findById(customer2.id, function(err, customer3) {
                console.log(customer3.toObject());
            });
            Customer.find({where: {name: 'John1'}, limit: 3}, function(err, customers) {
                customers.forEach(function(c) {
                console.log(c.toObject());
            });
        });
    });
});

To expose the model as a REST API, use the following:

var app = loopback();
app.model(Customer);
app.use(loopback.rest());
app.listen(3000, function() {
    console.log('The form application is ready at http://127.0.0.1:3000');
});

Until now the data access has been backed by an in-memory store. To make your data persistent, simply replace it with a MongoDB database by changing the data source configuration:

var ds = loopback.createDataSource('mongodb', {
    "host": "demo.strongloop.com",
    "database": "demo",
    "username": "demo", 
    "password": "L00pBack",
    "port": 27017
});

For more information about data sources and connectors, please check out:

http://docs.strongloop.com/loopback-datasource-juggler/#loopback-datasource-and-connector-guide

When defining a model, it may be troublesome to define all the properties from scratch. Fortunately, LoopBack can discover a model definition from existing systems such as relational databases or JSON documents, as we’ll show in part 3 next week.

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 training and certification for Node? Learn more about both the private and open options StrongLoop offers.