2019-05-22 11:28:57 +00:00
"use strict" ;
class ReadingRepo {
2019-07-09 13:05:42 +00:00
constructor ( { database , RSSIRepo } ) {
this . db = database ;
this . RSSIRepo = RSSIRepo ;
2019-05-22 17:04:46 +00:00
}
2019-05-23 21:48:51 +00:00
add ( reading ) {
2019-07-10 13:32:58 +00:00
const statement = this . db . prepare ( ` INSERT INTO readings (
id ,
latitude , longitude ,
data _rate , code _rate
) VALUES (
: id ,
: latitude ,
: longitude ,
: data _rate , : code _rate
) ; ` );
2019-05-29 10:35:10 +00:00
2019-07-10 13:32:58 +00:00
let new _id = statement . run ( reading ) . lastInsertRowid ;
2019-05-29 10:35:10 +00:00
2019-07-10 11:21:38 +00:00
// Attach the new id to the rssi objects
for ( let rssi of reading . rssis )
rssi . reading _id = new _id ;
// Insert the RSSIs into the database
2019-05-29 10:35:10 +00:00
this . RSSIRepo . add ( ... reading . rssis ) ;
}
2019-07-11 13:03:27 +00:00
exists ( id ) {
// Note that normally a count wouldn't be appropriate here just to test for existence, but it's ok because it's guaranteed that there will only ever be a single entry with a given id.
let statement = this . db . prepare ( ` SELECT COUNT(id) AS count
FROM readings
WHERE id = : id ` );
let count = statement . get ( { id } ) . count ;
return count > 0 ;
}
2019-05-29 10:35:10 +00:00
iterate ( ) {
return this . db . prepare ( ` SELECT * FROM readings ` ) . iterate ( ) ;
2019-05-22 11:28:57 +00:00
}
}
export default ReadingRepo ;