switch node demo to mariadb

This commit is contained in:
“Naeel” 2026-02-21 09:11:10 +04:00
parent 69a6f413bb
commit 417ee230a4
5 changed files with 855 additions and 1601 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules/

View File

@ -1,4 +1,4 @@
# Node + Postgres Demo
# Node + MariaDB Demo
Simple Node.js CRUD app that writes to `nubes_test_table` and renders a small HTML UI.
If the input is empty, it inserts "Node did it".

2412
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -54,7 +54,7 @@
"@loopback/rest-explorer": "^3.3.1",
"@loopback/service-proxy": "^3.2.1",
"loopback-connector-rest": "^3.7.0",
"pg": "^8.18.0",
"mysql2": "^3.11.0",
"tslib": "^2.0.0"
},
"devDependencies": {

View File

@ -1,23 +1,20 @@
const http = require("http");
const { URL } = require("url");
const { Pool } = require("pg");
const mysql = require("mysql2/promise");
const pool = new Pool(
process.env.DATABASE_URL
? { connectionString: process.env.DATABASE_URL }
: {
host: process.env.PGHOST,
port: process.env.PGPORT || "5432",
user: process.env.PGUSER,
password: process.env.PGPASSWORD,
database: process.env.PGDATABASE || "postgres",
ssl: process.env.PGSSLMODE === "disable" ? false : { rejectUnauthorized: false },
}
);
const pool = process.env.DATABASE_URL
? mysql.createPool(process.env.DATABASE_URL)
: mysql.createPool({
host: process.env.DB_HOST,
port: process.env.DB_PORT || "3306",
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME || "postgres",
});
async function ensureTable() {
await pool.query(
"CREATE TABLE IF NOT EXISTS nubes_test_table (id SERIAL PRIMARY KEY, test_data TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)"
"CREATE TABLE IF NOT EXISTS nubes_test_table (id INT AUTO_INCREMENT PRIMARY KEY, test_data TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)"
);
}
@ -59,10 +56,10 @@ function renderPage(rows, error) {
<html lang="en">
<head>
<meta charset="utf-8">
<title>Node + Postgres Demo</title>
<title>Node + MariaDB Demo</title>
</head>
<body>
<h3>Node + Postgres Demo</h3>
<h3>Node + MariaDB Demo</h3>
${errorHtml}
<form method="POST" action="/add">
<input type="text" name="txt_content" placeholder="New message">
@ -88,11 +85,11 @@ async function handleRequest(req, res) {
if (req.method === "GET" && url.pathname === "/") {
try {
await ensureTable();
const result = await pool.query(
const [rows] = await pool.query(
"SELECT id, test_data FROM nubes_test_table ORDER BY id DESC LIMIT 20"
);
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
res.end(renderPage(result.rows));
res.end(renderPage(rows));
} catch (err) {
res.writeHead(500, { "Content-Type": "text/html; charset=utf-8" });
res.end(renderPage([], err.message));
@ -111,16 +108,16 @@ async function handleRequest(req, res) {
await ensureTable();
if (url.pathname === "/add") {
const content = form.txt_content || "Node did it";
await pool.query("INSERT INTO nubes_test_table (test_data) VALUES ($1)", [content]);
await pool.query("INSERT INTO nubes_test_table (test_data) VALUES (?)", [content]);
}
if (url.pathname === "/update") {
await pool.query("UPDATE nubes_test_table SET test_data=$1 WHERE id=$2", [
await pool.query("UPDATE nubes_test_table SET test_data=? WHERE id=?", [
form.txt_content || "",
form.id,
]);
}
if (url.pathname === "/delete") {
await pool.query("DELETE FROM nubes_test_table WHERE id=$1", [form.id]);
await pool.query("DELETE FROM nubes_test_table WHERE id=?", [form.id]);
}
res.writeHead(303, { Location: "/" });
res.end();