-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
48 lines (42 loc) · 1.36 KB
/
db.js
File metadata and controls
48 lines (42 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('./bcv.db');
db.serialize(() => {
db.run(`CREATE TABLE IF NOT EXISTS historial (
id INTEGER PRIMARY KEY AUTOINCREMENT,
usd REAL,
eur REAL,
fecha TEXT,
hora TEXT
)`);
});
function guardarValor(usd, eur) {
const fecha = new Date().toISOString().split('T')[0];
const hora = new Date().toISOString().split('T')[1].split('.')[0];
db.run(`INSERT INTO historial (usd, eur, fecha, hora) VALUES (?, ?, ?, ?)`, [usd, eur, fecha, hora]);
}
function obtenerUltimoValor() {
return new Promise((resolve, reject) => {
db.get(`SELECT usd, eur, fecha, hora FROM historial ORDER BY id DESC LIMIT 1`, (err, row) => {
if (err) reject(err);
else resolve(row || {});
});
});
}
function obtenerHistorialHoy() {
const hoy = new Date().toISOString().split('T')[0];
return new Promise((resolve, reject) => {
db.all(`SELECT * FROM historial WHERE fecha = ?`, [hoy], (err, rows) => {
if (err) reject(err);
else resolve(rows);
});
});
}
function obtenerHistorialCompleto() {
return new Promise((resolve, reject) => {
db.all(`SELECT * FROM historial ORDER BY id DESC`, (err, rows) => {
if (err) reject(err);
else resolve(rows);
});
});
}
module.exports = { guardarValor, obtenerUltimoValor, obtenerHistorialHoy, obtenerHistorialCompleto };