My LoRaWAN Signal Mapping MSc summer project. This is a copy of the actual repository with personal information removed.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.3 KiB

  1. "use strict";
  2. class GatewayRepo {
  3. constructor({ database }) {
  4. this.db = database;
  5. }
  6. add(...gateways) {
  7. const statement = this.db.prepare(`INSERT INTO gateways (
  8. id,
  9. latitude, longitude,
  10. altitude
  11. ) VALUES (
  12. :id,
  13. :latitude, :longitude,
  14. :altitude
  15. )`);
  16. for(let gateway of gateways)
  17. statement.run(gateway);
  18. }
  19. /**
  20. * Determines whether a gateway exists in the dayabase with the given name.
  21. * @param {string} id The id of the gateway.
  22. * @return {bool} Whether a gateway with the given name exists in the database.
  23. */
  24. exists(id) {
  25. // 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.
  26. let statement = this.db.prepare(`SELECT COUNT(id) AS count
  27. FROM gateways
  28. WHERE id = :id`);
  29. let count = statement.get({ id }).count;
  30. return count > 0;
  31. }
  32. get_by_id(id) {
  33. return this.db.prepare(`SELECT * FROM gateways WHERE id = :id`).get({ id });
  34. }
  35. /**
  36. * Iterates over all the gateways in the table.
  37. * TODO: Use Symbol.iterator here?
  38. * @return {Generator} A generator that can be used with for..of
  39. */
  40. iterate() {
  41. return this.db.prepare(`SELECT * FROM gateways`).iterate();
  42. }
  43. }
  44. export default GatewayRepo;