diff --git a/Backend/.env.example b/Backend/.env.example new file mode 100644 index 0000000..88ff480 --- /dev/null +++ b/Backend/.env.example @@ -0,0 +1 @@ +DATABASE_URL=postgres://username:password@localhost:5432/database_name diff --git a/Backend/README.md b/Backend/README.md index d9b9b54..eecc946 100644 --- a/Backend/README.md +++ b/Backend/README.md @@ -1,15 +1,47 @@ # backend -To install dependencies: +## Install ```bash bun install ``` -To run: +## Environment + +Create a `.env` file: ```bash -bun run index.ts +cp .env.example .env ``` -This project was created using `bun init` in bun v1.3.5. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime. +Set: + +```bash +DATABASE_URL=postgres://username:password@localhost:5432/database_name +``` + +## Run app + +```bash +bun run dev +``` + +## Drizzle ORM + +Generate migrations: + +```bash +bun run db:generate +``` + +Apply migrations: + +```bash +bun run db:migrate +``` + +Open Drizzle Studio: + +```bash +bun run db:studio +``` diff --git a/Backend/db/client.ts b/Backend/db/client.ts new file mode 100644 index 0000000..7f942e8 --- /dev/null +++ b/Backend/db/client.ts @@ -0,0 +1,15 @@ +import 'dotenv/config'; +import { drizzle } from 'drizzle-orm/node-postgres'; +import { Pool } from 'pg'; + +import * as schema from './schema'; + +if (!process.env.DATABASE_URL) { + throw new Error('DATABASE_URL is not set'); +} + +export const pool = new Pool({ + connectionString: process.env.DATABASE_URL, +}); + +export const db = drizzle(pool, { schema }); diff --git a/Backend/db/schema.ts b/Backend/db/schema.ts new file mode 100644 index 0000000..531f33a --- /dev/null +++ b/Backend/db/schema.ts @@ -0,0 +1,8 @@ +import { pgTable, timestamp, uuid, varchar } from 'drizzle-orm/pg-core'; + +export const users = pgTable('users', { + id: uuid('id').defaultRandom().primaryKey(), + email: varchar('email', { length: 255 }).notNull().unique(), + name: varchar('name', { length: 255 }).notNull(), + createdAt: timestamp('created_at', { withTimezone: true }).defaultNow().notNull(), +}); diff --git a/Backend/drizzle.config.ts b/Backend/drizzle.config.ts new file mode 100644 index 0000000..cffe4d6 --- /dev/null +++ b/Backend/drizzle.config.ts @@ -0,0 +1,17 @@ +import 'dotenv/config'; +import { defineConfig } from 'drizzle-kit'; + +if (!process.env.DATABASE_URL) { + throw new Error('DATABASE_URL is not set'); +} + +export default defineConfig({ + schema: './db/schema.ts', + out: './drizzle', + dialect: 'postgresql', + dbCredentials: { + url: process.env.DATABASE_URL, + }, + strict: true, + verbose: true, +}); diff --git a/Backend/implementation-plan.mdc b/Backend/implementation-plan.mdc new file mode 100644 index 0000000..3dca909 --- /dev/null +++ b/Backend/implementation-plan.mdc @@ -0,0 +1,3 @@ +--- +alwaysApply: true +--- diff --git a/Backend/index.ts b/Backend/index.ts index b7a8e53..f0d3e97 100644 --- a/Backend/index.ts +++ b/Backend/index.ts @@ -8,4 +8,4 @@ app.get('/', (req, res) => { app.listen(3000, () => { console.log('Server is running on port 3000'); -}); \ No newline at end of file +}); diff --git a/Backend/package.json b/Backend/package.json index 717e4f1..2cbf40b 100644 --- a/Backend/package.json +++ b/Backend/package.json @@ -11,6 +11,7 @@ "@types/jsonwebtoken": "^9.0.10", "@types/node": "^25.2.1", "@types/pg": "^8.16.0", + "drizzle-kit": "^0.31.0", "ts-node": "^10.9.2" }, "peerDependencies": { @@ -22,6 +23,7 @@ "bcrypt": "^6.0.0", "cors": "^2.8.6", "dotenv": "^17.2.4", + "drizzle-orm": "^0.44.0", "express": "^5.2.1", "helmet": "^8.1.0", "jsonwebtoken": "^9.0.3", @@ -31,5 +33,13 @@ "socket.io": "^4.8.3", "uuid": "^13.0.0", "zod": "^4.3.6" + }, + "scripts": { + "start": "bun run index.ts", + "dev": "bun --watch index.ts", + "db:generate": "drizzle-kit generate", + "db:migrate": "drizzle-kit migrate", + "db:push": "drizzle-kit push", + "db:studio": "drizzle-kit studio" } }