Last time, we looked at using LoopBack while defining models from scratch. In the last part of this 5-part series, we demonstrate how to synchronize your data.

Now I have defined a LoopBack model, can LoopBack create or update the database schemas for me?

LoopBack provides two ways to synchronize model definitions with table schemas:

  • Auto-migrate: Automatically create or re-create the table schemas based on the model definitions. WARNING: An existing table will be dropped if its name matches the model name.

  • Auto-update: Automatically alter the table schemas based on the model definitions.

  • Auto-migration

    Let’s start with auto-migration of model definition. Here’s an example:

    var schema_v1 =
    {
      "name": "CustomerTest",
      "options": {
        "idInjection": false,
        "oracle": {
          "schema": "LOOPBACK",
          "table": "CUSTOMER_TEST"
        }
      },
      "properties": {
        "id": {
          "type": "String",
          "length": 20,
          "id": 1
        },
        "name": {
          "type": "String",
          "required": false,
          "length": 40
        },
        "email": {
          "type": "String",
          "required": false,
          "length": 40
        },
        "age": {
          "type": "Number",
          "required": false
        }
      }
    };
    

    Assuming the model doesn’t have a corresponding table in the Oracle database, you can create the corresponding schema objects to reflect the model definition:

    var ds = require('../data-sources/db')('oracle');
    var Customer = require('../models/customer');
    ds.createModel(schema_v1.name, schema_v1.properties, schema_v1.options);
    
    ds.automigrate(function () {
      ds.discoverModelProperties('CUSTOMER_TEST', function (err, props) {
        console.log(props);
      });
    });
    

    This creates the following objects in the Oracle database:

  • A table CUSTOMER_TEST.

  • A sequence CUSTOMER_TEST_ID_SEQUENCE for keeping sequential IDs.

  • A trigger CUSTOMER_ID_TRIGGER that sets values for the primary key.

  • Now we decide to make some changes to the model. Here is the second version:

    var schema_v2 =
    {
      "name": "CustomerTest",
      "options": {
        "idInjection": false,
        "oracle": {
          "schema": "LOOPBACK",
          "table": "CUSTOMER_TEST"
        }
      },
      "properties": {
        "id": {
          "type": "String",
          "length": 20,
          "id": 1
        },
        "email": {
          "type": "String",
          "required": false,
          "length": 60,
          "oracle": {
            "columnName": "EMAIL",
            "dataType": "VARCHAR",
            "dataLength": 60,
            "nullable": "Y"
          }
        },
        "firstName": {
          "type": "String",
          "required": false,
          "length": 40
        },
        "lastName": {
          "type": "String",
          "required": false,
          "length": 40
        }
      }
    }
    

    Auto-update

    If we run auto-migrate again, the table will be dropped and data will be lost. To avoid this problem use auto-update, as illustrated here:

    ds.createModel(schema_v2.name, schema_v2.properties, schema_v2.options);
    ds.autoupdate(schema_v2.name, function (err, result) {
      ds.discoverModelProperties('CUSTOMER_TEST', function (err, props) {
        console.log(props);
      });
    });
    

    Instead of dropping tables and recreating them, autoupdate calculates the difference between the LoopBack model and the database table definition and alters the table accordingly. This way, the column data will be kept as long as the property is not deleted from the model.

    Summary

    This series has walked through a few different use cases and how LoopBack handles each. Take a look at the table below for a round-up of what was covered.

    Recipe Use Case Model Strict Mode Database
    Open Model Taking care of free-form data false NoSQL
    Plain Model Defining a model to represent data true or false NoSQL or RDB
    Model from discovery Consuming existing data from RDB true RDB
    Model from introspection Consuming JSON data from NoSQL/REST false NoSQL
    Model synchronization Making sure models are in sync true RDB

    References

  • https://github.com/strongloop/loopback-sample-recipes

  • http://wiki.strongloop.com/display/DOC/LoopBack

  • http://wiki.strongloop.com/display/DOC/Oracle+connector

  • 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.