Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
more database logic elsewhere and update example to use proper db
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnMcLear committed Apr 29, 2015
1 parent 6688b76 commit c856475
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
2 changes: 1 addition & 1 deletion settings.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"user" : "root",
"host" : "localhost",
"password": "",
"database": "store"
"database": "etherdraw"
},
*/

Expand Down
45 changes: 20 additions & 25 deletions src/util/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,41 @@ var settings = require('./Settings.js'),
// Database connection
var db = new ueberDB.database(settings.dbType, settings.dbSettings);

// Init..
db.init(function(err){
if(err){
console.error(err);
}
});

// Write to teh database
exports.storeProject = function(room) {
var project = projects.projects[room].project;
var json = project.exportJSON();
db.init(function (err) {
if(err) {
console.error(err);
}
console.log("Writing project to database");
db.set(room, {project: json});
});
console.log("Writing project to database");
db.set(room, {project: json});
}

// Try to load room from database
exports.load = function(room, socket) {
console.log("load from db");
if (projects.projects[room] && projects.projects[room].project) {
var project = projects.projects[room].project;
db.init(function (err) {
if(err) {
console.error(err);
db.get(room, function(err, value) {
if (value && project && project.activeLayer) {
socket.emit('loading:start');
// Clear default layer as importing JSON adds a new layer.
// We want the project to always only have one layer.
project.activeLayer.remove();
project.importJSON(value.project);
socket.emit('project:load', value);
}
console.log("Initting db");
db.get(room, function(err, value) {
// if (value && project && project instanceof drawing.Project && project.activeLayer) {
if (value && project && project.activeLayer) {
socket.emit('loading:start');
// Clear default layer as importing JSON adds a new layer.
// We want the project to always only have one layer.
project.activeLayer.remove();
project.importJSON(value.project);
socket.emit('project:load', value);
}
socket.emit('loading:end');
db.close(function(){});
});
socket.emit('loading:end'); // used for sending back a blank database in case we try to load from DB but no project exists
socket.emit('loading:end');
});
socket.emit('loading:end'); // used for sending back a blank database in case we try to load from DB but no project exists
} else {
loadError(socket);
}
}

exports.db = db;

0 comments on commit c856475

Please sign in to comment.