meteostanica.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { Database } from "bun:sqlite";
  2. const meteostanicaDB = new Database("./data/meteostanica.sqlite");
  3. meteostanicaDB.run(`create table if not exists data (
  4. timestamp datetime default current_timestamp primary key,
  5. indoorTemp text not null,
  6. indoorPressure text not null,
  7. indoorHumidity text not null,
  8. indoorAltitude text not null,
  9. outdoorConnected integer not null,
  10. outdoorTemp text not null,
  11. outdoorPressure text not null,
  12. outdoorHumidity text not null,
  13. outdoorAltitude text not null
  14. );`)
  15. export default class Meteostanica {
  16. static getData() {
  17. const statement = meteostanicaDB.prepare(`
  18. SELECT *
  19. FROM data
  20. ORDER BY timestamp DESC;
  21. `)
  22. const result = statement.all();
  23. return result
  24. }
  25. static getDataProperty(property) {
  26. const tableNames = meteostanicaDB.prepare(`PRAGMA table_info('data');`).all()
  27. if (!tableNames.find(i => i.name === property)) return null
  28. const statement = meteostanicaDB.prepare(`
  29. SELECT ${property}
  30. FROM data
  31. ORDER BY timestamp DESC
  32. LIMIT 10;
  33. `)
  34. const result = statement.all();
  35. return result
  36. }
  37. }