diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-10-18 18:10:20 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-10-18 18:10:20 +0200 |
commit | 52eb8072664a61ea61dbdbef7485d6c81dbbcfe9 (patch) | |
tree | f4218bc276a0188da26da472d1233c264ff31dfe /src/DillerDao.js | |
parent | 9339e6269d6f5a5d0421c23c5e9ba3c8500fb535 (diff) | |
download | diller-server-52eb8072664a61ea61dbdbef7485d6c81dbbcfe9.tar.gz diller-server-52eb8072664a61ea61dbdbef7485d6c81dbbcfe9.tar.bz2 diller-server-52eb8072664a61ea61dbdbef7485d6c81dbbcfe9.tar.xz diller-server-52eb8072664a61ea61dbdbef7485d6c81dbbcfe9.zip |
o Dropping the app concept.
o Switching to pg-promise to not get overloaded by callbacks.
Diffstat (limited to 'src/DillerDao.js')
-rw-r--r-- | src/DillerDao.js | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/DillerDao.js b/src/DillerDao.js new file mode 100644 index 0000000..ed6fcf0 --- /dev/null +++ b/src/DillerDao.js @@ -0,0 +1,47 @@ +function DillerDao(client) { + + var deviceColumns = 'id, key, created_timestamp'; + var propertyColumns = 'id, device, key, created_timestamp'; + + function deviceByKey(key) { + return client.oneOrNone("SELECT " + deviceColumns + " FROM device WHERE key=$1", key); + } + + function insertDevice(key) { + return client.one("INSERT INTO device(id, key, created_timestamp) VALUES(DEFAULT, $1, CURRENT_TIMESTAMP) RETURNING " + deviceColumns, key); + } + + function devicePropertyByDeviceIdAndKey(deviceId, key) { + return client.oneOrNone('SELECT id FROM device_property WHERE device=$1 AND key=$2', [deviceId, key]); + } + + function insertDeviceProperty(deviceId, key) { + return client.oneOrNone('INSERT INTO device_property(id, device, key, created_timestamp) VALUES(DEFAULT, $1, $2, CURRENT_TIMESTAMP) RETURNING ' + propertyColumns, [deviceId, key]); + } + + function updatePropertyName(id, name) { + return client.none('UPDATE device_property SET name=$1 WHERE id=$2', name, id); + } + + function updatePropertyDescription(id, description) { + return client.none('UPDATE device_property SET description=$1 WHERE id=$2', description, id); + } + + function insertValue(propertyId, value) { + return client.none('INSERT INTO value(property, timestamp, value) VALUES($1, CURRENT_TIMESTAMP, $2)', [propertyId, value]); + } + + return { + deviceByKey: deviceByKey, + insertDevice: insertDevice, + + devicePropertyByDeviceIdAndKey: devicePropertyByDeviceIdAndKey, + insertDeviceProperty: insertDeviceProperty, + updatePropertyName: updatePropertyName, + updatePropertyDescription: updatePropertyDescription, + + insertValue: insertValue + } +} + +module.exports = DillerDao; |