syntaxhighlighter

Showing posts with label sqlite. Show all posts
Showing posts with label sqlite. Show all posts

Sunday, August 26, 2012

SQLite - express-examples/sqlite

Installing SQLite3

SQLite3 is very simple, and perfect for examples "plug & play". For installing it:

$ sudo aptitude install sqlite3

Creating a Database

In my case I just entered:

$ sqlite3 db/db.sqlite

Once inside, you can enter SQL commands. Following sqlite example table:

sqlite> CREATE TABLE user (name VARCHAR(50)); 

In SQLite exists a default auto-increment field named `rowid`, that means you can make queries like:

sqlite> SELECT rowid AS id, name FROM user WHERE id=?

Installing SQLite plugin for Node.js

I'm using node-sqlite3. I found this plugin immature and not working properly under several scenarios, since I'm not really interested in developing something complex with sqlite, not going to invest more time on this, but maybe node-sqlite is a better option.

For installing it:

$ npm install sqlite3

No matter which of the 2 previously mentioned plugins you use, you have to use callback programming style.

var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('./db/db.sqlite');

// Querying
db.all("SELECT rowid AS id, name FROM user WHERE id=?", [ id ], function(err, rows) {
  if (err) 
    console.log(err);

  callback((rows)?rows[0]:null);
});

// Adding
db.run("INSERT INTO user(name) VALUES(?)", [ name ]/*, function(error) {
  if (error) 
    console.log(error);

  callback((error)?false:true);
}*/);
callback(true); // TODO: Callback is not working!!

As you can see in the code above, callbacks are not working properly in `db.run` method. But, the really interesting thing I want to emphasize is the use of them (callbacks), this adds a-synchronicity to your code, which means things are not blocked until getting a response. Of course if you're experienced with thread capable languages (e.g. Java), this may not surprise you, but if you're PHP programmer you should be surprised. Another thing, the simplicity and let me say "harmony" of this approach is outstanding.

Friday, August 24, 2012

How to run the project - express-examples/sqlite

Installing Node.js

For create the application I installed node.js and its package manager npm. For doing that I just typed:

$ sudo aptitude install nodejs npm

My actual linux installation (obsolete):

$ cat /etc/issue
 Ubuntu 10.10 \n \l

My actual node.js packages are:

$ dpkg -l node\* npm
...
ii  nodejs                      0.8.2-1chl1~maverick1       Node.js event-based server-side javascript engine
ii  nodejs-dev                  0.8.2-1chl1~maverick1       Development files for Node.js
ii  npm                         1.1.39-1chl1~maverick1      package manager for nodejs

Note: If you want to install an specific version, you can do it directly from the sources.

Downloading the code

$ git clone https://github.com/camposer/express-examples.git

Starting the server

$ cd express-examples
$ cd sqlite
$ node app
Express server listening on port 3000

Running the app

Just enter in your browser: http://localhost:3000

Testing the REST services

You can test the REST services directly using CURL (installing it is as simple as enter: `sudo aptitude install curl`). Inside the app there is a script named curltest.sh.

echo "getUsers"
curl -X GET http://localhost:3000/rest/users; echo

echo "getUserById"
curl -X GET http://localhost:3000/rest/users/1; echo

echo "addUser"
curl -X PUT -H "Content-type:application/x-www-form-urlencoded" -d "name=rodolfo" http://localhost:3000/rest/users; echo

echo "updateUser"
curl -X POST -H "Content-type:application/x-www-form-urlencoded" -d "id=1&name=juan" http://localhost:3000/rest/users; echo

echo "deleteUser"
curl -X DELETE -H "Content-type:application/x-www-form-urlencoded" -d "id=1" http://localhost:3000/rest/users; echo

Kickoff - express-examples/sqlite

This is a little late kickoff for the project, but as I'm starting this blog and planning to bring here all my previous articles related...

Project location:

You can find the project in my github space at: github.com/camposer/express-examples

I'm planning to bring here all articles previously wrote in the project's wiki, but while I do that, you can find project's documentation here

Project goal:

I've been playing with Node.js for several days and started to ask myself how an application for real environments should be... This is how I got here, wanting to clarify common things/problems such as: i18n, MVC, DAO, SOA, REST, testing (xUnit style), documentation (Javadoc style), etc.

I hope this "conventions" (set of technologies and practices) help other programmers facing similar problems. I'm planning to add other examples including NoSQL and ORM, but right now only developed a simple app with Express as web framework and SQLite as SQL database engine. The app has CRUD operations for a user entity and was developed the way I think web apps should be programmed this days:

  • Presentation: HTML and CSS (inside CSS files). The HTML files should contain only static data and controls, NO format and NO logic.
  • Presentation logic: Javascript (inside JS files) and jQuery. jQuery Template for modifying presentation dynamically using HTML blocks. jQuery UI for improve user interface and effects.
  • Business logic interface: REST services. Presentation logic call REST services (using jQuery), which produces JSON data.
  • Business logic: Logic or service classes, separated from REST facades. This way your app can scale and be maintained easily. SOA style, decoupling!!!
  • Data logic: DAO classes. If you include an ORM, DAO should exist also for abstraction.