import express, {
type Application,
type Request,
type Response,
} from "express";
import { Pool } from "pg";
import config from "./config";
// Working with Express and NeonDB Postgre Database
// Three Basci (3) steps Only
// 1. Connect TO Database URL / Connection
// 2. Create or Initialize your Database Table
// You ARE READY FOR APPLY CRUD OPPERATION on DATABASE TABLE NOW
// 3. Apply CRUD OPERATION
// GOOD LUCK
const app: Application = express();
const port = config.port;
app.use(express.json());
app.use(express.text());
app.use(express.urlencoded({ extended: true }));
// Without Confugyration file
const pool = new Pool({
connectionString: "postgresql://neondb_owner:BolboNA_BolboNA_BolboNA_BolboNA_channel_binding=require",
});
// After Database connection
// Table Created successfully
// Update Delete Create Get , Single user by ID
// Configuration file added for connection string from database
// const pool = new Pool({
// connectionString: config.connection_string,
// });
const initDB = async () => {
try
{
await pool.query(`
CREATE TABLE IF NOT EXISTS users(
id SERIAL PRIMARY KEY,
name VARCHAR(20),
email VARCHAR(20) UNIQUE NOT NULL,
password VARCHAR(20) NOT NULL,
is_active BOOLEAN DEFAULT true,
age INT,
created_at TIMESTAMP DEFAULT NOW(),
updated_at TIMESTAMP DEFAULT NOW()
)
`);
console.log("Database connected successfully!");
}
catch (error)
{
console.log(error);
}
};
initDB();
app.get("/", (req: Request, res: Response) =>
{
//res.send("Hello World!");
res.status(200).json
(
{
message: "Express Server",
author: "Next Level",
}
);
});
// CREATE
app.post("/api/users", async (req: Request, res: Response) => {
// console.log(req.body);
const { name, email, password, age } = req.body;
try {
const result = await pool.query(
`
INSERT INTO users(name,email,password,age) VALUES($1,$2,$3,$4) RETURNING *
`,
[name, email, password, age],
);
// console.log(result);
res.status(201).json({
success: true,
message: "User Created successfully!",
data: result.rows[0],
});
} catch (error: any) {
res.status(500).json({
success: false,
message: error.message,
error: error,
});
}
});
// READ
app.get("/api/users", async (req: Request, res: Response) => {
try {
const result = await pool.query(`
SELECT * FROM users
`);
res.status(200).json({
success: true,
message: "Users retrived successfully!",
data: result.rows,
});
} catch (error: any) {
res.status(500).json({
success: false,
message: error.message,
error: error,
});
}
});
app.get("/api/users/:id", async (req: Request, res: Response) => {
const { id } = req.params;
try {
const result = await pool.query(
`
SELECT * FROM users WHERE id=$1
`,
[id],
);
if (result.rows.length === 0) {
res.status(404).json({
success: false,
message: "User Not found!",
data: {},
});
}
res.status(200).json({
success: true,
message: "User retrived successfully!",
data: result.rows[0],
});
} catch (error: any) {
res.status(500).json({
success: false,
message: error.message,
error: error,
});
}
});
// UPDATE
app.put("/api/users/:id", async (req: Request, res: Response) => {
const { id } = req.params;
const { name, password, age, is_active } = req.body;
// console.log("Id : ", id);
// console.log({ name, password, age, is_active });
try {
const result = await pool.query(
`
UPDATE users
SET
name=COALESCE($1,name),
password=COALESCE($2,password),
age=COALESCE($3,age),
is_active=COALESCE($4,is_active)
WHERE id=$5 RETURNING *
`,
[name, password, age, is_active, id],
);
if (result.rows.length === 0) {
res.status(404).json({
success: false,
message: "User Not found!",
});
}
// console.log(result);
res.status(200).json({
success: true,
message: "User updated successfully!",
data: result.rows[0],
});
} catch (error: any) {
res.status(500).json({
success: false,
message: error.message,
error: error,
});
}
});
// DELETE
app.delete("/api/users/:id", async (req: Request, res: Response) => {
const { id } = req.params;
try {
const result = await pool.query(
`
DELETE FROM users WHERE id=$1
`,
[id],
);
console.log(result);
if (result.rowCount === 0) {
res.status(404).json({
success: false,
message: "User Not found!",
});
}
res.status(200).json({
success: true,
message: "User deleted successfully!",
data: {},
});
} catch (error: any) {
res.status(500).json({
success: false,
message: error.message,
error: error,
});
}
});
// Without Config File
app.listen(5000, () => {
console.log(`Example app listening on port on 5000`);
});
// Config file added
// app.listen(port, () => {
// console.log(`Example app listening on port ${port}`);
// });