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


The phpMyAdmin interface


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


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.

//connect to the test server
var mongoose = require('mongoose');

var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));

db.once('open', function (callback) {

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

//create a schema ie a database tables

	var kittySchema = mongoose.Schema({
			name: String


	//add a function to a schema must be added before compiling with mongood model
	kittySchema.methods.speak = function () {
		var greeting =
		? "My name is " +

		: "I don't have a name";


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

//create a model with which to create instances of items in the database
	var Kitten = mongoose.model('Kitten', kittySchema);

	var fluffy = new Kitten({name : 'Fluffy'});

	var tibbles = new Kitten({name : 'Tibbles'});

	//save fluffy to the database (err, fluffy) {
		if (err) return console.error(err);



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.


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

	//find kitten in database

	Kitten.find(function (err, kittens)
		if (err) return console.error(err);


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., 2015. Propel, The Blazing Fast Open-Source PHP 5.4 ORM. [online] Available from: [Accessed 28 Nov. 2015].

Foundation, N., 2015. Node.js. [online] Available from: [Accessed 28 Nov. 2015].

Johnny-Five, 2015. Johnny-Five is the JavaScript Robotics Programming Framework. [online] Available from: [Accessed 28 Nov. 2015]., 2015. MongoDB for GIANT Ideas | MongoDB. [online] Available from: [Accessed 28 Nov. 2015]., 2015. Mongoose ODM v4.2.8. [online] Available from: [Accessed 28 Nov. 2015].

Strauch, C., 2015. No SQL databases. [online] Available from: [Accessed 28 Nov. 2015]., 2015. Mongoose Quick Start v4.2.8. [online] Available from: [Accessed 28 Nov. 2015].