Migrating from MySQL to MongoDB with the help of Parse – [Part 2]

Migrating from MySQL to MongoDB with the help of Parse – [Part 2]

On the first part of this guide, I gave you an introduction about data types in MongoDB that can be used to keep MySQL equivalent values.

I also showed you the Classes we are going to create in order to store that data.

Today is “hands-on” day, so get your cup of coffee, fasten your seat belts and let’s get it started!

Graphically or Programatically?

Parse supports both ways of creating your Parse.Objects (a.k.a. Classes) and their properties, so you can choose whatever flavor you like best.

Creating Parse.Objects graphically have the advantage of letting you see in real-time the result of your creation, double-check for data types while you are creating them and is a very, very user-friendly way to work.
screen-shot-2019-08-08-at-09-41-20

Creating a new Class graphically

screen-shot-2019-08-08-at-09-41-36

Adding a property graphically

Creating them programmatically has the advantage of letting Parse lazily generate everything for you, choosing the data type it interprets as the best fit and allowing you to create Parse.Objects from your code, which can be very useful if you have to create your data structure in runtime.

let Product = Parse.Object.extend("Product");
let product = new Product();
product.set ("price", 100);

Creating the same Class programmatically

So, as you can see, both ways are very simple. Choose the way that best fits your likings and your project’s needs.

For this tutorial, I am going to create a few classes graphically and others programmatically, so you can see both ways work.

 

Our first Class: Size

Let’s create our first Class and populate it.

I chose the Size class to be created graphically because it is a very small class (only one property) and, as you saw in the first part of the article, in MySQL this is an ENUM type, which is not supported on MongoDB, and as it only has a few objects, it will be easy to add them manually.

We will start by clicking the “Create a new class” button on our Database Browser:

screen-shot-2019-08-08-at-10-07-00

On the popup we will choose Custom, as this is will be a custom class. This means we will manually set all of its properties.

Let’s name this class Size (capital S because it is a class) and hit the “Create class” button:

screen-shot-2019-08-08-at-10-07-30

Now it is time to create our properties. Just one in this case.

Hit that “Add a new column” button on the top right:

screen-shot-2019-08-08-at-10-07-43

Now, on the popup choose the data type as String, as we will be storing string values such as “small”, “medium” and “large”, and give your property a good lowercase name (properties should use lowercase as standard):  “size” and hit that “Add column” button.

screen-shot-2019-08-08-at-10-07-55

Now, as we will only have a few values, let’s add them manually. Later on, I’ll show you how to add values programmatically to other classes.

Click that “Add a row” button:

screen-shot-2019-08-08-at-10-08-02

 

And a new row should show up. Double click the cell on the size column and you will be able to type your size: “small”.
Hit enter or click outside of the cell to save:

screen-shot-2019-08-08-at-10-08-24

In order to add the other values, click the “+” button on the left side under the objects:

screen-shot-2019-08-08-at-10-08-44

Now, repeat these steps until you have all the sizes that you want in your database. In my case I created only 3 sizes, but you can add as many as you need:

screen-shot-2019-08-08-at-10-08-59

 

Now, how about some code?

Now that you learned how to create a new class graphically, let’s start creating some code. I’ll be writing this code in NodeJS, but you can choose any programming language that you want, as long as it is supported by the Parse platform.

Currently, we have SDKs for the following programming languages: iOS (native), OSX, tvOS, Android (Java), JavaScript, .Net (includes Xamarin), Unity, PHP, Arduino (C++), Embedded C.
We also support direct REST API calls in case your programming language does not have a specific SDK, so that should be compatible with most programming languages.

If you are not familiar with NodeJS, we have a very good tutorial about how to set it up here.

Create an index.js file that we will be adding code to.

We will be coding the Breeder class and adding some objects to it, so first let’s tell Parse we want to create a class with that name by extending it. Add this to your index.js file:

const Breeder = Parse.Object.extend("Breeder");

This tells Parse that we have a Javascript class called Breeder (capital B as it is a class) that extends the Parse.Object with the same name (“Breeder” in quotes).

Now let’s add a new Breeder object programmatically, but manually setting its values instead of migrating them (we will see that later on):

let breeder = new Breeder();

This is the object we will be saving, so let’s set a few properties to it:

breeder.set ("name", "Alex");
breeder.set ("age", 39);
breeder.set ("address", "St. Monica Street, 1234");
breeder.set ("state", "CA");
breeder.set ("city", "Sunny Vale");
breeder.set ("phone", "555-1234");
breeder.set ("email", "[email protected]");

Note that I just tell Parse the name of the properties I want to save and its values, and Parse will lazily create the class and the properties (with the correct data types) for me when we first save it:

breeder.save()

Let’s create another one:

let anotherBreeder = new Breeder();
anotherBreeder.set ("name", "Alysson");
anotherBreeder.set ("age", 40);
anotherBreeder.set ("address", "Oak Avenue, 456");
anotherBreeder.set ("state", "CA");
anotherBreeder.set ("city", "Mountain View");
anotherBreeder.set ("phone", "555-6789");
anotherBreeder.set ("email", "[email protected]");
anotherBreeder.save();

So, the full code (including instantiating the Parse Framework, setting the AppId and Javascript Keys, and saving the objects) would be:

// Instantiate the Parse framework
const Parse = require('parse/node')

// Initialize it with the AppId and Javascript Keys, and set the URL to Back4app's
Parse.initialize('MyAppIdGoesHere', 'MyJavascriptKeyGoesHere')
Parse.serverURL = 'https://parseapi.back4app.com'

// Extend the Breeder class
const Breeder = Parse.Object.extend("Breeder");

// Instantiate one breeder
let breeder = new Breeder();

// Set all its properties
breeder.set ("name", "Alysson"); // String
breeder.set ("age", 39); // Number
breeder.set ("address", "Oak Avenue, 456"); // String
breeder.set ("state", "CA"); // String
breeder.set ("city", "Mountain View"); // String
breeder.set ("phone", "555-6789"); // String
breeder.set ("email", "[email protected]"); // String

// Save it
breeder.save();

// Instantiate another breeder
let anotherBreeder = new Breeder();

// Set its properties too
anotherBreeder.set ("name", "Alysson");
anotherBreeder.set ("age", 40);
anotherBreeder.set ("address", "Oak Avenue, 456");
anotherBreeder.set ("state", "CA");
anotherBreeder.set ("city", "Mountain View");
anotherBreeder.set ("phone", "555-6789");
anotherBreeder.set ("email", "[email protected]");

// Save it
anotherBreeder.save();

Now it is time to…

 

Save and Run 🤩

Save that index.js file and run it by typing this in your console:

node index.js

You should see the new records in your App’s Dashboard:

screen-shot-2019-08-08-at-11-54-08

Now, ain’t that easy peasy?

 

Conclusion

Now that we saw how to create our Classes and Properties both graphically and programmatically, we are ready to move on.

On the next post, I will show you how to read the data from MySQL and write it to MongoDB using the fundaments that we learned today.

Spoiler alert! It will get VERY technical. And you will love it!

Stay tuned!


Leave a reply

Your email address will not be published.