POI hinzufügen mit mysql2 Bibliothek irgendwie nicht optimal, mysql Bibliothek funktioniert gut, Pool auch genutzt in mysql Bibliothek für mehr Performence und weniger Probleme mit conntctions

This commit is contained in:
ISA
2024-08-14 20:38:55 +02:00
parent e0c23402f7
commit ad7c7f84a0
2 changed files with 32 additions and 42 deletions

View File

@@ -17,30 +17,30 @@
######################### #########################
DB_HOST=10.10.0.70 #DB_HOST=10.10.0.70
DB_USER=root
DB_PASSWORD="root#$"
DB_NAME=talas_v5
DB_PORT=3306
#########################
NEXT_PUBLIC_BASE_URL="http://10.10.0.70/talas5/devices/"
NEXT_PUBLIC_SERVER_URL="http://10.10.0.70"
NEXT_PUBLIC_PROXY_TARGET="http://10.10.0.70"
#NEXT_PUBLIC_ONLINE_TILE_LAYER="http://10.10.0.13:3000/mapTiles/{z}/{x}/{y}.png"
NEXT_PUBLIC_ONLINE_TILE_LAYER="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
#########################
#DB_HOST=192.168.10.167
#DB_USER=root #DB_USER=root
#DB_PASSWORD="root#$" #DB_PASSWORD="root#$"
#DB_NAME=talas_v5 #DB_NAME=talas_v5
#DB_PORT=3306 #DB_PORT=3306
#########################
#NEXT_PUBLIC_BASE_URL="http://10.10.0.70/talas5/devices/"
#NEXT_PUBLIC_SERVER_URL="http://10.10.0.70"
#NEXT_PUBLIC_PROXY_TARGET="http://10.10.0.70"
#NEXT_PUBLIC_ONLINE_TILE_LAYER="http://10.10.0.13:3000/mapTiles/{z}/{x}/{y}.png"
NEXT_PUBLIC_ONLINE_TILE_LAYER="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
#########################
DB_HOST=192.168.10.167
DB_USER=root
DB_PASSWORD="root#$"
DB_NAME=talas_v5
DB_PORT=3306
######################### #########################
#URLs für den Client (clientseitig) #URLs für den Client (clientseitig)
#NEXT_PUBLIC_BASE_URL="http://192.168.10.167/talas5/devices/" NEXT_PUBLIC_BASE_URL="http://192.168.10.167/talas5/devices/"
#NEXT_PUBLIC_SERVER_URL="http://192.168.10.167" NEXT_PUBLIC_SERVER_URL="http://192.168.10.167"
#NEXT_PUBLIC_PROXY_TARGET="http://192.168.10.167" NEXT_PUBLIC_PROXY_TARGET="http://192.168.10.167"
#NEXT_PUBLIC_ONLINE_TILE_LAYER="http://192.168.10.14:3000/mapTiles/{z}/{x}/{y}.png" #NEXT_PUBLIC_ONLINE_TILE_LAYER="http://192.168.10.14:3000/mapTiles/{z}/{x}/{y}.png"

View File

@@ -1,45 +1,35 @@
// pages/api/talas_v5_DB/pois/addLocation.js // pages/api/addLocation.js
import mysql from "mysql2/promise"; import mysql from "mysql";
// Erstellen eines Pools von Datenbankverbindungen
const pool = mysql.createPool({ const pool = mysql.createPool({
host: process.env.DB_HOST, host: process.env.DB_HOST,
user: process.env.DB_USER, user: process.env.DB_USER,
password: process.env.DB_PASSWORD, password: process.env.DB_PASSWORD,
database: process.env.DB_NAME, database: process.env.DB_NAME,
port: process.env.DB_PORT, port: process.env.DB_PORT,
waitForConnections: true, connectionLimit: 10, // Maximale Anzahl gleichzeitiger Verbindungen
connectionLimit: 10,
queueLimit: 0,
}); });
export default async function handler(req, res) { export default function handler(req, res) {
if (req.method === "POST") { if (req.method === "POST") {
const { name, poiTypeId, latitude, longitude, idLD } = req.body; const { name, poiTypeId, latitude, longitude, idLD } = req.body;
console.log("Received data:", req.body); // Überprüfen der empfangenen Daten console.log("Received data:", req.body); // Überprüfen der empfangenen Daten
if (!name || !poiTypeId || !latitude || !longitude || !idLD) { const query = "INSERT INTO poi (description, idPoiTyp, position, idLD) VALUES (?, ?, ST_GeomFromText(?),?)";
return res.status(400).json({ error: "Alle Felder sind erforderlich" });
}
const query = `
INSERT INTO poi (description, idPoiTyp, position, idLD)
VALUES (?, ?, ST_GeomFromText(?), ?)
`;
const point = `POINT(${longitude} ${latitude})`; const point = `POINT(${longitude} ${latitude})`;
const values = [name, poiTypeId, point, idLD]; const values = [name, poiTypeId, point, idLD];
try { // Verwende den Pool, um eine Verbindung zu bekommen und die Query auszuführen
// Ausführen der Abfrage mit dem Verbindungspool pool.query(query, values, (error, results) => {
const [results] = await pool.query(query, values); if (error) {
console.error("Fehler beim Einfügen des Standorts:", error);
return res.status(500).json({ error: "Ein Fehler ist aufgetreten" });
}
res.status(200).json({ res.status(200).json({
id: results.insertId, id: results.insertId,
message: "Standort erfolgreich hinzugefügt", message: "Standort erfolgreich hinzugefügt",
}); });
} catch (error) { });
console.error("Fehler beim Einfügen des Standorts:", error);
res.status(500).json({ error: "Ein Fehler ist aufgetreten" });
}
} else { } else {
res.setHeader("Allow", ["POST"]); res.setHeader("Allow", ["POST"]);
res.status(405).end(`Method ${req.method} Not Allowed`); res.status(405).end(`Method ${req.method} Not Allowed`);