ruscomap/dbHandler.js

52 lines
1.4 KiB
JavaScript
Raw Normal View History

2024-07-16 17:04:32 +02:00
const config = require('config');
const mysql = require('mysql');
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-07-17 12:00:34 +02:00
let query = mysql.format(insertQuery, [marker.name, marker.description, marker.filename, marker.long, marker.lat, new Date()]);
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-07-16 18:31:53 +02:00
let query = mysql.format(selectQuery, [new Date(parseInt(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
}