aboutsummaryrefslogtreecommitdiff
path: root/src/DillerDao.js
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-10-31 14:09:39 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2015-10-31 14:09:39 +0100
commit803148d5a23afe207fb5de9ac73c986a324feb9c (patch)
treeb887cede3f56e4e24c05dd9640dbd01afc2b9367 /src/DillerDao.js
parent02d6e77bd180cbbf6f7f6e1a69c670e922d8204d (diff)
downloaddiller-server-803148d5a23afe207fb5de9ac73c986a324feb9c.tar.gz
diller-server-803148d5a23afe207fb5de9ac73c986a324feb9c.tar.bz2
diller-server-803148d5a23afe207fb5de9ac73c986a324feb9c.tar.xz
diller-server-803148d5a23afe207fb5de9ac73c986a324feb9c.zip
core:
o Improved transaction handling. web: o Supporting changing a device's name and description.
Diffstat (limited to 'src/DillerDao.js')
-rw-r--r--src/DillerDao.js30
1 files changed, 14 insertions, 16 deletions
diff --git a/src/DillerDao.js b/src/DillerDao.js
index 2133dfc..a47f181 100644
--- a/src/DillerDao.js
+++ b/src/DillerDao.js
@@ -2,7 +2,7 @@ var _ = require('lodash');
function DillerDao(tx) {
- var deviceColumns = 'id, key, created_timestamp';
+ var deviceColumns = 'id, created_timestamp, key, name, description';
var propertyColumns = 'id, device, key, created_timestamp';
var valueColumns = 'property, timestamp, value_text, value_numeric';
@@ -27,27 +27,25 @@ function DillerDao(tx) {
}
function updateDevice(id, attributes) {
-
var values = [id];
var i = 2;
- var fields = _.map(attributes, function (value, name) {
- console.log('name', name, 'value', value, 'i', i);
- if (name == 'name') {
- values.push(value);
- return 'name = $' + i++;
- }
- });
+ var fields = _(attributes).chain()
+ .map(function (value, attribute) {
+ if (attribute == 'name' || attribute == 'description') {
+ values.push(value);
+ return attribute + ' = $' + i++;
+ }
+ })
+ .collect()
+ .join(', ')
+ .value();
if (fields.length == 0) {
- return; // TODO: return an empty promise;
+ return Promise.resolve({});
}
- fields = _.collect(fields);
-
- var x = 'UPDATE device SET ' + fields.join(', ') + ' WHERE id = $1';
- console.log('x', x);
- console.log('values', values);
- return tx.none(x, values);
+ var sql = 'UPDATE device SET ' + fields + ' WHERE id = $1';
+ return tx.none(sql, values);
}
// -------------------------------------------------------------------------------------------------------------------