LoRaWAN-Signal-Mapping/server/Repos.SQLite/ReadingRepo.mjs

52 lines
1.2 KiB
JavaScript
Raw Normal View History

"use strict";
class ReadingRepo {
constructor({ database, RSSIRepo }) {
this.db = database;
this.RSSIRepo = RSSIRepo;
}
add(reading) {
const statement = this.db.prepare(`INSERT INTO readings (
id,
latitude, longitude,
data_rate, code_rate
) VALUES (
:id,
:latitude,
:longitude,
:data_rate, :code_rate
);`);
let new_id = statement.run(reading).lastInsertRowid;
// 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
this.RSSIRepo.add(...reading.rssis);
}
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;
}
iterate_unreceived() {
return this.db.prepare(`SELECT * FROM readings WHERE data_rate IS NULL;`).iterate();
}
iterate() {
return this.db.prepare(`SELECT * FROM readings`).iterate();
}
}
export default ReadingRepo;