Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ If you would like to contribute, please read [CONTRIBUTING.md](docs/CONTRIBUTING
- **Theme Management:** next-themes for system preference detection

## Documentation

- [Changelog](docs/CHANGELOG.md) - Project changelog and release notes
- [Standards & Guidelines](docs/CLAUDE.md) - Development standards and project guidelines
- [Test Documentation](test/README.md) - Testing principles and structure
- [Database Setup](docs/DATABASE_SETUP.md) - How to set up the local database for development
122 changes: 122 additions & 0 deletions docs/DATABASE_SETUP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# Database Setup Guide for Local Development

This guide explains how to set up a local PostgreSQL database for development using the provided `setup-db.sh` script. The script handles the installation of PostgreSQL (if needed), database creation, schema setup, and data seeding to provide you with a fully functional test environment.

## Prerequisites

Before running the setup script, ensure you have:

1. **MacOS** - The script is designed for MacOS with Homebrew
2. **Homebrew** - The script uses Homebrew to install PostgreSQL if needed
- Install from [brew.sh](https://brew.sh) if not already installed
3. **Node.js** - Version 18 or higher is recommended
- The project uses Next.js and Prisma which require Node.js

## Quick Start

For developers who want to get started quickly:

```bash
# Clone the repository (if you haven't already)
git clone https://github.com/yourusername/yapli.git
cd yapli

# Make the setup script executable
chmod +x setup-db.sh

# Run the setup script
./setup-db.sh
```

The script will guide you through the setup process with clear prompts and colorful status messages.

## What the Setup Script Does

The `setup-db.sh` script performs the following tasks:

1. **Checks for PostgreSQL** - Verifies if PostgreSQL is installed and installs it via Homebrew if needed
2. **Ensures PostgreSQL is running** - Starts the PostgreSQL service if it's not already running
3. **Creates a database user** - Sets up the required Postgres superuser if needed
4. **Creates a database** - Creates the `yapli-local` database (or offers to recreate it if it exists)
5. **Sets up environment variables** - Creates or updates the `.env` file with the database connection string
6. **Applies database schema** - Runs Prisma migrations and ensures all tables are created
7. **Seeds the database** - Populates the database with test data for development

## Test Data

After running the setup script, your database will be populated with:

- **Test User**:
- Email: `testuser@example.com`
- Password: `password123`

- **Chat Rooms**:
- General (`roomUrl`: `696fcd`)
- Technology (`roomUrl`: `2zjbmc`)

- **Sample Messages**:
- Messages from users "Alice", "Bob", "Charlie", and "Dana"
- One message with a link preview

## Using the Database in Your Application

After setup, you can start the application with:

```bash
npm run dev
```

The application will connect to the local database using the connection string:

```
postgresql://postgres@localhost:5432/yapli-local
```

This connection string is automatically added to your `.env` file during setup.

## Common Issues and Troubleshooting

### PostgreSQL Not in PATH

If you see a warning about PostgreSQL not being in your PATH, consider adding it permanently:

```bash
echo 'export PATH="/opt/homebrew/opt/postgresql@16/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
```

### Database In Use Error

If you encounter "database removal failed: ERROR: database is being accessed by other users" when trying to recreate the database, the script will automatically attempt to terminate connections. If this fails:

1. Close any applications that might be using the database (pgAdmin, other terminal sessions, etc.)
2. Run the setup script again

### Schema Changes

If you've made changes to the Prisma schema (`prisma/schema.prisma`), you may need to update the database schema:

```bash
npx prisma generate # Update the Prisma client
npx prisma db push # Push schema changes to the database
```

## Manually Viewing the Database

To view the database tables after setup:

```bash
psql -d yapli-local -c '\dt' # List all tables
psql -d yapli-local -c 'SELECT * FROM chatrooms;' # View chat rooms
```

## Resetting the Database

If you want to completely reset the database to a clean state:

```bash
npx prisma db push --force-reset # Reset the database and apply schema
npm run db:seed # Re-seed with test data
```

Or simply run the setup script again and choose 'y' when asked if you want to recreate the database.
9 changes: 1 addition & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"build": "next build",
"start": "node -r dotenv/config server.js dotenv_config_path=.env.production",
"lint": "next lint",
"test": "jest",
"test:watch": "jest --watch"
"db:seed": "tsx prisma/seed.ts"
},
"dependencies": {
"@auth/prisma-adapter": "^2.9.1",
Expand All @@ -33,20 +32,14 @@
"devDependencies": {
"@eslint/eslintrc": "^3",
"@tailwindcss/postcss": "^4",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.3.0",
"@types/jest": "^30.0.0",
"@types/node": "^20",
"@types/react": "^19",
"@types/react-dom": "^19",
"eslint": "^9",
"eslint-config-next": "15.3.2",
"jest": "^30.0.4",
"jest-environment-jsdom": "^30.0.4",
"prisma": "^6.8.2",
"tailwind-merge": "^3.3.0",
"tailwindcss": "^4",
"ts-node": "^10.9.2",
"tsx": "^4.19.4",
"typescript": "^5"
}
Expand Down
91 changes: 91 additions & 0 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { PrismaClient } from '@prisma/client';
import { randomUUID } from 'crypto';

const prisma = new PrismaClient();

async function main() {
// Create a test user
const user = await prisma.user.create({
data: {
name: 'Test User',
email: 'testuser@example.com',
password: 'password123', // In a real app, this would be hashed
createdAt: new Date(),
updatedAt: new Date(),
}
});

// Create test chatrooms
const generalRoom = await prisma.chatroom.create({
data: {
title: 'General',
roomUrl: '696fcd',
userId: user.id,
},
});

const techRoom = await prisma.chatroom.create({
data: {
title: 'Technology',
roomUrl: '2zjbmc',
userId: user.id,
},
});

// Create some test messages
// Create messages with link previews
await prisma.message.create({
data: {
alias: 'Alice',
message: 'Hello everyone! Welcome to the General chat.',
chatroomId: generalRoom.id,
},
});

const bobMessage = await prisma.message.create({
data: {
alias: 'Bob',
message: 'Hey Alice! Check out this link https://yapli.chat',
chatroomId: generalRoom.id,
},
});

// Add a link preview to Bob's message
await prisma.linkPreview.create({
data: {
messageId: bobMessage.id,
url: 'https://yapli.chat',
title: 'Yapli Chat',
description: 'A low-friction chat application built with Next.js and Prisma',
domain: 'yapli.chat',
siteName: 'Yapli',
},
});

await prisma.message.create({
data: {
alias: 'Charlie',
message: 'Did you see the latest developments in AI?',
chatroomId: techRoom.id,
},
});

await prisma.message.create({
data: {
alias: 'Dana',
message: 'Yes! The new models are impressive.',
chatroomId: techRoom.id,
},
});

console.log('Database seeded with test data');
}

main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});
Loading