Skip to content

Commit 55add8a

Browse files
committed
Add database management scripts and update README for development setup
- Introduced new database management scripts: `clear-db.ts`, `create-superuser.ts`, `migrate.ts`, `pg.ts`, `pg-admin.ts`, and their respective stop scripts. - Added seeding functionality with `seed.ts` and `lib/seeder.ts` for populating the database with test data. - Updated `package.json` with new script commands for database operations. - Revised README to reflect the new setup instructions and available commands for development and database management.
1 parent 1421d34 commit 55add8a

13 files changed

+742
-28
lines changed

README.md

+148-27
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,171 @@
1-
# pariksa
1+
# Mint
22

3-
## Migrations
3+
A modern platform for conducting programming contests and managing coding problems.
4+
5+
## Development Setup
6+
7+
### Prerequisites
8+
9+
- Node.js 22.2.0+ and npm
10+
- Docker
11+
- [Bun](https://bun.sh) (recommended package manager)
12+
13+
### Quick Start
14+
15+
1. Clone the repository
16+
17+
2. Install dependencies
418

519
```bash
6-
npx drizzle-kit generate # generate SQL migration files based on your Drizzle schema
7-
npx drizzle-kit migrate # apply generated SQL migration files to your database
20+
bun install
821
```
922

10-
> [!NOTE]
11-
> Use `bun` instead of `npx` if you are using `bun`.
23+
3. Set up environment variables
1224

13-
---
25+
```bash
26+
cp .env.example .env
27+
```
1428

15-
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).
29+
4. Start the database
1630

17-
## Getting Started
31+
```bash
32+
bun pg
33+
```
1834

19-
First, run the development server:
35+
5. Run migrations and seed data
36+
37+
```bash
38+
bun db:migrate
39+
bun db:superuser # Create an admin user
40+
bun db:seed # Add test data (optional)
41+
```
42+
43+
6. Start the development server
2044

2145
```bash
22-
npm run dev
23-
# or
24-
yarn dev
25-
# or
26-
pnpm dev
27-
# or
2846
bun dev
2947
```
3048

31-
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
49+
Visit `http://localhost:3000` to see the application.
50+
51+
## Available Commands
52+
53+
### Development
54+
55+
```bash
56+
bun dev # Start development server
57+
bun build # Build for production
58+
bun start # Start production server
59+
bun lint # Run ESLint
60+
bun format # Format code with Prettier
61+
bun format:check # Check code formatting
62+
```
63+
64+
### Database Management
65+
66+
#### PostgreSQL Control
67+
68+
```bash
69+
bun pg # Start PostgreSQL container
70+
bun pg:stop # Stop and remove PostgreSQL container
71+
```
72+
73+
Connection Details:
74+
75+
- Host: `localhost` (from host) or `mint-postgres` (from containers)
76+
- Port: `5432`
77+
- Database: `mint`
78+
- Username: `postgres`
79+
- Password: `postgres`
80+
- URL: `postgres://postgres:postgres@localhost:5432/mint`
81+
82+
#### pgAdmin (Database UI)
83+
84+
```bash
85+
bun pg-admin # Start pgAdmin web interface
86+
bun pg-admin:stop # Stop and remove pgAdmin container
87+
```
88+
89+
Access Details:
90+
91+
- URL: http://localhost:5050
92+
93+
- Password: `admin`
94+
95+
#### Database Operations
96+
97+
```bash
98+
bun db:migrate # Generate and apply database migrations
99+
bun db:seed # Add test data to database
100+
bun db:clear # Clear all data from database
101+
bun db:superuser # Create an admin user interactively
102+
```
103+
104+
### Test Data
32105

33-
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
106+
The seeding process creates:
34107

35-
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.
108+
- Admin users (2)
109+
- Organizer users (3)
110+
- Regular users (5)
111+
- Organizations (3)
112+
- Problems per organization (2)
113+
- Contests per organization (2)
114+
- Groups per organization (2)
36115

37-
## Learn More
116+
All test users are created with password: `password123`
38117

39-
To learn more about Next.js, take a look at the following resources:
118+
## Project Structure
119+
120+
```bash
121+
mint/
122+
├── app/ # Next.js app router pages
123+
├── components/ # React components
124+
├── db/ # Database schema and migrations
125+
├── lib/ # Utility functions and shared logic
126+
├── public/ # Static assets
127+
└── scripts/ # CLI scripts for development
128+
```
40129

41-
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
42-
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
130+
## Troubleshooting
131+
132+
### Database Issues
133+
134+
1. If containers are already running:
135+
136+
```bash
137+
docker stop mint-postgres mint-pgadmin
138+
docker rm mint-postgres mint-pgadmin
139+
```
140+
141+
1. If ports are in use:
142+
143+
- Check if PostgreSQL is running locally: `sudo lsof -i :5432`
144+
- Check if something is using pgAdmin port: `sudo lsof -i :5050`
145+
146+
2. To completely reset:
147+
148+
```bash
149+
bun pg:stop
150+
bun pg-admin:stop
151+
bun pg
152+
bun pg-admin
153+
bun db:clear
154+
bun db:migrate
155+
bun db:seed
156+
```
157+
158+
> [!NOTE]
159+
> While Bun is recommended, you can still use npm by replacing `bun` with `npm run` in all commands.
43160
44-
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome!
161+
## Contributing
45162

46-
## Deploy on Vercel
163+
1. Fork the repository
164+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
165+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
166+
4. Push to the branch (`git push origin feature/amazing-feature`)
167+
5. Open a Pull Request
47168

48-
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
169+
## License
49170

50-
Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.
171+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

app/api/seed/route.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { NextResponse } from "next/server";
2+
import { seedDatabase, isDatabaseEmpty } from "@/lib/seeder";
3+
4+
export async function POST(request: Request) {
5+
try {
6+
// Optional: Check if database is already seeded
7+
const isEmpty = await isDatabaseEmpty();
8+
if (!isEmpty) {
9+
return NextResponse.json(
10+
{ error: "Database is not empty" },
11+
{ status: 400 }
12+
);
13+
}
14+
15+
// Optionally accept custom config
16+
const config = await request.json().catch(() => undefined);
17+
await seedDatabase(config);
18+
19+
return NextResponse.json({ message: "Database seeded successfully" });
20+
} catch (error) {
21+
console.error("Error in seed route:", error);
22+
return NextResponse.json(
23+
{ error: "Failed to seed database" },
24+
{ status: 500 }
25+
);
26+
}
27+
}

0 commit comments

Comments
 (0)