Move server side code
This commit is contained in:
parent
450eed0ac7
commit
e16e55d987
5 changed files with 65 additions and 58 deletions
57
dbHandler.js
57
dbHandler.js
|
@ -1,57 +0,0 @@
|
|||
const config = require('config');
|
||||
const mysql = require('mysql');
|
||||
const dateToUtc = require('./utils.js')
|
||||
|
||||
const pool = mysql.createPool({
|
||||
host: config.get('db.host'),
|
||||
user: config.get('db.user'),
|
||||
password: config.get('db.password'),
|
||||
database: config.get('db.database'),
|
||||
});
|
||||
|
||||
function addMarker(marker) {
|
||||
let insertQuery = 'INSERT INTO markers VALUES (?,?,?,POINT(?,?),?)';
|
||||
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.description, marker.filename, marker.long, marker.lat, formattedDate]);
|
||||
pool.query(query, (err, response) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return;
|
||||
}
|
||||
// rows added
|
||||
console.log(response);
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getUpdatedMarkers(fromDate, callback) {
|
||||
pool.getConnection((err, connection) => {
|
||||
if (err) throw err;
|
||||
let selectQuery = 'SELECT * FROM markers mrk WHERE mrk.ts >= ?';
|
||||
let query = mysql.format(selectQuery, fromDate)
|
||||
connection.query(query, (err, rows) => {
|
||||
connection.release(); // return the connection to pool
|
||||
if (err) throw err;
|
||||
callback(rows);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
addMarker,
|
||||
getAllMarkers,
|
||||
getUpdatedMarkers
|
||||
}
|
|
@ -4,7 +4,7 @@
|
|||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "node index.js"
|
||||
"start": "node server/index.js"
|
||||
},
|
||||
"author": "HacklabBO",
|
||||
"license": "UNLICENSED",
|
||||
|
|
64
server/dbHandler.js
Normal file
64
server/dbHandler.js
Normal file
|
@ -0,0 +1,64 @@
|
|||
const config = require("config");
|
||||
const mysql = require("mysql");
|
||||
const dateToUtc = require("../utils.js");
|
||||
|
||||
const pool = mysql.createPool({
|
||||
host: config.get("db.host"),
|
||||
user: config.get("db.user"),
|
||||
password: config.get("db.password"),
|
||||
database: config.get("db.database"),
|
||||
});
|
||||
|
||||
function addMarker(marker) {
|
||||
let insertQuery = "INSERT INTO markers VALUES (?,?,?,POINT(?,?),?)";
|
||||
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.description,
|
||||
marker.filename,
|
||||
marker.long,
|
||||
marker.lat,
|
||||
formattedDate,
|
||||
]);
|
||||
pool.query(query, (err, response) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return;
|
||||
}
|
||||
// rows added
|
||||
console.log(response);
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getUpdatedMarkers(fromDate, callback) {
|
||||
pool.getConnection((err, connection) => {
|
||||
if (err) throw err;
|
||||
let selectQuery = "SELECT * FROM markers mrk WHERE mrk.ts >= ?";
|
||||
let query = mysql.format(selectQuery, fromDate);
|
||||
connection.query(query, (err, rows) => {
|
||||
connection.release(); // return the connection to pool
|
||||
if (err) throw err;
|
||||
callback(rows);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
addMarker,
|
||||
getAllMarkers,
|
||||
getUpdatedMarkers,
|
||||
};
|
Loading…
Reference in a new issue