aboutsummaryrefslogtreecommitdiff
path: root/src/DillerDao.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/DillerDao.js')
-rw-r--r--src/DillerDao.js28
1 files changed, 21 insertions, 7 deletions
diff --git a/src/DillerDao.js b/src/DillerDao.js
index e7f6b54..63ad3c1 100644
--- a/src/DillerDao.js
+++ b/src/DillerDao.js
@@ -69,12 +69,27 @@ function DillerDao(tx) {
return tx.one('INSERT INTO device_property(id, device, key, created_timestamp) VALUES(DEFAULT, $1, $2, CURRENT_TIMESTAMP) RETURNING ' + propertyColumns, [deviceId, key]);
}
- function updatePropertyName(id, name) {
- return tx.none('UPDATE device_property SET name=$1 WHERE id=$2', [name, id]);
- }
+ function updateProperty(id, attributes) {
+ var values = [id];
+ var i = 2;
+ var fields = _(attributes).chain()
+ .map(function (value, attribute) {
+ if (attribute == 'name' || attribute == 'description') {
+ value = (value || '').trim();
+ values.push(value.length > 0 ? value : null);
+ return attribute + ' = $' + i++;
+ }
+ })
+ .collect()
+ .join(', ')
+ .value();
+
+ if (fields.length == 0) {
+ return Promise.resolve({});
+ }
- function updatePropertyDescription(id, description) {
- return tx.none('UPDATE device_property SET description=$1 WHERE id=$2', description, id);
+ var sql = 'UPDATE device_property SET ' + fields + ' WHERE id = $1 RETURNING *';
+ return tx.one(sql, values);
}
// -------------------------------------------------------------------------------------------------------------------
@@ -130,8 +145,7 @@ function DillerDao(tx) {
devicePropertyByDeviceIdAndKey: devicePropertyByDeviceIdAndKey,
devicePropertiesByDeviceId: devicePropertiesByDeviceId,
insertDeviceProperty: insertDeviceProperty,
- updatePropertyName: updatePropertyName,
- updatePropertyDescription: updatePropertyDescription,
+ updateProperty: updateProperty,
valuesByPropertyId: valuesByPropertyId,
insertValue: insertValue,