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

47 lines
1.1 KiB
JavaScript

"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() {
return this.db.prepare(`SELECT * FROM readings`).iterate();
}
}
export default ReadingRepo;