Skip to content

Commit 87644dd

Browse files
authored
Merge pull request #45 from PriDebnath/add-express-pg
Add express pg
2 parents c6234a1 + 0f6e8d4 commit 87644dd

33 files changed

Lines changed: 1164 additions & 2 deletions

‎backend-express-pg/.env.example‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
PORT=8000
2+
REDIS_URL=redis://localhost:6379
3+
PG_DATABASE_URL=postgresql://postgres:password@localhost:5432/quotes-keeper
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
services:
3+
redis:
4+
image: redis:7
5+
container_name: backend-express-pg-redis
6+
ports:
7+
- "6379:6379"
8+
db: # run postgres container on port 5432
9+
image: postgres:16
10+
container_name: backend-express-pg-postgres
11+
restart: unless-stopped
12+
environment:
13+
POSTGRES_USER: postgres
14+
POSTGRES_PASSWORD: password
15+
POSTGRES_DB: quotes-keeper
16+
ports:
17+
- "5432:5432"
18+
volumes: # map volume to /var/lib/postgresql/data
19+
- postgres_data:/var/lib/postgresql/data
20+
volumes: # create volume
21+
postgres_data:

‎backend-express-pg/package.json‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"name": "backend-express",
3+
"version": "1.0.50",
4+
"scripts": {
5+
"dev": "nodemon --legacy-watch --watch src --ext ts,json --exec ts-node --files src/server.ts",
6+
"build": "tsc",
7+
"start:redis": "redis-server --ignore-warnings ARM64-COW-BUG",
8+
"start": "node dist/server.js",
9+
"docker:up": "docker compose up",
10+
"docker:down": "docker compose down -v",
11+
"db:generate": "npx drizzle-kit generate --config=./src/drizzle.config.ts",
12+
"db:migrate": "npx drizzle-kit migrate --config=./src/drizzle.config.ts",
13+
"db:studio": "npx drizzle-kit studio --config=./src/drizzle.config.ts"
14+
},
15+
"dependencies": {
16+
"@bull-board/api": "^7.1.5",
17+
"@bull-board/express": "^7.1.5",
18+
"@types/cors": "^2.8.19",
19+
"@types/express": "^5.0.6",
20+
"@types/jsonwebtoken": "^9.0.10",
21+
"autocannon": "^8.0.0",
22+
"axios": "^1.13.6",
23+
"bcryptjs": "^3.0.3",
24+
"bullmq": "^5.76.10",
25+
"cors": "^2.8.6",
26+
"dotenv": "^17.3.1",
27+
"eslint": "^9.39.4",
28+
"express": "^5.2.1",
29+
"express-rate-limit": "^8.5.1",
30+
"helmet": "^8.1.0",
31+
"jsonwebtoken": "^9.0.3",
32+
"mongoose": "^9.3.1",
33+
"nodemon": "^3.1.14",
34+
"pino-http": "^11.0.0",
35+
"pino-pretty": "^13.1.3",
36+
"prettier": "^3.8.1",
37+
"prom-client": "^15.1.3",
38+
"redis": "^5.12.1",
39+
"swagger-jsdoc": "^6.2.8",
40+
"swagger-ui-express": "^5.0.1",
41+
"ts-node": "^10.9.2",
42+
"ts-node-dev": "^2.0.0",
43+
"drizzle-orm": "^0.45.1",
44+
"typescript": "~5.9.3",
45+
"drizzle-kit": "^0.31.8",
46+
"@types/pg": "^8.16.0",
47+
"zod": "^4.3.6",
48+
"@types/node": "^24.12.0",
49+
"@types/swagger-jsdoc": "^6.0.4",
50+
"@types/swagger-ui-express": "^4.1.8"
51+
},
52+
"author": {
53+
"name": "Pritam Debnath",
54+
"email": "debnathpritam0802@gmail.com",
55+
"url": "https://github.com/PriDebnath/portfolio-v2"
56+
}
57+
}

‎backend-express-pg/src/app.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import cors from "cors"
2+
// import helmet from "helmet"
3+
import swaggerUi from "swagger-ui-express"
4+
import express, { Express, } from "express"
5+
import { logger } from "./utils/config/logger.config";
6+
import { router as routerAuth } from "./module/auth/router";
7+
import { router as routerNote } from "./module/note/router";
8+
// import { register } from "./utils/config/app-metric.config";
9+
import { swaggerUiApp } from "./utils/config/swagger.config";
10+
// import { router as routerUsers } from "./module/user/router";
11+
// import { appMetric } from "./middleware/app-metric.middleware";
12+
import { rateLimit } from "./utils/config/rate-limiter.config";
13+
import { validateJwt } from "./middleware/validate-jwt";
14+
// import { router as routerPractice} from "./module/practice/router";
15+
// import { dashboardPath, serverAdapter } from "./utils/config/worker-dashboard.config";
16+
17+
const app: Express = express()
18+
19+
app.use(cors( ))
20+
// app.use(helmet( ))
21+
// app.use(appMetric)
22+
app.use(express.json({ strict: false,limit:"10kb" }));
23+
app.use(logger);
24+
app.use(rateLimit);
25+
26+
// custom middleware
27+
// app.use(validateJwt);// can apply at globally
28+
29+
30+
//// Swagger for API docs
31+
app.use("/docs", swaggerUi.serve, swaggerUiApp);
32+
33+
// Worker dashboard
34+
// app.use(dashboardPath, serverAdapter.getRouter());
35+
36+
// Metric
37+
// app.get("/metrics", async (req, res) => {
38+
// res.set("Content-Type", register.contentType)
39+
// res.end(await register.metrics())
40+
// })
41+
42+
// Routers
43+
// app.use("/api/v1/users", routerUsers);
44+
app.use("/api/v1/auth", routerAuth);
45+
app.use("/api/v1/notes", routerNote);
46+
// app.use("/api/v1/practice", routerPractice);
47+
48+
app.get("/", (req, res) => {
49+
res.send({ message: "🟩 Server is up and running" })
50+
})
51+
52+
export default app
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
3+
import { Pool } from "pg"
4+
import { drizzle } from "drizzle-orm/node-postgres"
5+
import { env } from "../../utils/load-env"
6+
import * as schema from "../model/index"
7+
8+
export const pool = new Pool({
9+
connectionString: env.PG_DATABASE_URL
10+
})
11+
12+
export const db = drizzle(pool, { schema: schema})
13+
14+
export const connectDB = async () => {
15+
try {
16+
await pool.connect()
17+
console.info("🟩 DB connected")
18+
} catch (err) {
19+
console.error("🟥 DB connection failed: ", err)
20+
process.exit(1)
21+
}
22+
}
23+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
CREATE TABLE IF NOT EXISTS "notes" (
2+
"id" serial PRIMARY KEY NOT NULL,
3+
"text" text NOT NULL,
4+
"created_at" timestamp DEFAULT now() NOT NULL,
5+
"updated_at" timestamp DEFAULT now() NOT NULL,
6+
"deleted_at" timestamp,
7+
"is_deleted" boolean DEFAULT false NOT NULL,
8+
"user_id" integer NOT NULL
9+
);
10+
--> statement-breakpoint
11+
CREATE TABLE "users" (
12+
"id" serial PRIMARY KEY NOT NULL,
13+
"name" varchar(100),
14+
"password" varchar(200) NOT NULL,
15+
"email" text NOT NULL,
16+
"created_at" timestamp DEFAULT now() NOT NULL,
17+
"updated_at" timestamp DEFAULT now() NOT NULL,
18+
"is_deleted" boolean DEFAULT false,
19+
CONSTRAINT "users_email_unique" UNIQUE("email")
20+
);
21+
--> statement-breakpoint
22+
ALTER TABLE "notes" ADD CONSTRAINT "notes_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
{
2+
"id": "3529b3f7-f411-4547-b669-c2ed311cb5b9",
3+
"prevId": "00000000-0000-0000-0000-000000000000",
4+
"version": "7",
5+
"dialect": "postgresql",
6+
"tables": {
7+
"public.notes": {
8+
"name": "notes",
9+
"schema": "",
10+
"columns": {
11+
"id": {
12+
"name": "id",
13+
"type": "serial",
14+
"primaryKey": true,
15+
"notNull": true
16+
},
17+
"text": {
18+
"name": "text",
19+
"type": "text",
20+
"primaryKey": false,
21+
"notNull": true
22+
},
23+
"created_at": {
24+
"name": "created_at",
25+
"type": "timestamp",
26+
"primaryKey": false,
27+
"notNull": true,
28+
"default": "now()"
29+
},
30+
"updated_at": {
31+
"name": "updated_at",
32+
"type": "timestamp",
33+
"primaryKey": false,
34+
"notNull": true,
35+
"default": "now()"
36+
},
37+
"deleted_at": {
38+
"name": "deleted_at",
39+
"type": "timestamp",
40+
"primaryKey": false,
41+
"notNull": false
42+
},
43+
"is_deleted": {
44+
"name": "is_deleted",
45+
"type": "boolean",
46+
"primaryKey": false,
47+
"notNull": true,
48+
"default": false
49+
},
50+
"user_id": {
51+
"name": "user_id",
52+
"type": "integer",
53+
"primaryKey": false,
54+
"notNull": true
55+
}
56+
},
57+
"indexes": {},
58+
"foreignKeys": {
59+
"notes_user_id_users_id_fk": {
60+
"name": "notes_user_id_users_id_fk",
61+
"tableFrom": "notes",
62+
"tableTo": "users",
63+
"columnsFrom": [
64+
"user_id"
65+
],
66+
"columnsTo": [
67+
"id"
68+
],
69+
"onDelete": "cascade",
70+
"onUpdate": "no action"
71+
}
72+
},
73+
"compositePrimaryKeys": {},
74+
"uniqueConstraints": {},
75+
"policies": {},
76+
"checkConstraints": {},
77+
"isRLSEnabled": false
78+
},
79+
"public.users": {
80+
"name": "users",
81+
"schema": "",
82+
"columns": {
83+
"id": {
84+
"name": "id",
85+
"type": "serial",
86+
"primaryKey": true,
87+
"notNull": true
88+
},
89+
"name": {
90+
"name": "name",
91+
"type": "varchar(100)",
92+
"primaryKey": false,
93+
"notNull": false
94+
},
95+
"password": {
96+
"name": "password",
97+
"type": "varchar(200)",
98+
"primaryKey": false,
99+
"notNull": true
100+
},
101+
"email": {
102+
"name": "email",
103+
"type": "text",
104+
"primaryKey": false,
105+
"notNull": true
106+
},
107+
"created_at": {
108+
"name": "created_at",
109+
"type": "timestamp",
110+
"primaryKey": false,
111+
"notNull": true,
112+
"default": "now()"
113+
},
114+
"updated_at": {
115+
"name": "updated_at",
116+
"type": "timestamp",
117+
"primaryKey": false,
118+
"notNull": true,
119+
"default": "now()"
120+
},
121+
"is_deleted": {
122+
"name": "is_deleted",
123+
"type": "boolean",
124+
"primaryKey": false,
125+
"notNull": false,
126+
"default": false
127+
}
128+
},
129+
"indexes": {},
130+
"foreignKeys": {},
131+
"compositePrimaryKeys": {},
132+
"uniqueConstraints": {
133+
"users_email_unique": {
134+
"name": "users_email_unique",
135+
"nullsNotDistinct": false,
136+
"columns": [
137+
"email"
138+
]
139+
}
140+
},
141+
"policies": {},
142+
"checkConstraints": {},
143+
"isRLSEnabled": false
144+
}
145+
},
146+
"enums": {},
147+
"schemas": {},
148+
"sequences": {},
149+
"roles": {},
150+
"policies": {},
151+
"views": {},
152+
"_meta": {
153+
"columns": {},
154+
"schemas": {},
155+
"tables": {}
156+
}
157+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "7",
3+
"dialect": "postgresql",
4+
"entries": [
5+
{
6+
"idx": 0,
7+
"version": "7",
8+
"when": 1779956902611,
9+
"tag": "0000_soft_zarek",
10+
"breakpoints": true
11+
}
12+
]
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
3+
import { notes } from "../../module/note/model";
4+
import { users } from "../../module/user/model";
5+
6+
export const table = {
7+
notes,
8+
users,
9+
} as const
10+
11+
export type Table = typeof table
12+
13+
export {
14+
notes,
15+
users
16+
//
17+
} // drizzle look at it to maintain migration
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { env } from './utils/load-env';
2+
import { defineConfig } from 'drizzle-kit';
3+
4+
export default defineConfig({
5+
out: './src/database/migrations/drizzle',
6+
schema: './src/database/model/index.ts',
7+
dialect: 'postgresql',
8+
dbCredentials: {
9+
url: env.PG_DATABASE_URL!,
10+
},
11+
});

0 commit comments

Comments
 (0)