| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- import { Database } from "bun:sqlite";
- import { nanoid } from 'nanoid'
- import generateSecureRandomString from "./generateSecureRandomString";
- const meteostaniceDB = new Database("./data/meteostanice.sqlite");
- meteostaniceDB.run(`create table if not exists list (
- id text not null primary key,
- owner text not null,
- name text not null,
- description text,
- websocketKey text not null,
- timestamp datetime default current_timestamp
- );`)
- meteostaniceDB.run(`create table if not exists data (
- id text not null primary key,
- meteostanica text not null,
- timestamp datetime default current_timestamp,
- indoorTemp text not null,
- indoorPressure text not null,
- indoorHumidity text not null,
- indoorAltitude text not null,
- outdoorConnected integer not null,
- outdoorTemp text not null,
- outdoorPressure text not null,
- outdoorHumidity text not null,
- outdoorAltitude text not null
- );`)
- meteostaniceDB.run(`create table if not exists public (
- id text not null primary key,
- name text not null,
- description text,
- timestamp datetime default current_timestamp,
- showOwner integer default 1,
-
- showIndoorTemp integer default 1,
- showIndoorPressure integer default 1,
- showIndoorHumidity integer default 1,
- showIndoorAltitude integer default 1,
- showOutdoorConnected integer default 1,
- showOutdoorTemp integer default 1,
- showOutdoorPressure integer default 1,
- showOutdoorHumidity integer default 1,
- showOutdoorAltitude integer default 1
- );`)
- export default class Meteostanice {
- static add(owner, name, description) {
- const id = nanoid()
- const websocketKey = generateSecureRandomString()
- meteostaniceDB.prepare(`
- INSERT INTO list (id, owner, name, description, websocketKey)
- VALUES (?, ?, ?, ?, ?);
- `).run(id, owner, name, description, websocketKey)
- }
- static get(owner, id) {
- const statement = meteostaniceDB.prepare(`
- SELECT *
- FROM list
- WHERE owner = $owner AND id = $id;
- `)
- const result = statement.get({
- $owner: owner,
- $id: id
- });
- return result
- }
- static getWebsocket(key) {
- const statement = meteostaniceDB.prepare(`
- SELECT *
- FROM list
- WHERE websocketKey = $key;
- `)
- const result = statement.get({
- $key: key
- });
- return result
- }
- static getOwned(owner) {
- const statement = meteostaniceDB.prepare(`
- SELECT *
- FROM list
- WHERE owner = $owner;
- `)
- const result = statement.all({
- $owner: owner
- });
- return result
- }
- static edit(id, newName, newDescription, newOwner) {
- meteostaniceDB.prepare(`
- update list
- set name = ?,
- description = ?,
- owner = ?
- where id = ?;
- `).run(newName, newDescription, newOwner, id)
- }
- static remove(id) {
- meteostaniceDB.prepare(`
- DELETE
- FROM data
- WHERE meteostanica = $id;
- `).run({
- $owner: owner,
- $id: id
- });
- meteostaniceDB.prepare(`
- DELETE
- FROM public
- WHERE id = $id;
- `).run({
- $id: id
- });
- meteostaniceDB.prepare(`
- DELETE
- FROM list
- WHERE owner = $owner AND id = $id;
- `).run({
- $owner: owner,
- $id: id
- });
- }
- static removeOwned(owner) {
- const meteostanice = this.getOwned(owner)
- for (const meteostanica of meteostanice) {
- this.remove(meteostanica.owner, meteostanica.id)
- }
- }
- static postData(meteostanica, indoorTemp, indoorPressure, indoorHumidity, indoorAltitude, outdoorConnected, outdoorTemp, outdoorPressure, outdoorHumidity, outdoorAltitude) {
- const id = nanoid()
- meteostaniceDB.prepare(`
- INSERT INTO data (id, meteostanica, indoorTemp, indoorPressure, indoorHumidity, indoorAltitude, outdoorConnected, outdoorTemp, outdoorPressure, outdoorHumidity, outdoorAltitude)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
- `).run(id, meteostanica, indoorTemp, indoorPressure, indoorHumidity, indoorAltitude, outdoorConnected, outdoorTemp, outdoorPressure, outdoorHumidity, outdoorAltitude)
- }
- static getData(meteostanica) {
- const statement = meteostaniceDB.prepare(`
- SELECT *
- FROM data
- WHERE meteostanica = $meteostanica;
- `)
- const result = statement.all({
- $meteostanica: meteostanica
- });
- return result
- }
- static resetWebsocketKey(id) {
- const websocketKey = generateSecureRandomString()
- meteostaniceDB.prepare(`
- update list
- set websocketKey = ?
- where id = ?;
- `).run(websocketKey, id)
- }
- }
|