FABE: Calling the data server

To get the real data from MySQL we will be using our good friend from a previous post, the dataserver.jss library which will handle all our data needs.

Just copy that library from here dataserver.jss and drop it in the /fabe/ folder.

There is nothing we have to change in that library except for the username and password, also the database to 'myblog'. Maybe in the future we will move config data to a config file away from prying eyes, but for our tut it will suffice as it is.

Now, let's change the models.jss library to fetch the data from MySQL just by modifying the getLasEntries() method, the library will now look like this:

var SQL = require('./dataserver.jss').SQL;

function getLastEntries(n){
  var sql,query,data;
  query = 'SELECT * FROM posts ORDER BY date DESC LIMIT {0}';
  sql   = new SQL();
  data  = sql.getRecords(query,[n]);
  sql.disconnect();
  return data;
}

exports.getLastEntries=getLastEntries;

Simple, just a call to MySQL to get our last blog entries.

Of course you will get nothing if you try to run it. We need to create the database and our tables first.

So let's run the wonderful PhpMyAdmin tool and create a database named 'myblog' and add a table named 'posts' with the folowing structure:

id        int
date      datetime
title     varchar 120
content   text
summary   text
url       varchar 60

Or SQL speak if you prefer so:

CREATE TABLE 'posts' (
  'id'      int(11)      NOT NULL AUTO_INCREMENT,
  'date'    datetime     DEFAULT NULL,
  'title'   varchar(120) DEFAULT NULL,
  'content' text,
  'summary' text,
  'url'     varchar(60)  DEFAULT NULL,
  PRIMARY KEY ('id')
);

Now add a couple of dummy records to it just to see it working. Beautiful!

As you already can guess, sooner than later we will need to create a form to add posts to our blog.

More on that, next week...