This repository has been archived by the owner on Jan 4, 2019. It is now read-only.
forked from byrichardpowell/draw
-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathdb.js
45 lines (40 loc) · 1.33 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var settings = require('./Settings.js'),
projects = require('./projects.js'),
ueberDB = require('ueberDB')
// 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();
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.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);
}
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;