ruscomap/dbHandler.js

57 lines
1.6 KiB
JavaScript
Raw Normal View History

2024-07-16 17:04:32 +02:00
const config = require('config');
const mysql = require('mysql');
2024-09-07 01:41:43 +02:00
const dateToUtc = require('./utils.js')
2024-07-16 17:04:32 +02:00
const pool = mysql.createPool({
host: config.get('db.host'),
user: config.get('db.user'),
password: config.get('db.password'),
database: config.get('db.database'),
});
2024-06-30 22:56:19 +02:00
2024-07-16 17:04:32 +02:00
function addMarker(marker) {
let insertQuery = 'INSERT INTO markers VALUES (?,?,?,POINT(?,?),?)';
2024-09-07 01:41:43 +02:00
const date = new Date().toISOString()
const datePart = date.slice(0, 10); // '2024-09-06'
const timePart = date.slice(11, 19); // '21:33:44'
const formattedDate = `${datePart} ${timePart}`
let query = mysql.format(insertQuery, [marker.name, marker.dTIMESTAMPescription, marker.filename, marker.long, marker.lat, formattedDate]);
2024-07-16 17:04:32 +02:00
pool.query(query, (err, response) => {
if (err) {
console.error(err);
return;
}
// rows added
console.log(response);
});
2024-06-30 22:56:19 +02:00
}
2024-07-16 17:04:32 +02:00
function getAllMarkers(callback) {
pool.getConnection((err, connection) => {
if (err) throw err;
connection.query('SELECT * from markers', (err, rows) => {
connection.release(); // return the connection to pool
if (err) throw err;
callback(rows);
});
});
2024-06-30 22:56:19 +02:00
}
2024-07-16 17:04:32 +02:00
function getUpdatedMarkers(fromDate, callback) {
pool.getConnection((err, connection) => {
if (err) throw err;
let selectQuery = 'SELECT * FROM markers mrk WHERE mrk.ts >= ?';
2024-09-07 01:41:43 +02:00
let query = mysql.format(selectQuery, fromDate)
2024-07-16 17:04:32 +02:00
connection.query(query, (err, rows) => {
connection.release(); // return the connection to pool
if (err) throw err;
callback(rows);
});
});
2024-06-30 22:56:19 +02:00
}
module.exports = {
2024-07-16 17:04:32 +02:00
addMarker,
getAllMarkers,
getUpdatedMarkers
}