kopia lustrzana https://github.com/FacilMap/facilmap
Clean up db code: No more *WithData() functions
rodzic
1ff7e03d01
commit
cae8b16eb4
|
@ -121,13 +121,41 @@ module.exports = function(Database) {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
_padObjectExists(type, padId, id) {
|
||||||
|
return this._conn.model(type).count({ where: { padId: padId, id: id }, limit: 1 }).then(num => num > 0);
|
||||||
|
},
|
||||||
|
|
||||||
|
_getPadObject(type, padId, id) {
|
||||||
|
var includeData = [ "Marker", "Line" ].includes(type);
|
||||||
|
|
||||||
|
var cond = { where: { id: id, padId: padId }, include: includeData ? [ this._conn.model(type + "Data") ] : [ ] };
|
||||||
|
return this._conn.model(type).findOne(cond).then(data => {
|
||||||
|
if(data == null)
|
||||||
|
throw new Error(type + " " + id + " of pad " + padId + " could not be found.");
|
||||||
|
|
||||||
|
if(includeData) {
|
||||||
|
data.data = this._dataFromArr(data[type+"Data"]);
|
||||||
|
data.setDataValue("data", data.data); // For JSON.stringify()
|
||||||
|
data.setDataValue(type+"Data", undefined);
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
_getPadObjects(type, padId, condition) {
|
_getPadObjects(type, padId, condition) {
|
||||||
|
var includeData = [ "Marker", "Line" ].includes(type);
|
||||||
|
|
||||||
|
if(includeData) {
|
||||||
|
condition = condition || { };
|
||||||
|
condition.include = [ ...(condition.include || [ ]), this._conn.model(type + "Data") ];
|
||||||
|
}
|
||||||
|
|
||||||
var ret = new utils.ArrayStream();
|
var ret = new utils.ArrayStream();
|
||||||
|
|
||||||
var o = this._conn.model("Pad").build({ id: padId });
|
this._conn.model("Pad").build({ id: padId })["get" + this._conn.model(type).getTableName()](condition).then((objs) => {
|
||||||
this._conn.model("Pad").build({ id: padId })["get"+type+"s"](condition).then((objs) => {
|
|
||||||
objs.forEach((it) => {
|
objs.forEach((it) => {
|
||||||
if(it[type+"Data"] != null) {
|
if(includeData) {
|
||||||
it.data = this._dataFromArr(it[type+"Data"]);
|
it.data = this._dataFromArr(it[type+"Data"]);
|
||||||
it.setDataValue("data", it.data); // For JSON.stringify()
|
it.setDataValue("data", it.data); // For JSON.stringify()
|
||||||
it.setDataValue(type+"Data", undefined);
|
it.setDataValue(type+"Data", undefined);
|
||||||
|
@ -142,63 +170,70 @@ module.exports = function(Database) {
|
||||||
},
|
},
|
||||||
|
|
||||||
_createPadObject(type, padId, data) {
|
_createPadObject(type, padId, data) {
|
||||||
var obj = this._conn.model(type).build(data);
|
var includeData = [ "Marker", "Line" ].includes(type);
|
||||||
obj.padId = padId;
|
|
||||||
return obj.save();
|
|
||||||
},
|
|
||||||
|
|
||||||
_createPadObjectWithData(type, padId, data) {
|
return utils.promiseAuto({
|
||||||
return this._createPadObject(type, padId, data).then((obj) => {
|
create: () => {
|
||||||
if(data.data != null) {
|
var obj = this._conn.model(type).build(data);
|
||||||
obj.data = data.data;
|
obj.padId = padId;
|
||||||
obj.setDataValue("data", obj.data); // For JSON.stringify()
|
return obj.save();
|
||||||
return this._setObjectData(type, obj.id, data.data).then(() => {
|
},
|
||||||
return obj;
|
data: (create) => {
|
||||||
});
|
if(includeData) {
|
||||||
} else {
|
create.data = data.data || { };
|
||||||
obj.data = { };
|
create.setDataValue("data", create.data); // For JSON.stringify()
|
||||||
obj.setDataValue("data", obj.data); // For JSON.stringify()
|
|
||||||
return obj;
|
if(data.data != null)
|
||||||
|
return this._setObjectData(type, create.id, data.data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
}).then(res => res.create);
|
||||||
},
|
},
|
||||||
|
|
||||||
_updatePadObject(type, padId, objId, data) {
|
_updatePadObject(type, padId, objId, data) {
|
||||||
return this._conn.model(type).update(data, { where: { id: objId, padId: padId } }).then((res) => {
|
var includeData = [ "Marker", "Line" ].includes(type);
|
||||||
if(res[0] == 0)
|
|
||||||
throw new Error(type + " " + objId + " of pad " + padId + "could not be found.");
|
|
||||||
|
|
||||||
return this._conn.model(type).findById(objId);
|
return utils.promiseAuto({
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
_updatePadObjectWithData(type, padId, objId, data) {
|
update: () => {
|
||||||
return Promise.all([
|
return this._conn.model(type).update(data, { where: { id: objId, padId: padId } }).then((res) => {
|
||||||
this._updatePadObject(type, padId, objId, data),
|
if(res[0] == 0)
|
||||||
data.data != null ? this._setObjectData(type, objId, data.data) : this._getObjectData(type, objId)
|
throw new Error(type + " " + objId + " of pad " + padId + "could not be found.");
|
||||||
]).then((results) => {
|
});
|
||||||
var obj = results[0];
|
},
|
||||||
obj.data = (data.data != null ? data.data : results[1]);
|
|
||||||
obj.setDataValue("data", obj.data); // For JSON.stringify()
|
newData: (update) => {
|
||||||
return obj;
|
return this._getPadObject(type, padId, objId);
|
||||||
});
|
},
|
||||||
|
|
||||||
|
updateData: (newData) => {
|
||||||
|
if(includeData) {
|
||||||
|
return (data.data != null ? this._setObjectData(type, objId, data.data) : this._getObjectData(type, objId)).then((dataData) => {
|
||||||
|
newData.data = (data.data != null ? data.data : dataData);
|
||||||
|
return newData.setDataValue("data", newData.data); // For JSON.stringify()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).then(res => res.newData);
|
||||||
},
|
},
|
||||||
|
|
||||||
_deletePadObject(type, padId, objId) {
|
_deletePadObject(type, padId, objId) {
|
||||||
return this._conn.model(type).findOne({ where: { id: objId, padId: padId }}).then((obj) => {
|
var includeData = [ "Marker", "Line" ].includes(type);
|
||||||
if(obj == null)
|
|
||||||
throw new Error(type + " " + objId + " of pad " + padId + " could not be found.");
|
|
||||||
|
|
||||||
return obj.destroy().then(() => {
|
return utils.promiseAuto({
|
||||||
return obj;
|
oldData: () => {
|
||||||
});
|
return this._getPadObject(type, padId, objId);
|
||||||
});
|
},
|
||||||
},
|
|
||||||
|
|
||||||
_deletePadObjectWithData(type, padId, objId) {
|
destroyData: (oldData) => {
|
||||||
return this._setObjectData(type, objId, { }).then(() => {
|
if(includeData)
|
||||||
return this._deletePadObject(type, padId, objId); // Return the object
|
return this._setObjectData(type, objId, { });
|
||||||
});
|
},
|
||||||
|
|
||||||
|
destroy: (oldData, destroyData) => {
|
||||||
|
return oldData.destroy();
|
||||||
|
}
|
||||||
|
}).then(res => res.oldData);
|
||||||
},
|
},
|
||||||
|
|
||||||
_dataToArr(data, extend) {
|
_dataToArr(data, extend) {
|
||||||
|
|
|
@ -71,15 +71,12 @@ module.exports = function(Database) {
|
||||||
|
|
||||||
utils.extend(Database.prototype, {
|
utils.extend(Database.prototype, {
|
||||||
getPadLines(padId, fields) {
|
getPadLines(padId, fields) {
|
||||||
var cond = { include: [ this._conn.model("LineData") ] };
|
var cond = fields ? { attributes: (typeof fields == "string" ? fields.split(/\s+/) : fields) } : { };
|
||||||
if(fields)
|
|
||||||
cond.attributes = (typeof fields == "string" ? fields.split(/\s+/) : fields);
|
|
||||||
|
|
||||||
return this._getPadObjects("Line", padId, cond);
|
return this._getPadObjects("Line", padId, cond);
|
||||||
},
|
},
|
||||||
|
|
||||||
getPadLinesByType(padId, typeId) {
|
getPadLinesByType(padId, typeId) {
|
||||||
return this._getPadObjects("Line", padId, { where: { typeId: typeId }, include: [ this._conn.model("LineData") ] });
|
return this._getPadObjects("Line", padId, { where: { typeId: typeId } });
|
||||||
},
|
},
|
||||||
|
|
||||||
getPadLinesWithPoints(padId, bboxWithZoom) {
|
getPadLinesWithPoints(padId, bboxWithZoom) {
|
||||||
|
@ -117,12 +114,7 @@ module.exports = function(Database) {
|
||||||
},
|
},
|
||||||
|
|
||||||
getLine(padId, lineId) {
|
getLine(padId, lineId) {
|
||||||
return this._conn.model("Line").findOne({ where: { id: lineId, padId: padId }, include: [ this._conn.model("LineData") ] }).then(line => {
|
return this._getPadObject("Line", padId, lineId);
|
||||||
if(line == null)
|
|
||||||
throw new Error("Line " + lineId + " of pad " + padId + " could not be found.");
|
|
||||||
|
|
||||||
return line;
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
createLine(padId, data) {
|
createLine(padId, data) {
|
||||||
|
@ -144,7 +136,7 @@ module.exports = function(Database) {
|
||||||
var dataCopy = utils.extend({ }, data);
|
var dataCopy = utils.extend({ }, data);
|
||||||
delete dataCopy.trackPoints; // They came if mode is track
|
delete dataCopy.trackPoints; // They came if mode is track
|
||||||
|
|
||||||
return this._createPadObjectWithData("Line", padId, dataCopy);
|
return this._createPadObject("Line", padId, dataCopy);
|
||||||
},
|
},
|
||||||
|
|
||||||
lineEvent: (createLine) => {
|
lineEvent: (createLine) => {
|
||||||
|
@ -181,7 +173,7 @@ module.exports = function(Database) {
|
||||||
var dataCopy = utils.extend({ }, data);
|
var dataCopy = utils.extend({ }, data);
|
||||||
delete dataCopy.trackPoints; // They came if mode is track
|
delete dataCopy.trackPoints; // They came if mode is track
|
||||||
|
|
||||||
return this._updatePadObjectWithData("Line", padId, lineId, dataCopy);
|
return this._updatePadObject("Line", padId, lineId, dataCopy);
|
||||||
},
|
},
|
||||||
|
|
||||||
updateStyle: (newLine) => {
|
updateStyle: (newLine) => {
|
||||||
|
@ -216,7 +208,7 @@ module.exports = function(Database) {
|
||||||
|
|
||||||
deleteLine(padId, lineId) {
|
deleteLine(padId, lineId) {
|
||||||
return utils.promiseAuto({
|
return utils.promiseAuto({
|
||||||
line: this._deletePadObjectWithData("Line", padId, lineId),
|
line: this._deletePadObject("Line", padId, lineId),
|
||||||
points: this._setLinePoints(padId, lineId, [ ], true)
|
points: this._setLinePoints(padId, lineId, [ ], true)
|
||||||
}).then((res) => {
|
}).then((res) => {
|
||||||
this.emit("deleteLine", padId, { id: lineId });
|
this.emit("deleteLine", padId, { id: lineId });
|
||||||
|
|
|
@ -30,11 +30,11 @@ module.exports = function(Database) {
|
||||||
|
|
||||||
utils.extend(Database.prototype, {
|
utils.extend(Database.prototype, {
|
||||||
getPadMarkers(padId, bbox) {
|
getPadMarkers(padId, bbox) {
|
||||||
return this._getPadObjects("Marker", padId, { where: this._makeBboxCondition(bbox), include: [ this._conn.model("MarkerData") ] });
|
return this._getPadObjects("Marker", padId, { where: this._makeBboxCondition(bbox) });
|
||||||
},
|
},
|
||||||
|
|
||||||
getPadMarkersByType(padId, typeId) {
|
getPadMarkersByType(padId, typeId) {
|
||||||
return this._getPadObjects("Marker", padId, { where: { padId: padId, typeId: typeId }, include: [ this._conn.model("MarkerData") ] });
|
return this._getPadObjects("Marker", padId, { where: { padId: padId, typeId: typeId } });
|
||||||
},
|
},
|
||||||
|
|
||||||
createMarker(padId, data) {
|
createMarker(padId, data) {
|
||||||
|
@ -48,7 +48,7 @@ module.exports = function(Database) {
|
||||||
if(type.defaultSymbol)
|
if(type.defaultSymbol)
|
||||||
data.symbol = type.defaultSymbol;
|
data.symbol = type.defaultSymbol;
|
||||||
|
|
||||||
return this._createPadObjectWithData("Marker", padId, data);
|
return this._createPadObject("Marker", padId, data);
|
||||||
},
|
},
|
||||||
styles: (create) => {
|
styles: (create) => {
|
||||||
return this._updateObjectStyles(create, false)
|
return this._updateObjectStyles(create, false)
|
||||||
|
@ -62,7 +62,7 @@ module.exports = function(Database) {
|
||||||
|
|
||||||
updateMarker(padId, markerId, data, doNotUpdateStyles) {
|
updateMarker(padId, markerId, data, doNotUpdateStyles) {
|
||||||
return utils.promiseAuto({
|
return utils.promiseAuto({
|
||||||
update: this._updatePadObjectWithData("Marker", padId, markerId, data),
|
update: this._updatePadObject("Marker", padId, markerId, data),
|
||||||
updateStyles: (update) => {
|
updateStyles: (update) => {
|
||||||
if(!doNotUpdateStyles)
|
if(!doNotUpdateStyles)
|
||||||
return this._updateObjectStyles(update, false);
|
return this._updateObjectStyles(update, false);
|
||||||
|
@ -75,7 +75,7 @@ module.exports = function(Database) {
|
||||||
},
|
},
|
||||||
|
|
||||||
deleteMarker(padId, markerId) {
|
deleteMarker(padId, markerId) {
|
||||||
this._deletePadObjectWithData("Marker", padId, markerId).then(del => {
|
return this._deletePadObject("Marker", padId, markerId).then(del => {
|
||||||
this.emit("deleteMarker", padId, { id: del.id });
|
this.emit("deleteMarker", padId, { id: del.id });
|
||||||
|
|
||||||
return del;
|
return del;
|
||||||
|
|
|
@ -47,12 +47,12 @@ module.exports = function(Database) {
|
||||||
|
|
||||||
var addColMigrations = renameColMigrations.then(() => {
|
var addColMigrations = renameColMigrations.then(() => {
|
||||||
return Promise.all([ 'Marker', 'Type' ].map((table) => {
|
return Promise.all([ 'Marker', 'Type' ].map((table) => {
|
||||||
queryInterface.describeTable(table+"s").then((attributes) => {
|
var model = this._conn.model(table);
|
||||||
|
queryInterface.describeTable(model.getTableName()).then((attributes) => {
|
||||||
var promises = [ ];
|
var promises = [ ];
|
||||||
var model = this._conn.model(table);
|
|
||||||
for(var attribute in model.attributes) {
|
for(var attribute in model.attributes) {
|
||||||
if(!attributes[attribute])
|
if(!attributes[attribute])
|
||||||
promises.push(queryInterface.addColumn(table+"s", attribute, model.attributes[attribute]));
|
promises.push(queryInterface.addColumn(model.getTableName(), attribute, model.attributes[attribute]));
|
||||||
}
|
}
|
||||||
return Promise.all(promises);
|
return Promise.all(promises);
|
||||||
});
|
});
|
||||||
|
|
|
@ -103,12 +103,7 @@ module.exports = function(Database) {
|
||||||
},
|
},
|
||||||
|
|
||||||
getType(padId, typeId) {
|
getType(padId, typeId) {
|
||||||
return this._conn.model("Type").findOne({ where: { id: typeId, padId: padId } }).then(res => {
|
return this._getPadObject("Type", padId, typeId);
|
||||||
if(res == null)
|
|
||||||
throw new Error("Type " + typeId + " of pad " + padId + " could not be found.");
|
|
||||||
|
|
||||||
return res;
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
createType(padId, data) {
|
createType(padId, data) {
|
||||||
|
|
Ładowanie…
Reference in New Issue