week-7: add RDS terraform config and PostgreSQL backend integration

This commit is contained in:
ikechukwu Elizabeth
2026-05-21 02:24:50 +01:00
parent 4e31d619a7
commit ec768b5f62
4 changed files with 236 additions and 35 deletions
View File
+153 -6
View File
@@ -1,15 +1,16 @@
{
"name": "task-backend",
"version": "1.0.0",
"version": "2.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "task-backend",
"version": "1.0.0",
"version": "2.0.0",
"dependencies": {
"dotenv": "^17.4.0",
"express": "^4.21.2"
"express": "^4.21.2",
"pg": "^8.13.3"
},
"devDependencies": {
"@eslint/js": "^10.0.1",
@@ -2111,9 +2112,9 @@
}
},
"node_modules/brace-expansion": {
"version": "5.0.5",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-5.0.5.tgz",
"integrity": "sha512-VZznLgtwhn+Mact9tfiwx64fA9erHH/MCXEUfB/0bX/6Fz6ny5EGTXYltMocqg4xFAQZtnO3DHWWXi8RiuN7cQ==",
"version": "5.0.6",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-5.0.6.tgz",
"integrity": "sha512-kLpxurY4Z4r9sgMsyG0Z9uzsBlgiU/EFKhj/h91/8yHu0edo7XuixOIH3VcJ8kkxs6/jPzoI6U9Vj3WqbMQ94g==",
"dev": true,
"license": "MIT",
"dependencies": {
@@ -4971,6 +4972,95 @@
"integrity": "sha512-A/AGNMFN3c8bOlvV9RreMdrv7jsmF9XIfDeCd87+I8RNg6s78BhJxMu69NEMHBSJFxKidViTEdruRwEk/WIKqA==",
"license": "MIT"
},
"node_modules/pg": {
"version": "8.21.0",
"resolved": "https://registry.npmjs.org/pg/-/pg-8.21.0.tgz",
"integrity": "sha512-AUP1EYJuHraQGsVoCQVIcM7TEJVGtDzxWtGFZd8rds9d+CCXlU5Js1rYgfLNvxy9iJrpHjGrRjoi/3BT9fRyiA==",
"license": "MIT",
"dependencies": {
"pg-connection-string": "^2.13.0",
"pg-pool": "^3.14.0",
"pg-protocol": "^1.14.0",
"pg-types": "2.2.0",
"pgpass": "1.0.5"
},
"engines": {
"node": ">= 16.0.0"
},
"optionalDependencies": {
"pg-cloudflare": "^1.4.0"
},
"peerDependencies": {
"pg-native": ">=3.0.1"
},
"peerDependenciesMeta": {
"pg-native": {
"optional": true
}
}
},
"node_modules/pg-cloudflare": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/pg-cloudflare/-/pg-cloudflare-1.4.0.tgz",
"integrity": "sha512-Vo7z/6rrQYxpNRylp4Tlob2elzbh+N/MOQbxFVWCxS7oEx6jF53GTJFxK2WWpKuBRkmiin4Mt+xofFDjx09R0A==",
"license": "MIT",
"optional": true
},
"node_modules/pg-connection-string": {
"version": "2.13.0",
"resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.13.0.tgz",
"integrity": "sha512-EMnU9E2fSULdsbErBbMaXJvFeD9B4+nPcM3f+4lsiCR0BHLPrLVjv3DbyM2hgQQviKJaTWIRRTjKjWlHg3p2ig==",
"license": "MIT"
},
"node_modules/pg-int8": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz",
"integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==",
"license": "ISC",
"engines": {
"node": ">=4.0.0"
}
},
"node_modules/pg-pool": {
"version": "3.14.0",
"resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.14.0.tgz",
"integrity": "sha512-gKtPkFdQPU3DksooVLi9LsjZxrsBUZIpa+7aVx+LV5pNh0KzP4Zleud2po+ConrxbuXGBJ6Hfer6hdgpIBpBaw==",
"license": "MIT",
"peerDependencies": {
"pg": ">=8.0"
}
},
"node_modules/pg-protocol": {
"version": "1.14.0",
"resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.14.0.tgz",
"integrity": "sha512-n5taZ1kO3s9ngDTVxsEznOqCyToTgz0FLuPq0B33COy5pPpuWJpY3/2oRBVETuOgzdqRXfWpM9HIhp2LBBT1BA==",
"license": "MIT"
},
"node_modules/pg-types": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz",
"integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==",
"license": "MIT",
"dependencies": {
"pg-int8": "1.0.1",
"postgres-array": "~2.0.0",
"postgres-bytea": "~1.0.0",
"postgres-date": "~1.0.4",
"postgres-interval": "^1.1.0"
},
"engines": {
"node": ">=4"
}
},
"node_modules/pgpass": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.5.tgz",
"integrity": "sha512-FdW9r/jQZhSeohs1Z3sI1yxFQNFvMcnmfuj4WBMUTxOrAyLMaTcE1aAMBiTlbMNaXvBCQuVi0R7hd8udDSP7ug==",
"license": "MIT",
"dependencies": {
"split2": "^4.1.0"
}
},
"node_modules/picocolors": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz",
@@ -5070,6 +5160,45 @@
"node": ">=8"
}
},
"node_modules/postgres-array": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz",
"integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==",
"license": "MIT",
"engines": {
"node": ">=4"
}
},
"node_modules/postgres-bytea": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.1.tgz",
"integrity": "sha512-5+5HqXnsZPE65IJZSMkZtURARZelel2oXUEO8rH83VS/hxH5vv1uHquPg5wZs8yMAfdv971IU+kcPUczi7NVBQ==",
"license": "MIT",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/postgres-date": {
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz",
"integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==",
"license": "MIT",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/postgres-interval": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz",
"integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==",
"license": "MIT",
"dependencies": {
"xtend": "^4.0.0"
},
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/prelude-ls": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz",
@@ -5453,6 +5582,15 @@
"source-map": "^0.6.0"
}
},
"node_modules/split2": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/split2/-/split2-4.2.0.tgz",
"integrity": "sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg==",
"license": "ISC",
"engines": {
"node": ">= 10.x"
}
},
"node_modules/sprintf-js": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
@@ -6197,6 +6335,15 @@
"node": "^14.17.0 || ^16.13.0 || >=18.0.0"
}
},
"node_modules/xtend": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",
"integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==",
"license": "MIT",
"engines": {
"node": ">=0.4"
}
},
"node_modules/y18n": {
"version": "5.0.8",
"resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz",
+3 -2
View File
@@ -1,6 +1,6 @@
{
"name": "task-backend",
"version": "1.0.0",
"version": "2.0.0",
"description": "Backend API for scalable task platform",
"main": "server.js",
"scripts": {
@@ -11,7 +11,8 @@
},
"dependencies": {
"dotenv": "^17.4.0",
"express": "^4.21.2"
"express": "^4.21.2",
"pg": "^8.13.3"
},
"devDependencies": {
"@eslint/js": "^10.0.1",
+79 -26
View File
@@ -1,18 +1,16 @@
require("dotenv").config();
const express = require("express");
const { pool, initDb } = require("./db");
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
// In-memory task store
let tasks = [];
let nextId = 1;
// ── Routes ────────────────────────────────────
// Routes
app.get("/api", (req, res) => {
res.json({ message: "Task API is running", version: "1.0.0" });
res.json({ message: "Task API is running", version: "2.0.0" });
});
app.get("/api/health", (req, res) => {
@@ -23,49 +21,104 @@ app.get("/api/health", (req, res) => {
});
});
app.get("/api/tasks", (req, res) => {
res.json(tasks);
// GET /api/tasks — return all tasks, newest first
app.get("/api/tasks", async (req, res) => {
try {
const result = await pool.query(
"SELECT * FROM tasks ORDER BY created_at DESC"
);
res.json(result.rows);
} catch (err) {
console.error("GET /api/tasks error:", err);
res.status(500).json({ error: "Failed to fetch tasks" });
}
});
app.post("/api/tasks", (req, res) => {
// POST /api/tasks — create a new task
app.post("/api/tasks", async (req, res) => {
const { title } = req.body;
if (!title) {
return res.status(400).json({ error: "Title is required" });
}
const task = { id: nextId++, title, completed: false, createdAt: new Date() };
tasks.push(task);
res.status(201).json(task);
try {
const result = await pool.query(
"INSERT INTO tasks (title) VALUES ($1) RETURNING *",
[title]
);
res.status(201).json(result.rows[0]);
} catch (err) {
console.error("POST /api/tasks error:", err);
res.status(500).json({ error: "Failed to create task" });
}
});
app.patch("/api/tasks/:id", (req, res) => {
const task = tasks.find((t) => t.id === parseInt(req.params.id));
if (!task) return res.status(404).json({ error: "Task not found" });
task.completed = !task.completed;
res.json(task);
// PATCH /api/tasks/:id — toggle completed status
app.patch("/api/tasks/:id", async (req, res) => {
const { id } = req.params;
try {
const result = await pool.query(
`UPDATE tasks
SET completed = NOT completed
WHERE id = $1
RETURNING *`,
[id]
);
if (result.rowCount === 0) {
return res.status(404).json({ error: "Task not found" });
}
res.json(result.rows[0]);
} catch (err) {
console.error("PATCH /api/tasks/:id error:", err);
res.status(500).json({ error: "Failed to update task" });
}
});
app.delete("/api/tasks/:id", (req, res) => {
const index = tasks.findIndex((t) => t.id === parseInt(req.params.id));
if (index === -1) return res.status(404).json({ error: "Task not found" });
tasks.splice(index, 1);
res.status(204).send();
// DELETE /api/tasks/:id — delete a task
app.delete("/api/tasks/:id", async (req, res) => {
const { id } = req.params;
try {
const result = await pool.query(
"DELETE FROM tasks WHERE id = $1",
[id]
);
if (result.rowCount === 0) {
return res.status(404).json({ error: "Task not found" });
}
res.status(204).send();
} catch (err) {
console.error("DELETE /api/tasks/:id error:", err);
res.status(500).json({ error: "Failed to delete task" });
}
});
// 404 handler
// ── Error handlers ────────────────────────────
app.use((req, res) => {
res.status(404).json({ error: "Route not found" });
});
// Global error handler
app.use((err, req, res, _next) => {
console.error(err.stack);
res.status(500).json({ error: "Internal server error" });
});
// ── Start ─────────────────────────────────────
// initDb runs first — creates the tasks table if it
// does not exist, then starts the server.
// Wrapped in require.main check so Jest can import
// the app without starting the server or connecting
// to the database.
if (require.main === module) {
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
initDb()
.then(() => {
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
})
.catch((err) => {
console.error("Failed to initialise database:", err);
process.exit(1);
});
}
module.exports = app;