Skip to content

Commit

Permalink
switched to using uberdb for ORM
Browse files Browse the repository at this point in the history
  • Loading branch information
ethersheet-collective committed Dec 18, 2012
1 parent 0db036c commit c5c326f
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 186 deletions.
13 changes: 8 additions & 5 deletions examples/config-example.js
Expand Up @@ -2,9 +2,12 @@ module.exports = {
port: 8080,
https_key: './examples/snakeoil.key',
https_cert: './examples/snakeoil.crt',
mysql_database: 'ethersheet',
mysql_user: 'ethersheet',
mysql_password: 'ethersheet',
mysql_host: 'localhost',
debug: false
db_name: 'ethersheet',
db_user: 'ethersheet',
db_password: 'ethersheet',
db_host: 'localhost',
db_type: 'mysql',
debug: false,
default_row_count: 100,
default_col_count: 20
}
220 changes: 132 additions & 88 deletions lib/ethersheet.js
@@ -1,17 +1,26 @@
var util = require("util");
var events = require("events");
var mysql = require('mysql');
var ueberDB = require("ueberDB");
var uuid = require('node-uuid').v4;
var async = require('async');

/***********************************************
* EtherSheetService
***********************************************/
var EtherSheetService = exports.EtherSheetService = function(config){
var es = this;
es.config = config;
events.EventEmitter.call(this);
this.sql = mysql.createConnection({
user: config.mysql_user,
password: config.mysql_password,
host: config.mysql_host,
database: config.mysql_database
this.connectionHandler = function(){};
es.db = new ueberDB.database(
es.config.db_type, {
user: es.config.db_user,
host: es.config.db_host,
password: es.config.db_password,
database: es.config.db_name
});
es.db.init(function(err){
es.connectionHandler(err);
});
}
// inherits from EventEmitter
Expand All @@ -30,106 +39,141 @@ EtherSheetService.colors = [
];

// EtherSheet API
EtherSheetService.prototype.find_or_create_user = function(user_id, cb){
var es = this;
es.sql.query(
'SELECT * FROM users WHERE user_id = ?',
[user_id],
function(err, results, fields) {
if(results.length > 0){ // user exists
cb(err, results[0], results);
} else {
es.sql.query(
'INSERT INTO users (user_id, color) VALUES (?, ?)',
[user_id, es.get_random_color()],
function(err, results, fields){
if(err) {
throw err;
} else {
es.find_or_create_user(user_id, cb);
}
}
);
}
}
);
}

EtherSheetService.prototype.add_user_to_room = function(user, sheet_id, cb){
EtherSheetService.sheets[sheet_id] = EtherSheetService.sheets[sheet_id] || {count:0, users:{}};
EtherSheetService.sheets[sheet_id].count++;
EtherSheetService.sheets[sheet_id].users[user.user_id] = user;
cb(null, null);
EtherSheetService.prototype.onConnect = function(cb){
this.connectionHandler = cb;
};

EtherSheetService.prototype.remove_user_from_room = function(user, sheet_id){
delete(EtherSheetService.sheets[sheet_id].users[user.user_id]);
EtherSheetService.sheets[sheet_id].count--;
if(EtherSheetService.sheets[sheet_id].count < 1){
delete(EtherSheetService.sheets[sheet_id]);
}
}

EtherSheetService.prototype.get_random_color = function(){
EtherSheetService.prototype.getRandomColor = function(){
var idx = Math.floor(Math.random() * 100);
return EtherSheetService.colors[idx % EtherSheetService.colors.length]
};

EtherSheetService.prototype.save_sheet = function(sheet_id, sheet_data){
this.sql.query(
'UPDATE sheets SET sheetdata = ? WHERE sheetid = ?', [sheet_data, sheet_id],
function(err, results, fields){
if(err){
console.log('ERROR: ' + err);
}
EtherSheetService.prototype.getSheet = function(sheet_id,cb){
var es = this;
async.parallel({
row_index: function(cb){
es.getRowIndex(sheet_id,cb);
},
col_index: function(cb){
es.getColIndex(sheet_id,cb);
},
cells: function(cb){
es.getCells(sheet_id,cb);
}
);
};
},function(err,data){
if(err) throw err;
if(!data.row_index){
return es.createSheet(sheet_id,cb);
}
cb(null,data);
});
};

EtherSheetService.prototype.find_or_create_sheet = function(sheet_id,cb){
EtherSheetService.prototype.createSheet = function(sheet_id,cb){
var es = this;
es.sql.query(
'SELECT * FROM sheets WHERE sheetid = ?',
[sheet_id],
function(err, results, fields) {
if(results.length > 0){ // a sheet exists
//load the data and emit an event
cb(err, results[0], results);
} else {
//create a new sheet
es.create_sheet(sheet_id,cb);
}
var data = {
row_index: es.initializeRows(),
col_index: es.initializeCols(),
cells: {}
};

async.parallel({
row_index: function(cb){
es.setRowIndex(sheet_id,data.row_index,cb);
},
col_index: function(cb){
es.setColIndex(sheet_id,data.col_index,cb);
},
cells: function(cb){
es.setCells(sheet_id,data.cells,cb);
}
);
},function(err){
if(err) throw err;
cb(null,data);
});
};

EtherSheetService.prototype.create_sheet = function(sheet_id,cb){
EtherSheetService.prototype.deleteSheet = function(sheet_id,cb){
var es = this;
es.sql.query(
'INSERT INTO sheets VALUES (?, ?)',
[sheet_id, ''],
function(err, results, fields){
cb(err, {sheetid: sheet_id, sheetdata: ""}, results);
async.parallel({
row_index: function(cb){
es.deleteRowIndex(sheet_id,cb);
},
col_index: function(cb){
es.deleteColIndex(sheet_id,cb);
},
cells: function(cb){
es.deleteCells(sheet_id,cb);
}
);
};
},function(err){
if(err) throw err;
cb(null);
});
};

EtherSheetService.prototype.delete_user = function(user_id, cb){
var es = this;
es.sql.query(
'DELETE FROM users WHERE user_id = ?', [user_id],
function(err, results){
cb(err, results);
EtherSheetService.prototype.initializeRows = function(){
var es = this;
var row_count = es.config.default_row_count;
var rows = [];
for(var i = 0; i<row_count; i++){
rows.push(uuid());
}
);
return rows
};
EtherSheetService.prototype.initializeCols = function(){
var es = this;
var col_count = es.config.default_col_count;
var cols = [];
for(var i = 0; i<col_count; i++){
cols.push(uuid());
}
return cols;
};

EtherSheetService.prototype.delete_sheet = function(sheetid, cb){
EtherSheetService.prototype.setRowIndex = function(sheet_id,row_index,cb){
var es = this;
es.sql.query(
'DELETE FROM sheets WHERE sheetid = ?', [sheetid],
function(err, results){
cb(err, results);
}
);
es.db.set(sheet_id + ':row_index', row_index, cb);
};

EtherSheetService.prototype.getRowIndex = function(sheet_id,cb){
var es = this;
es.db.get(sheet_id + ':row_index',cb);
};

EtherSheetService.prototype.deleteRowIndex = function(sheet_id,cb){
var es = this;
es.db.remove(sheet_id + ':row_index',cb);
};


EtherSheetService.prototype.setColIndex = function(sheet_id, col_index, cb){
var es = this;
es.db.set(sheet_id + ':col_index', col_index, cb);
};

EtherSheetService.prototype.getColIndex = function(sheet_id, cb){
var es = this;
es.db.get(sheet_id + ':col_index',cb);
};

EtherSheetService.prototype.deleteColIndex = function(sheet_id,cb){
var es = this;
es.db.remove(sheet_id + ':col_index',cb);
};

EtherSheetService.prototype.setCells = function(sheet_id, cells, cb){
var es = this;
es.db.set(sheet_id + ':cells', cells, cb);
};

EtherSheetService.prototype.getCells = function(sheet_id, cb){
var es = this;
es.db.get(sheet_id + ':cells',cb);
};

EtherSheetService.prototype.deleteCells = function(sheet_id,cb){
var es = this;
es.db.remove(sheet_id + ':cells',cb);
};

10 changes: 8 additions & 2 deletions lib/server.js
Expand Up @@ -2,7 +2,7 @@ var http = require('http');
var express = require('express');
var sockjs = require('sockjs');
var Transactor = require('transactor');
var esSocketHandler = require('./socket_handler');
var EtherSheetService = require('./ethersheet');

var ES_CLIENT_PATH= __dirname + '/../node_modules/es_client';
var LAYOUT_PATH = __dirname + '/layouts';
Expand All @@ -22,8 +22,14 @@ exports.createServer = function(config){
app.use(express.logger({ format: ':method :url' }));
app.use('/es_client',express.static(ES_CLIENT_PATH));

/***********************************************
* EtherSheet DB Client
***********************************************/
var es = new EtherSheetService(config);

// listen after setup
process.nextTick(function(){
es.onConnect(function(err){
if(err) throw err;
http_server.listen(config.port, config.host, function(){
console.log('ethersheet is listening over http on port ' + config.port);
});
Expand Down
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -18,6 +18,9 @@
"mysql": ">=0.9.5",
"underscore": ">=1.3",
"sockjs": ">=0.3.5",
"ueberDB": "git://github.com/Pita/ueberDB.git",
"async": "*",
"node-uuid": "*",
"stitchit": "*"
},
"devDependencies": {
Expand Down

0 comments on commit c5c326f

Please sign in to comment.