Workshop: Databases, PHP and Node JS

We had a workshop with Kyle in which we used Propel ORM to create database of users for an web app that emulates twitter. This app used php to create a database of users and store their data. This could then be viewed on phpMyAdmin

phpmyadmin

The phpMyAdmin interface

phpmyadmin-tables

Creating a database with four fields, user id (which is unique and increments automatically), name, age and gender of the user.

phpmyadmin-users

Viewing the users in the database. There are three users in this one.

My project is also going to use databases to store users and the amount of steps they have earned. Php is quite a handy and standard way to handle database structures. However as mentioned before, I intend to use Node.js to control the Arduino in my project. Therefore I decided to look at ways databases are handled using Node.js.

NoSQLers came to share how they had overthrown the tyranny of slow, expensive relational databases in favor of more efficient and cheaper ways of managing data.

-(Strauch, 2015)

One way databases are handled using Node.js is by using a No SQL database, instead of using a relational database structure like the Propel one I used earlier. The advantage of using this approach means that the database is more scalable, fields can be added to it easily, runs faster and it is also easier to reference the data stored in that database.

A popular way to do this is to use a MongoDB database which stores the database in JSON files which can be interacted with using Javascript. I followed this tutorial on how to get started with Mongoose.js, which is a Node package which allows interaction with MongoDB databases though javascript.

First was to connect to the Node server where the database will be stored. Next I created a Schema which is essentially a database.

Methods can also be added to schema in order to test things by posting strings to the console.

Next was to create a model which serves as the structure of the database. Unlike PHP databases fields can be added in after the model is created. Here I created a model called Kitten and then added a field called name to the items and gave the kitten a name. I then called the speak method on both of the kittens.

mongooseTest-printout

The kittens then run the speak method to the console, which prints out their names.

After printing the speak method to the console, I used the find function to search the database and print a list of kittens to the console.

Propelorm.org, 2015. Propel, The Blazing Fast Open-Source PHP 5.4 ORM. [online] Available from: http://propelorm.org/ [Accessed 28 Nov. 2015].

Foundation, N., 2015. Node.js. [online] Nodejs.org. Available from: https://nodejs.org/en/ [Accessed 28 Nov. 2015].

Johnny-Five, 2015. Johnny-Five is the JavaScript Robotics Programming Framework. [online] Available from: http://johnny-five.io/ [Accessed 28 Nov. 2015].

Mongodb.org, 2015. MongoDB for GIANT Ideas | MongoDB. [online] Available from: https://www.mongodb.org/ [Accessed 28 Nov. 2015].

Mongoosejs.com, 2015. Mongoose ODM v4.2.8. [online] Available from: http://mongoosejs.com/ [Accessed 28 Nov. 2015].

Strauch, C., 2015. No SQL databases. [online] Available from: http://www.christof-strauch.de/nosqldbs.pdf [Accessed 28 Nov. 2015].

Mongoosejs.com, 2015. Mongoose Quick Start v4.2.8. [online] Available from: http://mongoosejs.com/docs/index.html [Accessed 28 Nov. 2015].