Permalink
Browse files

Changed shoutbox example to use MySQL instead of CouchDB.

  • Loading branch information...
1 parent 255993a commit a30c21b0e23c74c30c2af53a00b57531112fbb25 @cskr committed Feb 12, 2011
@@ -1,3 +1,3 @@
-This example requires [node-couchdb](http://github.com/felixge/node-couchdb). Install it with these commands,
+This example requires [node-mysql](http://github.com/felixge/node-mysql). Install it with these commands,
- npm install couchdb
+ npm install mysql
@@ -1,31 +1,39 @@
-var couchdb = require('couchdb'),
+var mysql = require('mysql'),
+ db_cred = require('../db_cred'),
Shout = require('./model').Shout;
exports.all = function(cb) {
- var db = couchdb.createClient().db('shout');
- db.view('shout', 'all', {}, function(err, res) {
+ var client = getClient();
+ client.query('select * from shouts', function(err, results) {
+ var shouts = [];
if(!err) {
- var shouts = []
- res.rows.forEach(function(row) {
+ results.forEach(function(result) {
var shout = new Shout();
- shout.name(row.value.name)
- .email(row.value.email)
- .message(row.value.message);
+ shout.name(result.name)
+ .email(result.email)
+ .message(result.message);
shouts.push(shout);
});
}
+
+ client.end();
cb(err, shouts);
});
};
exports.save = function(shout, cb) {
- var doc = {
- name: shout.name(),
- email: shout.email(),
- message: shout.message()
- };
- var db = couchdb.createClient().db('shout');
- db.saveDoc(doc, function(err) {
- cb(err);
+ var client = getClient();
+ client.query('insert into shouts(name, email, message) values (?,?,?)',
+ [ shout.name(), shout.email(), shout.message() ],
+ function(err) {
+ client.end();
+ cb(err);
});
};
+
+function getClient() {
+ var client = new mysql.Client(db_cred);
+ client.database = 'shoutbox';
+ client.connect();
+ return client;
+}
@@ -1,14 +1,14 @@
-var couchdb = require('couchdb'),
- client = couchdb.createClient();
+var mysql = require('mysql'),
+ db_cred = require('./db_cred');
-client.request('put', '/shout');
+var client = new mysql.Client(db_cred);
-client.db('shout').saveDesign('shout', {
- views: {
- all: {
- map: function(doc) {
- emit(null, doc);
- }
- }
- }
+client.connect();
+
+client.query('create database shoutbox');
+client.query('use shoutbox');
+client.query('create table shouts (id int primary key auto_increment, '
+ + 'name varchar(50), email varchar(50), '
+ + 'message varchar(255))', function() {
+ client.end();
});
@@ -0,0 +1,4 @@
+module.exports = {
+ user: 'root',
+ password: 'pass'
+}

0 comments on commit a30c21b

Please sign in to comment.