Appcelerator Blog

The Leading Resource for All Things Mobile

ArrowDB Mock Data and Data Import

16 Flares 16 Flares ×

So you’ve created your Arrow Builder project and created ArrowDB models. Arrow has automatically created all your APIs and even documented them for you. Maybe this is a mock API or maybe it’s your production API. Now, how do you get data into your Arrow Database?

This blog post describes a method for populating your ArrowDB database. (Currently, Arrow does not have an import/export feature but it is on the roadmap.) Note, that the method described here could also be used for other backend data sources as well, but a common use case is to create a mock API in Arrow and populate it with mock data. This enables mobile developers to create their mobile app, make API calls and work with mock data immediately while the production APIs are created.

The basic concept is to create a Arrow custom API that we can call. The (mock) data will be included as a resource in the custom API. When the API is called, it will populate the Arrow Database with the desired data set.

Prepare The Data

Prepare your data so that it is in the form of an array of JSON objects.

For example, assume that my ArrowDB model has three fields: Name, Type, and Phone as follows:


var Arrow = require('arrow');
var Model = Arrow.createModel('account', {
	fields: {
		name: {
			type: String
		},
		type: {
			type: String
		},
		phone: {
			type: String
		}
	},
	connector: 'appc.arrowdb',
	actions: [
		'create',
		'read',
		'update',
		'delete',
		'deleteAll'
	]
});
module.exports = Model;

So, the data should be of the form of:


[{"Name":"Jabberbean","Type":"Prospect","Phone":"62-(392)370-9245"},
{"Name":"Feedmix","Type":"Prospect","Phone":"54-(426)656-8984"},
...
...
...
{"Name":"Yadel","Type":"Prospect","Phone":"86-(818)912-0705"}]

If your data is in a CSV file, then convert it to JSON using CSV2JSON or something similar.

In this post, I used mockeroo to create the mock data set above. See the following screen shots that illustrate how to do that, but it is fairly straightforward.

mockdata-1 mockdata-2 mockdata-3 mockdata-4 mockdata-5

When you are done creating the data, download it to your hard disk.

Create An Arrow Custom API

I created the following Custom API, populateData.js,in the /api folder:


var Arrow = require('arrow');
var populateData = Arrow.API.extend({
	group: 'MockData',
	path: '/api/populateData',
	method: 'GET',
	description: 'this is an api that populates a model with mock data',
	action: function (req, resp, next) {
		var Model = Arrow.getModel("account");
		data.forEach(function(item) {
			Model.create(item, function(err, instance) {
		    if (err) {
		        console.log('Account create error');
		    }
			});
		});
		next();
	}
});
module.exports = populateData;

var data = [{"Name":"Jabberbean","Type":"Prospect","Phone":"62-(392)370-9245"},
{"Name":"Feedmix","Type":"Prospect","Phone":"54-(426)656-8984"},
.
.
.
{"Name":"Yadel","Type":"Prospect","Phone":"86-(818)912-0705"}];

In the above custom API, populateData, I appended the JSON array of mock data (downloaded from mockeroo) and assigned it to a variable called data.

When the API is called, the API programmatically gets a handle to the account model using:


var Model = Arrow.getModel("account");

Then I use lodash .forEach to loop through the JSON array of mock data and for each object, I create a new ArrowDB record in the account model using:


Model.create(item, function(err, instance) {
	if (err) {
			console.log('Account create error');
	}
});

Note that lodash is already required into your project’s package.json automatically so you don’t need to.

Test Your APIs

Before your run the populateData API, first run the account FindAll API and see that there is no data.

mockdata-6

Now run the populateData API.

mockdata-7

And check the account FindAll again.

mockdata-8

You can see all of the mock data in the Data tab.

mockdata-9

Alternate Method

If you don’t want to embed your data in your custom api file or if you will have lots of data, you may to consider the following custom api which requires in the data from an external file, MOCK_DATA.js which resides in the root of the project folder.


var Arrow = require('arrow');
var Mockdata = require('MOCK_DATA');
var populateData = Arrow.API.extend({
	group: 'MockData',
	path: '/api/populateData',
	method: 'GET',
	description: 'this is an api that populates a model with mock data from MOCK_DATA.js file',
	action: function (req, resp, next) {
		var Model = Arrow.getModel("account");
		Mockdata.data.forEach(function(item) {
			Model.create(item, function(err, instance) {
		    if (err) {
		        console.log('Account create error');
		    }
			});
		});
		next();
	}
});
module.exports = populateData;

and the data in the file MOCK_DATA.js:


module.exports = {
  data: [
    {"Name":"Demivee","Type":"Prospect","Phone":"1-(818)173-8443"},
    {"Name":"Shufflester","Type":"Prospect","Phone":"93-(232)869-5557"},
    {"Name":"Voonte","Type":"Customer","Phone":"62-(269)473-1186"},
    ...
    {"Name":"Katz","Type":"Prospect","Phone":"86-(276)527-0690"}
  ]
}

Summary

In this blog post we created a means of populating your ArrowDB with data using a custom API in your Arrow project. The custom API actually has a small amount of code and it should be very easy to modify it for your needs.

16 Flares Twitter 0 Facebook 0 Google+ 1 LinkedIn 15 Email -- 16 Flares ×

2 Comments

  1. Bert

    Great tutorial Leor. Providing mock endpoints is super useful to test against.

    I wonder though could you not have pulled the data directly from Mockaroo via API :)

    • Leor Brenman

      Thx Bert. I looked into pulling mock data using Mockaroo’s API and that’s definitely viable. I think the reasoning behind not using that method is to make sure your mock data is consistent (if that’s a requirement).

Comments are closed.

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.
16 Flares Twitter 0 Facebook 0 Google+ 1 LinkedIn 15 Email -- 16 Flares ×