feat: add Drizzle ORM support and update README with environment setup and migration scripts
This commit is contained in:
1
Backend/.env.example
Normal file
1
Backend/.env.example
Normal file
@@ -0,0 +1 @@
|
|||||||
|
DATABASE_URL=postgres://username:password@localhost:5432/database_name
|
||||||
@@ -1,15 +1,47 @@
|
|||||||
# backend
|
# backend
|
||||||
|
|
||||||
To install dependencies:
|
## Install
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
bun install
|
bun install
|
||||||
```
|
```
|
||||||
|
|
||||||
To run:
|
## Environment
|
||||||
|
|
||||||
|
Create a `.env` file:
|
||||||
|
|
||||||
```bash
|
```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
|
||||||
|
```
|
||||||
|
|||||||
15
Backend/db/client.ts
Normal file
15
Backend/db/client.ts
Normal file
@@ -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 });
|
||||||
8
Backend/db/schema.ts
Normal file
8
Backend/db/schema.ts
Normal file
@@ -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(),
|
||||||
|
});
|
||||||
17
Backend/drizzle.config.ts
Normal file
17
Backend/drizzle.config.ts
Normal file
@@ -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,
|
||||||
|
});
|
||||||
3
Backend/implementation-plan.mdc
Normal file
3
Backend/implementation-plan.mdc
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
---
|
||||||
|
alwaysApply: true
|
||||||
|
---
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
"@types/jsonwebtoken": "^9.0.10",
|
"@types/jsonwebtoken": "^9.0.10",
|
||||||
"@types/node": "^25.2.1",
|
"@types/node": "^25.2.1",
|
||||||
"@types/pg": "^8.16.0",
|
"@types/pg": "^8.16.0",
|
||||||
|
"drizzle-kit": "^0.31.0",
|
||||||
"ts-node": "^10.9.2"
|
"ts-node": "^10.9.2"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
@@ -22,6 +23,7 @@
|
|||||||
"bcrypt": "^6.0.0",
|
"bcrypt": "^6.0.0",
|
||||||
"cors": "^2.8.6",
|
"cors": "^2.8.6",
|
||||||
"dotenv": "^17.2.4",
|
"dotenv": "^17.2.4",
|
||||||
|
"drizzle-orm": "^0.44.0",
|
||||||
"express": "^5.2.1",
|
"express": "^5.2.1",
|
||||||
"helmet": "^8.1.0",
|
"helmet": "^8.1.0",
|
||||||
"jsonwebtoken": "^9.0.3",
|
"jsonwebtoken": "^9.0.3",
|
||||||
@@ -31,5 +33,13 @@
|
|||||||
"socket.io": "^4.8.3",
|
"socket.io": "^4.8.3",
|
||||||
"uuid": "^13.0.0",
|
"uuid": "^13.0.0",
|
||||||
"zod": "^4.3.6"
|
"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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user