Databases review

Relational database management systems

Relational database management systems (RDBMSs) allow complex information to be stored and easily queried. RDBMSs have traditionally been used for relatively high-end applications, such as content management, customer relationship management, and shopping carts. They can perform well when used correctly, but they require special- ized administration knowledge and access to a database server. They also require knowledge of SQL, although there are object-relational mappers (ORMs) with APIs that can write SQL for you in the background. RDBMS administration, ORMs, and SQL are beyond the scope of this book, but you’ll find many online resources that cover these technologies.

MySQL

MySQL is the world’s most popular SQL database, and it’s well supported by the Node community. If you’re new to MySQL and interested in learning about it, you’ll find the official tutorial online (http://dev.mysql.com/doc/refman/5.0/en/tutorial.html). For those new to SQL, many online tutorials and books, including Chris Fehily’s SQL: Visual QuickStart Guide (Peachpit Press, 2008), are available to help you get up to speed.

PostgreSQL

PostgreSQL is well regarded for its standards compliance and robustness, and many Node developers favor it over other RDBMSs. Unlike MySQL, PostgreSQL supports recursive queries and many specialized data types. PostgreSQL can also use a variety of standard authentication methods, such as Lightweight Directory Access Protocol (LDAP) and Generic Security Services Application Program Interface (GSSAPI). For those using replication for scalability or redundancy, PostgreSQL supports synchro- nous replication, a form of replication in which data loss is prevented by verifying rep- lication after each data operation.

Redis

Redis is a data store well suited to handling simple data that doesn’t need to be stored for long-term access, such as instant messages and game-related data. Redis stores data in RAM, logging changes to it to disk. The downside to this is that storage space is limited, but the advantage is that Redis can perform data manipulation quickly. If a Redis server crashes and the contents of RAM are lost, the disk log can be used to restore the data.

Node Js Class inheritance

// --- begin of definition of Watcher class -----

function Watcher(watchDir, processedDir) {
    this.watchDir = watchDir;
    this.processedDir = processedDir;
}

var events = require('events'), util = require('util');
util.inherits(Watcher, events.EventEmitter);

var fs = require('fs'),
    watchDir = './watch',
    processedDir = './done';

// The `watch` method cycles through the directory, procesing any files found.
// Extend EventEmitter with method that processes files
Watcher.prototype.watch = function() {
    var watcher = this; //Store reference to Watcher object for use in readdir callback
    fs.readdir(this.watchDir, function(err, files) {
	if (err) throw err;
	for (var index in files) {
	    watcher.emit('process', files[index]);
	}
    })
}

// The `start` method starts the directory monitoring.
// Extend EventEmitter with method to start watching
Watcher.prototype.start = function() {
    var watcher = this;
    fs.watchFile(watchDir, function() {
	watcher.watch();
    });
}
// ---- end of definition of Watcher class ----