meteostanice.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import { Database } from "bun:sqlite";
  2. import { nanoid } from 'nanoid'
  3. import generateSecureRandomString from "./generateSecureRandomString";
  4. const meteostaniceDB = new Database("./data/meteostanice.sqlite");
  5. meteostaniceDB.run(`create table if not exists list (
  6. id text not null primary key,
  7. owner text not null,
  8. name text not null,
  9. description text,
  10. websocketKey text not null,
  11. timestamp datetime default current_timestamp
  12. );`)
  13. meteostaniceDB.run(`create table if not exists data (
  14. id text not null primary key,
  15. meteostanica text not null,
  16. timestamp datetime default current_timestamp,
  17. indoorTemp text not null,
  18. indoorPressure text not null,
  19. indoorHumidity text not null,
  20. indoorAltitude text not null,
  21. outdoorConnected integer not null,
  22. outdoorTemp text not null,
  23. outdoorPressure text not null,
  24. outdoorHumidity text not null,
  25. outdoorAltitude text not null
  26. );`)
  27. meteostaniceDB.run(`create table if not exists public (
  28. id text not null primary key,
  29. name text not null,
  30. description text,
  31. timestamp datetime default current_timestamp,
  32. showOwner integer default 1,
  33. showIndoorTemp integer default 1,
  34. showIndoorPressure integer default 1,
  35. showIndoorHumidity integer default 1,
  36. showIndoorAltitude integer default 1,
  37. showOutdoorConnected integer default 1,
  38. showOutdoorTemp integer default 1,
  39. showOutdoorPressure integer default 1,
  40. showOutdoorHumidity integer default 1,
  41. showOutdoorAltitude integer default 1
  42. );`)
  43. export default class Meteostanice {
  44. static add(owner, name, description) {
  45. const id = nanoid()
  46. const websocketKey = generateSecureRandomString()
  47. meteostaniceDB.prepare(`
  48. INSERT INTO list (id, owner, name, description, websocketKey)
  49. VALUES (?, ?, ?, ?, ?);
  50. `).run(id, owner, name, description, websocketKey)
  51. }
  52. static get(owner, id) {
  53. const statement = meteostaniceDB.prepare(`
  54. SELECT *
  55. FROM list
  56. WHERE owner = $owner AND id = $id;
  57. `)
  58. const result = statement.get({
  59. $owner: owner,
  60. $id: id
  61. });
  62. return result
  63. }
  64. static getWebsocket(key) {
  65. const statement = meteostaniceDB.prepare(`
  66. SELECT *
  67. FROM list
  68. WHERE websocketKey = $key;
  69. `)
  70. const result = statement.get({
  71. $key: key
  72. });
  73. return result
  74. }
  75. static getOwned(owner) {
  76. const statement = meteostaniceDB.prepare(`
  77. SELECT *
  78. FROM list
  79. WHERE owner = $owner;
  80. `)
  81. const result = statement.all({
  82. $owner: owner
  83. });
  84. return result
  85. }
  86. static edit(id, newName, newDescription, newOwner) {
  87. meteostaniceDB.prepare(`
  88. update list
  89. set name = ?,
  90. description = ?,
  91. owner = ?
  92. where id = ?;
  93. `).run(newName, newDescription, newOwner, id)
  94. }
  95. static remove(id) {
  96. meteostaniceDB.prepare(`
  97. DELETE
  98. FROM data
  99. WHERE meteostanica = $id;
  100. `).run({
  101. $owner: owner,
  102. $id: id
  103. });
  104. meteostaniceDB.prepare(`
  105. DELETE
  106. FROM public
  107. WHERE id = $id;
  108. `).run({
  109. $id: id
  110. });
  111. meteostaniceDB.prepare(`
  112. DELETE
  113. FROM list
  114. WHERE owner = $owner AND id = $id;
  115. `).run({
  116. $owner: owner,
  117. $id: id
  118. });
  119. }
  120. static removeOwned(owner) {
  121. const meteostanice = this.getOwned(owner)
  122. for (const meteostanica of meteostanice) {
  123. this.remove(meteostanica.owner, meteostanica.id)
  124. }
  125. }
  126. static postData(meteostanica, indoorTemp, indoorPressure, indoorHumidity, indoorAltitude, outdoorConnected, outdoorTemp, outdoorPressure, outdoorHumidity, outdoorAltitude) {
  127. const id = nanoid()
  128. meteostaniceDB.prepare(`
  129. INSERT INTO data (id, meteostanica, indoorTemp, indoorPressure, indoorHumidity, indoorAltitude, outdoorConnected, outdoorTemp, outdoorPressure, outdoorHumidity, outdoorAltitude)
  130. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
  131. `).run(id, meteostanica, indoorTemp, indoorPressure, indoorHumidity, indoorAltitude, outdoorConnected, outdoorTemp, outdoorPressure, outdoorHumidity, outdoorAltitude)
  132. }
  133. static getData(meteostanica) {
  134. const statement = meteostaniceDB.prepare(`
  135. SELECT *
  136. FROM data
  137. WHERE meteostanica = $meteostanica;
  138. `)
  139. const result = statement.all({
  140. $meteostanica: meteostanica
  141. });
  142. return result
  143. }
  144. static resetWebsocketKey(id) {
  145. const websocketKey = generateSecureRandomString()
  146. meteostaniceDB.prepare(`
  147. update list
  148. set websocketKey = ?
  149. where id = ?;
  150. `).run(websocketKey, id)
  151. }
  152. }