Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

switched to using uberdb for ORM

  • Loading branch information...
commit c5c326f26dacc7666815ff40fc35906a2e7a0c80 1 parent 0db036c
ethersheet-collective authored
View
13 examples/config-example.js
@@ -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
}
View
220 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
@@ -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);
+};
+
View
10 lib/server.js
@@ -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';
@@ -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);
});
View
3  package.json
@@ -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": {
View
134 test/unit/ethersheet.js
@@ -2,6 +2,7 @@ var EtherSheetService = require('../../lib/ethersheet').EtherSheetService;
var _ = require('underscore');
var config = require('../../config.js');
var should = require('chai').should();
+var async = require('async');
describe('EtherSheetService', function(){
@@ -18,119 +19,70 @@ describe('EtherSheetService', function(){
var es = new EtherSheetService(config);
var colors = [];
for(var i=0; i<100; i++){
- colors.push(es.get_random_color());
+ colors.push(es.getRandomColor());
}
_.uniq(colors).length.should.eql(EtherSheetService.colors.length);
})
})
- describe('.sql', function(){
- it("should have a sql connection", function(){
- var es = new EtherSheetService(config);
- es.should.have.property('sql');
- es.sql.should.have.property('query');
- })
- })
-
- describe('users', function(){
- var es = new EtherSheetService(config);
- it('should delete a user', function(done){
- es.delete_user('test',function(err, results){
- if(err){
- throw err;
- }
- results.should.have.property('affectedRows');
- results.affectedRows.should.be.below(2);
- done();
- });
- });
-
- it('should create a user', function(done){
- es.find_or_create_user('test', function(err, user){
- user.should.have.property('color');
- user.should.have.property('nickname');
- user.should.have.property('user_id');
- user.user_id.should.eql('test');
- user.color.should.not.be.false;
- done();
- });
- });
-
- it('should find a user', function(done){
- es.find_or_create_user('test', function(err, user, results){
- user.should.have.property('user_id');
- results.length.should.eql(1);
- done();
- });
+ describe('sheets', function(){
+ var es;
+
+ before(function(done){
+ es = new EtherSheetService(config);
+ es.onConnect(done);
});
- })
- describe('sheets', function(){
- var es = new EtherSheetService(config);
it('should delete a sheet', function(done){
- es.delete_sheet('test',function(err, results){
- if(err){
- throw err;
- }
- results.should.have.property('affectedRows');
- results.affectedRows.should.be.below(2);
- done();
+ es.createSheet('delete_test',function(err){
+ if(err) throw err;
+ es.deleteSheet('delete_test',function(err){
+ if(err) throw err;
+ es.getRowIndex('delete_test',function(err,rows){
+ if(err) throw err;
+ should.not.exist(rows);
+ done();
+ });
+ });
});
});
- it('should create a sheet', function(done){
- es.find_or_create_sheet('test', function(err, sheet){
+ it('should create a sheet if it does not exist', function(done){
+ es.getSheet('create_test', function(err, sheet){
if(err){
throw err;
}
- sheet.should.have.property('sheetid');
- sheet.should.have.property('sheetdata');
- sheet.sheetid.should.eql('test');
- sheet.sheetdata.should.eql = '';
+ sheet.should.have.property('row_index');
+ sheet.should.have.property('col_index');
+ sheet.should.have.property('cells');
+ sheet.cells.should.be.empty;
+ sheet.row_index.length.should.eq(config.default_row_count);
+ sheet.col_index.length.should.eq(config.default_col_count);
done();
});
});
it('should find a sheet', function(done){
- es.find_or_create_sheet('test', function(err, sheet, results){
+ var cb = function(){};
+ async.parallel([
+ function(cb){es.setRowIndex('test_sheet',['foo'],cb)},
+ function(cb){es.setColIndex('test_sheet',['bar'],cb)},
+ function(cb){es.setCells('test_sheet',{foo:{bar:123}},cb)},
+ ],
+ function(err){
if(err){
throw err;
}
- sheet.should.have.property('sheetid');
- results.length.should.eql(1);
- done();
- });
- });
- })
-
- describe('adding and removing user from rooms', function(){
- var es = new EtherSheetService(config);
- var test_sheet = '';
- var test_user = '';
- before(function(done){
- es.find_or_create_sheet('test', function(err, sheet){
- test_sheet = sheet;
- });
- es.find_or_create_user('test', function(err, user){
- test_user = user;
- });
- done();
- })
-
- it('should add a user to a room', function(){
- EtherSheetService.sheets.should.be.a('object');
- es.add_user_to_room(test_user, test_sheet.sheetid, function(){
- EtherSheetService.sheets.should.have.property(test_sheet.sheetid);
- EtherSheetService.sheets[test_sheet.sheetid].count.should.eql(1);
- EtherSheetService.sheets[test_sheet.sheetid].users.should.have.property(test_user.user_id);
+ es.getSheet('test_sheet', function(err, sheet){
+ if(err){
+ throw err;
+ }
+ sheet.cells.should.not.be.empty;
+ sheet.cells['foo']['bar'].should == 123;
+ done();
+ });
});
});
-
- it('should remove a user from a room', function(){
- es.remove_user_from_room(test_user, test_sheet.sheetid);
- EtherSheetService.sheets.should.not.have.property(test_sheet.sheetid);
- });
- })
-
-})
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.