Appcelerator Blog

The Leading Resource for All Things Mobile

Arrow – Connecting to Multiple Instances of the Same Back-End Data Source Type

12 Flares 12 Flares ×

Arrow makes it extremely easy to connect to various back-end data sources through Appcelerator provided connectors, such as REST, SOAP, Salesforce, MySQL and MS SQL and expose mobile-optimized REST APIs. A complete list of connectors can be found here. One can also build their own connector following the instructions here.

Simply install a connector into your Arrow project, edit the connector configuration file and you’re ready to go.

But, what if you want to connect to multiple instances of the same data source type? For example, what if you have two MySQL databases that you want to create mobile-optimized web services for? Well, Arrow makes that easy too.

Install the Base Connector

For example, the following command will install a MySQL connector in your project:

appc install connector/appc.mysql

Edit the Connector Configuration File

Open the conifguration file in the project’s conf folder. In this example, the configuration file is appc.mysql.default.js:

module.exports = {
    connectors: {
        'appc.mysql': {
            connectionPooling: true,
            connectionLimit: 10,

		host: 'localhost',
        port: 3306,
        database: 'test',
        user: 'root',
        password: '',

        // Create models based on your schema that can be used in your API.
        generateModelsFromSchema: true,

        // Whether or not to generate APIs based on the methods in generated models.
        modelAutogen: true

    }
}

};

Modify the Configuration File

We are going to make three major edits:

  1. Create two connector entries in the connectors property
  2. Add a ‘appc.mysql’ connector reference in each entry
  3. Configure each one to point to the correct MySQL instance
module.exports = {
    connectors: {
        'appc.mysql1': {
            connector: 'appc.mysql',
            connectionPooling: true,
            connectionLimit: 10,

		database: 'salesreport',
        user: 'root',
        password: 'XXXX',
        host: 'localhost',
        port: 3306,

        // Create models based on your schema that can be used in your API.
        generateModelsFromSchema: true,

        // Whether or not to generate APIs based on the methods in generated models.
        modelAutogen: true

    },
    'appc.mysql2': {
        connector: 'appc.mysql',
        connectionPooling: true,
        connectionLimit: 10,

        database: 'lbmysql',
        user: 'lbrenman',
        password: 'XXXX',
        host: 'db4free.net',
        port: 3306,

        // Create models based on your schema that can be used in your API.
        generateModelsFromSchema: true,

        // Whether or not to generate APIs based on the methods in generated models.
        modelAutogen: true

    }
}

};

Notice that I have both generateModelsFromSchema and modelAutogen set to true so that Arrow will automatically expose each databases tables and views as APIs.

Now you can access data from both MySQL databases using the appc.mysql1 and appc.mysql2 connectors. It’s that easy.

For example, the following model:

var Arrow = require("arrow");

var Model = Arrow.createModel("combined",{
    "fields": {
        "row": {
            "model": "appc.mysql1/region",
            "type": "Object"
        },
        "us": {
            "model": "appc.mysql2/region",
            "type": "Object"
        }
    },
    "connector": "appc.composite",
    "actions": [
        "create",
        "read",
        "update",
        "delete",
        "deleteAll"
    ],
    "singular": "combined",
    "plural": "combineds"
});

module.exports = Model;

returns the following data for a findAll:

{
  "success": true,
  "request-id": "6d9481d2-3d04-4afd-b49e-494d3651d8c2",
  "key": "combined",
  "combined": {
    "row": [
      {
        "id": 0,
        "rid": 0,
        "region": "LATAM",
        "act": 166,
        "fcst": 177
      },
      {
        "id": 1,
        "rid": 1,
        "region": "NORDIC",
        "act": 60,
        "fcst": 62
      },
      {
        "id": 2,
        "rid": 2,
        "region": "APAC",
        "act": 337,
        "fcst": 427
      },
      {
        "id": 3,
        "rid": 3,
        "region": "EMEA",
        "act": 552,
        "fcst": 661
      }
    ],
    "us": [
      {
        "id": 0,
        "rid": 0,
        "region": "NE",
        "act": 61,
        "fcst": 99
      },
      {
        "id": 1,
        "rid": 1,
        "region": "SE",
        "act": 30,
        "fcst": 74
      },
      {
        "id": 2,
        "rid": 2,
        "region": "West",
        "act": 32,
        "fcst": 46
      },
      {
        "id": 3,
        "rid": 3,
        "region": "Central",
        "act": 8,
        "fcst": 11
      },
      {
        "id": 4,
        "rid": 4,
        "region": "Canada",
        "act": 20,
        "fcst": 44
      }
    ]
  }
}

for the following two MySQL Database tables:

appc.mysql1/region appc.mysql1/region

appc.mysql2/region appc.mysql2/region

12 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 12 Email -- 12 Flares ×

Sign up for updates!

Become a mobile leader. Take the first step to scale mobile innovation throughout your enterprise.
Get in touch
computer and tablet showing Appcelerator software
Start free, grow from there.
12 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 12 Email -- 12 Flares ×