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) {
|
||||
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 o = this._conn.model("Pad").build({ id: padId });
|
||||
this._conn.model("Pad").build({ id: padId })["get"+type+"s"](condition).then((objs) => {
|
||||
this._conn.model("Pad").build({ id: padId })["get" + this._conn.model(type).getTableName()](condition).then((objs) => {
|
||||
objs.forEach((it) => {
|
||||
if(it[type+"Data"] != null) {
|
||||
if(includeData) {
|
||||
it.data = this._dataFromArr(it[type+"Data"]);
|
||||
it.setDataValue("data", it.data); // For JSON.stringify()
|
||||
it.setDataValue(type+"Data", undefined);
|
||||
|
@ -142,63 +170,70 @@ module.exports = function(Database) {
|
|||
},
|
||||
|
||||
_createPadObject(type, padId, data) {
|
||||
var obj = this._conn.model(type).build(data);
|
||||
obj.padId = padId;
|
||||
return obj.save();
|
||||
},
|
||||
var includeData = [ "Marker", "Line" ].includes(type);
|
||||
|
||||
_createPadObjectWithData(type, padId, data) {
|
||||
return this._createPadObject(type, padId, data).then((obj) => {
|
||||
if(data.data != null) {
|
||||
obj.data = data.data;
|
||||
obj.setDataValue("data", obj.data); // For JSON.stringify()
|
||||
return this._setObjectData(type, obj.id, data.data).then(() => {
|
||||
return obj;
|
||||
});
|
||||
} else {
|
||||
obj.data = { };
|
||||
obj.setDataValue("data", obj.data); // For JSON.stringify()
|
||||
return obj;
|
||||
return utils.promiseAuto({
|
||||
create: () => {
|
||||
var obj = this._conn.model(type).build(data);
|
||||
obj.padId = padId;
|
||||
return obj.save();
|
||||
},
|
||||
data: (create) => {
|
||||
if(includeData) {
|
||||
create.data = data.data || { };
|
||||
create.setDataValue("data", create.data); // For JSON.stringify()
|
||||
|
||||
if(data.data != null)
|
||||
return this._setObjectData(type, create.id, data.data);
|
||||
}
|
||||
}
|
||||
});
|
||||
}).then(res => res.create);
|
||||
},
|
||||
|
||||
_updatePadObject(type, padId, objId, data) {
|
||||
return this._conn.model(type).update(data, { where: { id: objId, padId: padId } }).then((res) => {
|
||||
if(res[0] == 0)
|
||||
throw new Error(type + " " + objId + " of pad " + padId + "could not be found.");
|
||||
var includeData = [ "Marker", "Line" ].includes(type);
|
||||
|
||||
return this._conn.model(type).findById(objId);
|
||||
});
|
||||
},
|
||||
return utils.promiseAuto({
|
||||
|
||||
_updatePadObjectWithData(type, padId, objId, data) {
|
||||
return Promise.all([
|
||||
this._updatePadObject(type, padId, objId, data),
|
||||
data.data != null ? this._setObjectData(type, objId, data.data) : this._getObjectData(type, objId)
|
||||
]).then((results) => {
|
||||
var obj = results[0];
|
||||
obj.data = (data.data != null ? data.data : results[1]);
|
||||
obj.setDataValue("data", obj.data); // For JSON.stringify()
|
||||
return obj;
|
||||
});
|
||||
update: () => {
|
||||
return this._conn.model(type).update(data, { where: { id: objId, padId: padId } }).then((res) => {
|
||||
if(res[0] == 0)
|
||||
throw new Error(type + " " + objId + " of pad " + padId + "could not be found.");
|
||||
});
|
||||
},
|
||||
|
||||
newData: (update) => {
|
||||
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) {
|
||||
return this._conn.model(type).findOne({ where: { id: objId, padId: padId }}).then((obj) => {
|
||||
if(obj == null)
|
||||
throw new Error(type + " " + objId + " of pad " + padId + " could not be found.");
|
||||
var includeData = [ "Marker", "Line" ].includes(type);
|
||||
|
||||
return obj.destroy().then(() => {
|
||||
return obj;
|
||||
});
|
||||
});
|
||||
},
|
||||
return utils.promiseAuto({
|
||||
oldData: () => {
|
||||
return this._getPadObject(type, padId, objId);
|
||||
},
|
||||
|
||||
_deletePadObjectWithData(type, padId, objId) {
|
||||
return this._setObjectData(type, objId, { }).then(() => {
|
||||
return this._deletePadObject(type, padId, objId); // Return the object
|
||||
});
|
||||
destroyData: (oldData) => {
|
||||
if(includeData)
|
||||
return this._setObjectData(type, objId, { });
|
||||
},
|
||||
|
||||
destroy: (oldData, destroyData) => {
|
||||
return oldData.destroy();
|
||||
}
|
||||
}).then(res => res.oldData);
|
||||
},
|
||||
|
||||
_dataToArr(data, extend) {
|
||||
|
|
|
@ -71,15 +71,12 @@ module.exports = function(Database) {
|
|||
|
||||
utils.extend(Database.prototype, {
|
||||
getPadLines(padId, fields) {
|
||||
var cond = { include: [ this._conn.model("LineData") ] };
|
||||
if(fields)
|
||||
cond.attributes = (typeof fields == "string" ? fields.split(/\s+/) : fields);
|
||||
|
||||
var cond = fields ? { attributes: (typeof fields == "string" ? fields.split(/\s+/) : fields) } : { };
|
||||
return this._getPadObjects("Line", padId, cond);
|
||||
},
|
||||
|
||||
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) {
|
||||
|
@ -117,12 +114,7 @@ module.exports = function(Database) {
|
|||
},
|
||||
|
||||
getLine(padId, lineId) {
|
||||
return this._conn.model("Line").findOne({ where: { id: lineId, padId: padId }, include: [ this._conn.model("LineData") ] }).then(line => {
|
||||
if(line == null)
|
||||
throw new Error("Line " + lineId + " of pad " + padId + " could not be found.");
|
||||
|
||||
return line;
|
||||
});
|
||||
return this._getPadObject("Line", padId, lineId);
|
||||
},
|
||||
|
||||
createLine(padId, data) {
|
||||
|
@ -144,7 +136,7 @@ module.exports = function(Database) {
|
|||
var dataCopy = utils.extend({ }, data);
|
||||
delete dataCopy.trackPoints; // They came if mode is track
|
||||
|
||||
return this._createPadObjectWithData("Line", padId, dataCopy);
|
||||
return this._createPadObject("Line", padId, dataCopy);
|
||||
},
|
||||
|
||||
lineEvent: (createLine) => {
|
||||
|
@ -181,7 +173,7 @@ module.exports = function(Database) {
|
|||
var dataCopy = utils.extend({ }, data);
|
||||
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) => {
|
||||
|
@ -216,7 +208,7 @@ module.exports = function(Database) {
|
|||
|
||||
deleteLine(padId, lineId) {
|
||||
return utils.promiseAuto({
|
||||
line: this._deletePadObjectWithData("Line", padId, lineId),
|
||||
line: this._deletePadObject("Line", padId, lineId),
|
||||
points: this._setLinePoints(padId, lineId, [ ], true)
|
||||
}).then((res) => {
|
||||
this.emit("deleteLine", padId, { id: lineId });
|
||||
|
|
|
@ -30,11 +30,11 @@ module.exports = function(Database) {
|
|||
|
||||
utils.extend(Database.prototype, {
|
||||
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) {
|
||||
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) {
|
||||
|
@ -48,7 +48,7 @@ module.exports = function(Database) {
|
|||
if(type.defaultSymbol)
|
||||
data.symbol = type.defaultSymbol;
|
||||
|
||||
return this._createPadObjectWithData("Marker", padId, data);
|
||||
return this._createPadObject("Marker", padId, data);
|
||||
},
|
||||
styles: (create) => {
|
||||
return this._updateObjectStyles(create, false)
|
||||
|
@ -62,7 +62,7 @@ module.exports = function(Database) {
|
|||
|
||||
updateMarker(padId, markerId, data, doNotUpdateStyles) {
|
||||
return utils.promiseAuto({
|
||||
update: this._updatePadObjectWithData("Marker", padId, markerId, data),
|
||||
update: this._updatePadObject("Marker", padId, markerId, data),
|
||||
updateStyles: (update) => {
|
||||
if(!doNotUpdateStyles)
|
||||
return this._updateObjectStyles(update, false);
|
||||
|
@ -75,7 +75,7 @@ module.exports = function(Database) {
|
|||
},
|
||||
|
||||
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 });
|
||||
|
||||
return del;
|
||||
|
|
|
@ -47,12 +47,12 @@ module.exports = function(Database) {
|
|||
|
||||
var addColMigrations = renameColMigrations.then(() => {
|
||||
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 model = this._conn.model(table);
|
||||
for(var attribute in model.attributes) {
|
||||
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);
|
||||
});
|
||||
|
|
|
@ -103,12 +103,7 @@ module.exports = function(Database) {
|
|||
},
|
||||
|
||||
getType(padId, typeId) {
|
||||
return this._conn.model("Type").findOne({ where: { id: typeId, padId: padId } }).then(res => {
|
||||
if(res == null)
|
||||
throw new Error("Type " + typeId + " of pad " + padId + " could not be found.");
|
||||
|
||||
return res;
|
||||
});
|
||||
return this._getPadObject("Type", padId, typeId);
|
||||
},
|
||||
|
||||
createType(padId, data) {
|
||||
|
|
Ładowanie…
Reference in New Issue