From 954be66c7f93c6ee7f1ade04e8c1f36b4019324b Mon Sep 17 00:00:00 2001 From: Candid Dauth Date: Thu, 11 Dec 2014 02:42:32 +0100 Subject: [PATCH] Migrating from mongoose to sequelize, introducing types --- frontend/js/ng.js | 43 +++- frontend/js/pad.js | 17 +- frontend/pad.html | 36 +++- server/config.js | 6 +- server/lib/database.js | 91 ++++++-- server/lib/databaseBackendMongodb.js | 305 --------------------------- server/lib/server.js | 90 ++++++-- server/lib/utils.js | 48 ++++- server/package.json | 2 +- 9 files changed, 278 insertions(+), 360 deletions(-) delete mode 100644 server/lib/databaseBackendMongodb.js diff --git a/frontend/js/ng.js b/frontend/js/ng.js index e6537b3a..bdebff64 100644 --- a/frontend/js/ng.js +++ b/frontend/js/ng.js @@ -171,6 +171,7 @@ $scope.markers = { }; $scope.lines = { }; $scope.views = { }; + $scope.types = { }; $scope.dialog = null; $scope.dialogError = null; $scope.saveViewName = null; @@ -192,7 +193,7 @@ $scope.onMove = function() { if($scope.currentMarker) - $scope.currentMarker.xy = fp.posToXy($scope.currentMarker.position); + $scope.currentMarker.xy = fp.posToXy($scope.currentMarker); if($scope.currentLine && $scope.currentLine.clickPos) $scope.currentLine.clickXy = fp.posToXy($scope.currentLine.clickPos); }; @@ -297,7 +298,14 @@ $scope.onMove(); }.fpWrapApply($scope)); - $scope.addMarker = function() { + $scope.addObject = function(type) { + if(type.type == "marker") + $scope.addMarker(type); + else if(type.type == "line") + $scope.addLine(type); + }; + + $scope.addMarker = function(type) { var message = $scope.showMessage("info", "Please click on the map to add a marker.", [ { label: "Cancel", click: function() { $scope.closeMessage(message); @@ -307,7 +315,7 @@ var listener = fp.addClickListener(function(pos) { $scope.closeMessage(message); - socket.emit("addMarker", { position: { lon: pos.lon, lat: pos.lat } }, function(err, marker) { + socket.emit("addMarker", { lon: pos.lon, lat: pos.lat, typeId: type.id }, function(err, marker) { if(err) return $scope.showMessage("error", err); @@ -339,7 +347,7 @@ var listener = fp.addClickListener(function(pos) { $scope.closeMessage(message); - socket.emit("editMarker", { id: marker.id, position: pos }, function(err) { + socket.emit("editMarker", { id: marker.id, lat: pos.lat, lon: pos.lon }, function(err) { if(err) return $scope.showMessage("error", err); @@ -355,8 +363,11 @@ }); }; - $scope.addLine = function() { - socket.emit("addLine", { points: [ ] }, function(err, line) { + $scope.addLine = function(type) { + socket.emit("addLine", { points: [ ], typeId: type.id }, function(err, line) { + if(err) + return $scope.showMessage("error", err); + line.actualPoints = [ ]; $scope.currentLine = line; var message = $scope.showMessage("info", "Please click on the map to draw a line. Double-click to finish it.", [ @@ -474,7 +485,7 @@ return $scope.dialogError = err; if(makeDefault) { - socket.emit("editPad", { defaultView: view.id }, function(err) { + socket.emit("editPad", { defaultViewId: view.id }, function(err) { if(err) return $scope.dialogError = err; @@ -489,7 +500,7 @@ }; $scope.setDefaultView = function(view) { - socket.emit("editPad", { defaultView: view.id }, function(err) { + socket.emit("editPad", { defaultViewId: view.id }, function(err) { if(err) $scope.dialogError = err; }); @@ -502,6 +513,14 @@ }); }; + $scope.editType = function(type) { + // TODO + }; + + $scope.removeType = function(type) { + // TODO + }; + $scope.copyPad = function() { socket.emit("copyPad", { toId: $scope.copyPadId }, function(err) { if(err) { @@ -670,6 +689,14 @@ $scope.padData.defaultViewId = null; }); + socket.on("type", function(data) { + $scope.types[data.id] = data; + }); + + socket.on("deleteType", function(data) { + delete $scope.types[data.id]; + }); + socket.on("disconnect", function() { $scope.error = $scope.showMessage("error", "The connection to the server was lost."); $scope.markers = { }; diff --git a/frontend/js/pad.js b/frontend/js/pad.js index a6678466..086f5720 100644 --- a/frontend/js/pad.js +++ b/frontend/js/pad.js @@ -164,15 +164,16 @@ var FacilPad = { } fp.getCurrentView = function() { - var ret = { - view: fp.map.getExtent().clone().transform(fp.map.getProjectionObject(), _p()), - baseLayer: fp.map.baseLayer.permalinkName, - layers: [ ] - }; + var ret = fp.map.getExtent().clone().transform(fp.map.getProjectionObject(), _p()); + + ret.baseLayer = fp.map.baseLayer.permalinkName; + ret.layers = [ ]; + for(var i=0; i @@ -76,7 +76,7 @@

{{currentMarker.name}}

-

Coordinates: {{round(currentMarker.position.lat, 5)}}, {{round(currentMarker.position.lon, 5)}}

+

Coordinates: {{round(currentMarker.lat, 5)}}, {{round(currentMarker.lon, 5)}}

@@ -170,6 +170,34 @@
+
+
+

{{dialogError}}

+ + + + + + + + + + + + + + + + + + + + + +
NameTypeEdit
{{type.name}}{{type.type}}
+
+
+

{{dialogError}}

diff --git a/server/config.js b/server/config.js index 96e6a375..c7cb4031 100644 --- a/server/config.js +++ b/server/config.js @@ -2,11 +2,11 @@ module.exports = { host : null, port : 40829, db : { - type: "mongodb", // mongodb, mysql, postgres, mariadb, sqlite + type: "mysql", // mysql, postgres, mariadb, sqlite host: "localhost", port: null, database: "facilpad", - user: null, - password: null + user: "facilpad", + password: "password" } }; \ No newline at end of file diff --git a/server/lib/database.js b/server/lib/database.js index a7a1bbec..e83c3e69 100644 --- a/server/lib/database.js +++ b/server/lib/database.js @@ -1,21 +1,33 @@ -var backend = require("./databaseBackendMongodb"); +var backend = require("./databaseBackendSequelize"); var listeners = require("./listeners"); var routing = require("./routing"); var utils = require("./utils"); var async = require("async"); var underscore = require("underscore"); +var DEFAULT_TYPES = [ + { name: "marker", type: "marker", fields: { description: { type: "textarea" } } }, + { name: "line", type: "line", fields: { description: { type: "textarea" } } } +]; + function getPadData(padId, callback) { backend.getPadDataByWriteId(padId, function(err, data) { if(err || data != null) - return callback(err, utils.extend(data, { writable: true })); + return callback(err, utils.extend(JSON.parse(JSON.stringify(data)), { writable: true, writeId: null })); backend.getPadData(padId, function(err, data) { if(err || data != null) - return callback(err, utils.extend(data, { writable: false })); + return callback(err, utils.extend(JSON.parse(JSON.stringify(data)), { writable: false, writeId: null })); backend.createPad(utils.generateRandomId(10), padId, function(err, data) { - callback(err, utils.extend(data, { writable: true })); + if(err) + return callback(err); + + async.each(DEFAULT_TYPES, function(it, next) { + backend.createType(data.id, it, next); + }, function(err) { + callback(err, utils.extend(JSON.parse(JSON.stringify(data)), { writable: true, writeId: null })); + }); }); }); }); @@ -43,7 +55,7 @@ function createView(padId, data, callback) { if(err) return callback(err); - listeners.notifyPadListeners(data._pad, "view", data); + listeners.notifyPadListeners(data.PadId, "view", data); callback(null, data); }); } @@ -56,7 +68,7 @@ function updateView(viewId, data, callback) { if(err) return callback(err); - listeners.notifyPadListeners(data._pad, "view", data); + listeners.notifyPadListeners(data.PadId, "view", data); callback(null, data); }); } @@ -66,7 +78,47 @@ function deleteView(viewId, callback) { if(err) return callback(err); - listeners.notifyPadListeners(data._pad, "deleteView", { id: data.id }); + listeners.notifyPadListeners(data.PadId, "deleteView", { id: data.id }); + callback(null, data); + }); +} + +function getTypes(padId) { + return backend.getTypes(padId); +} + +function createType(padId, data, callback) { + if(data.name == null || data.name.trim().length == 0) + return callback("No name provided."); + + backend.createType(padId, data, function(err, data) { + if(err) + return callback(err); + + listeners.notifyPadListeners(data.PadId, "type", data); + callback(null, data); + }); +} + +function updateType(typeId, data, callback) { + if(data.name == null || data.name.trim().length == 0) + return callback("No name provided."); + + backend.updateType(typeId, data, function(err, data) { + if(err) + return callback(err); + + listeners.notifyPadListeners(data.PadId, "type", data); + callback(null, data); + }); +} + +function deleteType(typeId, callback) { + backend.deleteView(typeId, function(err, data) { + if(err) + return callback(err); + + listeners.notifyPadListeners(data.PadId, "deleteType", { id: data.id }); callback(null, data); }); } @@ -90,7 +142,7 @@ function updateMarker(markerId, data, callback) { if(err) return callback(err); - listeners.notifyPadListeners(data._pad, "marker", _getMarkerDataFunc(data)); + listeners.notifyPadListeners(data.PadId, "marker", _getMarkerDataFunc(data)); callback(null, data); }); } @@ -100,7 +152,7 @@ function deleteMarker(markerId, callback) { if(err) return callback(err); - listeners.notifyPadListeners(data._pad, "deleteMarker", { id: data.id }); + listeners.notifyPadListeners(data.PadId, "deleteMarker", { id: data.id }); callback(null, data); }); } @@ -147,7 +199,7 @@ function updateLine(lineId, data, callback) { if(!res.calculateRouting) return next(); - _setLinePoints(res.originalLine._pad, lineId, res.calculateRouting, next); + _setLinePoints(res.originalLine.PadId, lineId, res.calculateRouting, next); } ] }, function(err, res) { callback(err, res.updateLine); @@ -159,7 +211,7 @@ function _createLine(padId, data, callback) { if(err) return callback(err); - listeners.notifyPadListeners(data._pad, "line", data); + listeners.notifyPadListeners(data.PadId, "line", data); callback(null, data); }); } @@ -169,7 +221,7 @@ function _updateLine(lineId, data, callback) { if(err) return callback(err); - listeners.notifyPadListeners(data._pad, "line", data); + listeners.notifyPadListeners(data.PadId, "line", data); callback(null, data); }); } @@ -196,7 +248,7 @@ function deleteLine(lineId, callback) { if(err) return callback; - listeners.notifyPadListeners(data._pad, "deleteLine", { id: data.id }); + listeners.notifyPadListeners(data.PadId, "deleteLine", { id: data.id }); callback(null, data); }); }); @@ -216,7 +268,7 @@ function getLinePoints(padId, bboxWithZoom) { }); } -function copyPad(fromPadId, toPadId, callback) { +/*function copyPad(fromPadId, toPadId, callback) { function _handleStream(stream, next, cb) { stream.on("data", function(data) { stream.pause(); @@ -280,7 +332,7 @@ function copyPad(fromPadId, toPadId, callback) { }); }] }, callback); -} +}*/ function _calculateRouting(line, callback) { if(line.points && line.points.length >= 2 && line.mode) { @@ -308,7 +360,7 @@ function _calculateRouting(line, callback) { function _getMarkerDataFunc(marker) { return function(bbox) { - if(!utils.isInBbox(marker.position, bbox)) + if(!utils.isInBbox(marker, bbox)) return null; return marker; @@ -347,6 +399,10 @@ module.exports = { createView : createView, updateView : updateView, deleteView : deleteView, + getTypes : getTypes, + createType : createType, + updateType : updateType, + deleteType : deleteType, getPadMarkers : getPadMarkers, createMarker : createMarker, updateMarker : updateMarker, @@ -356,5 +412,6 @@ module.exports = { updateLine : updateLine, deleteLine : deleteLine, getLinePoints : getLinePoints, - copyPad : copyPad + //copyPad : copyPad, + _defaultTypes : DEFAULT_TYPES }; \ No newline at end of file diff --git a/server/lib/databaseBackendMongodb.js b/server/lib/databaseBackendMongodb.js deleted file mode 100644 index 7e481ab3..00000000 --- a/server/lib/databaseBackendMongodb.js +++ /dev/null @@ -1,305 +0,0 @@ -var mongoose = require("mongoose"); -var config = require("../config"); -var utils = require("./utils"); - -var OLD_MARKER_COLOURS = { blue: "8da8f6", green: "90ee90", gold: "ffd700", red: "ff0000" }; - -function connect(callback) { - var connectionString = "mongodb://" - + (config.db.user ? encodeURIComponent(config.db.user) + ":" + encodeURIComponent(config.db.password) + "@" : "") - + config.db.host - + (config.db.port ? ":" + config.db.port : "") - + "/" + config.db.database; - mongoose.connect(connectionString); - callback(); -} - -var ObjectId = mongoose.Schema.Types.ObjectId; - -var positionType = { - lon: Number, - lat: Number -}; - -var bboxType = { - top: Number, - right: Number, - bottom: Number, - left: Number -}; - -var markerSchema = mongoose.Schema({ - _pad : { type: String, ref: "Pad" }, - position : positionType, - name : { type: String, default: "Untitled marker" }, - description : String, - colour : { type: String, default: "ff0000" } -}); - -var lineSchema = mongoose.Schema({ - _pad : { type: String, ref: "Pad" }, - points : [positionType], - mode : { type: String, default: "" }, - colour : { type: String, default: "0000ff" }, - width : { type: Number, default: 4 }, - description : String, - name : { type: String, default: "Untitled line" }, - distance : Number, - time : Number -}); - -var linePointsSchema = mongoose.Schema(utils.extend({ }, positionType, { - zoom : Number, - idx : Number, - _line : { type: ObjectId, ref: "Line" } -})); - -linePointsSchema.index({ _id: 1, idx: 1 }); - -var viewSchema = mongoose.Schema({ - _pad : { type: String, ref: "Pad" }, - name : String, - baseLayer : String, - layers : [String], - view : bboxType -}); - -var padSchema = mongoose.Schema({ - _id : String, - defaultView : { type: ObjectId, ref: "View" }, - name: { type: String, default: "New FacilPad" }, - writeId : String -}); - -padSchema.index({ writeId: 1 }); - -var Marker = mongoose.model("Marker", markerSchema); -var Line = mongoose.model("Line", lineSchema); -var LinePoints = mongoose.model("LinePoints", linePointsSchema); -var View = mongoose.model("View", viewSchema); -var Pad = mongoose.model("Pad", padSchema); - -function getPadData(padId, callback) { - Pad.findById(padId).populate("defaultView").exec(_fixIdCallback(callback)); -} - -function getPadDataByWriteId(writeId, callback) { - Pad.findOne({ writeId: writeId }).populate("defaultView").exec(_fixIdCallback(callback)); -} - -function createPad(padId, writeId, callback) { - Pad.create({ _id: padId, writeId: writeId }, _fixIdCallback(callback)); -} - -function updatePadData(padId, data, callback) { - Pad.findByIdAndUpdate(padId, data).populate("defaultView").exec(_fixIdCallback(callback)); -} - -function getViews(padId) { - return _fixIdStream(View.find({ "_pad" : padId }).stream()); -} - -function createView(padId, data, callback) { - data._pad = padId; - View.create(data, _fixIdCallback(callback)); -} - -function updateView(viewId, data, callback) { - View.findByIdAndUpdate(viewId, data, _fixIdCallback(callback)); -} - -function deleteView(viewId, callback) { - View.findByIdAndRemove(viewId, _fixIdCallback(callback)); -} - -function getPadMarkers(padId, bbox) { - var condition = { $and: [ _makeBboxCondition(bbox, "position."), { - "_pad" : padId - } ] }; - - return _fixIdStream(Marker.find(condition).stream()); -} - -function createMarker(padId, data, callback) { - data._pad = padId; - Marker.create(data, _fixIdCallback(callback)); -} - -function updateMarker(markerId, data, callback) { - Marker.findByIdAndUpdate(markerId, data, _fixIdCallback(callback)); -} - -function deleteMarker(markerId, callback) { - Marker.findByIdAndRemove(markerId, _fixIdCallback(callback)); -} - -function getPadLines(padId, fields) { - var condition = { - "_pad" : padId - }; - - return _fixIdStream(Line.find(condition, fields).stream()); -} - -function getLine(lineId, callback) { - Line.findById(lineId, _fixIdCallback(callback)); -} - -function createLine(padId, data, callback) { - data = utils.extend({ }, data, { _pad: padId }); - Line.create(data, _fixIdCallback(callback)); -} - -function updateLine(lineId, data, callback) { - Line.findByIdAndUpdate(lineId, data, _fixIdCallback(callback)); -} - -function deleteLine(lineId, callback) { - Line.findByIdAndRemove(lineId, _fixIdCallback(callback)); -} - -function getLinePoints(lineId, callback) { - LinePoints.find({ _line: lineId }).sort("idx").exec(_linePointsCallback(callback)); -} - -function getLinePointsByBbox(lineId, bboxWithZoom, callback) { - var condition = { $and: [ _makeBboxCondition(bboxWithZoom), { - "_line" : lineId, - "zoom" : { $lte: bboxWithZoom.zoom } - } ] }; - - LinePoints.find(condition, "idx zoom", { sort: "idx" }).exec(_linePointsCallback(callback)); -} - -function getLinePointsByIdx(lineId, indexes, callback) { - LinePoints.find({ _line: lineId, idx: { $in: indexes } }).select("lon lat idx").sort("idx").exec(_linePointsCallback(callback)); -} - -function setLinePoints(lineId, points, callback) { - LinePoints.remove({ _line: lineId }, function(err) { - if(err) - return callback(err); - - var create = [ ]; - for(var i=0; i 0) - LinePoints.create(create, callback); - else - callback(); - }); -} - -function _fixId(data) { - if(data != null) { - data = JSON.parse(JSON.stringify(data)); - data.id = data._id; - delete data._id; - - if(data.writeId) - delete data.writeId; - - if(data.defaultView) { - data.defaultView.id = data.defaultView._id; - delete data.defaultView._id; - } - - if(data.points) { - for(var i=0; i", "version": "0.0.1", "dependencies": { - "mongoose": "x.x.x", + "sequelize": "x.x.x", "socket.io": "x.x.x", "async" : "x.x.x", "request" : "x.x.x",