Skip to content

Commit 146780e

Browse files
committed
ADD - db test
1 parent ce9a178 commit 146780e

5 files changed

Lines changed: 134 additions & 1 deletion

File tree

client/src/App.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Hero } from "./components/Hero.jsx";
22
import { MainPage } from "./components/MainPage.jsx";
3+
import { TestPage } from "./components/TestPage.jsx";
34

45
export default function App() {
56
const pathname = window.location.pathname.replace(/\/+$/, "") || "/";
@@ -8,5 +9,9 @@ export default function App() {
89
return <MainPage />;
910
}
1011

12+
if (pathname === "/test") {
13+
return <TestPage />;
14+
}
15+
1116
return <Hero />;
1217
}

client/src/components/TestPage.css

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
.test-page {
2+
min-height: 100vh;
3+
padding: 48px 20px;
4+
background: #73a5ca;
5+
}
6+
7+
.test-page__card {
8+
width: min(900px, 100%);
9+
margin: 0 auto;
10+
padding: 32px;
11+
border: 4px solid #162456;
12+
border-radius: 24px;
13+
background: #fff8d8;
14+
box-shadow: 8px 8px 0 #162456;
15+
}
16+
17+
.test-page__card h1 {
18+
margin: 0 0 12px;
19+
font-size: clamp(2rem, 6vw, 4rem);
20+
}
21+
22+
.test-page__card p {
23+
margin: 0 0 20px;
24+
font-size: 1.1rem;
25+
}
26+
27+
.test-page__error {
28+
color: #b3261e;
29+
}
30+
31+
.test-page__rows {
32+
display: grid;
33+
gap: 16px;
34+
}
35+
36+
.test-page__rows pre {
37+
margin: 0;
38+
padding: 16px;
39+
overflow-x: auto;
40+
border-radius: 12px;
41+
background: #162456;
42+
color: #fff8d8;
43+
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
44+
font-size: 0.95rem;
45+
}

client/src/components/TestPage.jsx

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { useEffect, useState } from "react";
2+
import "./TestPage.css";
3+
4+
export function TestPage() {
5+
const [rows, setRows] = useState([]);
6+
const [status, setStatus] = useState("Loading test rows...");
7+
const [error, setError] = useState("");
8+
9+
useEffect(() => {
10+
let isMounted = true;
11+
12+
async function loadRows() {
13+
try {
14+
const response = await fetch("/api/test");
15+
const isJson = response.headers.get("content-type")?.includes("application/json");
16+
const data = isJson ? await response.json() : {};
17+
18+
if (!response.ok) {
19+
throw new Error(data.error || "Failed to load test rows.");
20+
}
21+
22+
if (isMounted) {
23+
setRows(data.rows || []);
24+
setStatus("");
25+
}
26+
} catch (err) {
27+
if (isMounted) {
28+
setError(err.message);
29+
setStatus("");
30+
}
31+
}
32+
}
33+
34+
loadRows();
35+
36+
return () => {
37+
isMounted = false;
38+
};
39+
}, []);
40+
41+
return (
42+
<main className="test-page">
43+
<section className="test-page__card">
44+
<h1>Test Table</h1>
45+
<p>Rows from the production Postgres <code>test</code> table.</p>
46+
47+
{status && <p>{status}</p>}
48+
{error && <p className="test-page__error">{error}</p>}
49+
50+
{!status && !error && rows.length === 0 && <p>No rows found.</p>}
51+
52+
{rows.length > 0 && (
53+
<div className="test-page__rows">
54+
{rows.map((row, index) => (
55+
<pre key={row.id ?? index}>{JSON.stringify(row, null, 2)}</pre>
56+
))}
57+
</div>
58+
)}
59+
</section>
60+
</main>
61+
);
62+
}

server/db.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,12 @@ export async function checkDatabaseConnection() {
4040
now: result.rows[0].now,
4141
};
4242
}
43+
44+
export async function getTestRows() {
45+
if (!pool) {
46+
throw new Error("DATABASE_URL is not set.");
47+
}
48+
49+
const result = await pool.query("select * from test");
50+
return result.rows;
51+
}

server/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import "dotenv/config";
22
import express from "express";
33
import path from "path";
44
import { fileURLToPath } from "url";
5-
import { checkDatabaseConnection } from "./db.js";
5+
import { checkDatabaseConnection, getTestRows } from "./db.js";
66

77
const __dirname = path.dirname(fileURLToPath(import.meta.url));
88
const app = express();
@@ -69,6 +69,18 @@ if (siteLockEnabled) {
6969
});
7070
}
7171

72+
app.get("/api/test", async (req, res) => {
73+
try {
74+
const rows = await getTestRows();
75+
res.json({ rows });
76+
} catch (error) {
77+
console.error("Failed to load test rows:", error);
78+
res.status(500).json({
79+
error: "Failed to load test rows.",
80+
});
81+
}
82+
});
83+
7284
if (isProd) {
7385
const dist = path.join(__dirname, "../client/dist");
7486
app.use(express.static(dist));

0 commit comments

Comments
 (0)