lightning-auth-and-payment
Version:
Lightning Network authentication and payment processing library for modern web applications
311 lines (225 loc) • 5.8 kB
Markdown
# 🗄️ Database Setup Guide
This guide explains how to configure different database systems for the Lightning Auth example.
## 📋 Supported Databases
- **SQLite** (Default) - Perfect for development
- **PostgreSQL** - Recommended for production
- **MySQL** - Supported via Prisma
- **SQL Server** - Supported via Prisma
## 🗃️ SQLite Setup (Default)
SQLite is the default database and requires no additional setup.
### Configuration
```env
DATABASE_URL="file:./dev.db"
```
### Pros
- ✅ Zero configuration
- ✅ File-based database
- ✅ Perfect for development
- ✅ No external dependencies
- ✅ Fast for small applications
### Cons
- ❌ Limited concurrent writes
- ❌ Not suitable for production scale
- ❌ No advanced features
### Setup Commands
```bash
# Generate Prisma client
npm run db:generate
# Run migrations
npm run db:migrate
# Seed with sample data
npm run db:seed
```
## 🐘 PostgreSQL Setup
PostgreSQL is recommended for production applications.
### Prerequisites
1. Install PostgreSQL
2. Create a database
3. Update connection string
### Installation
#### macOS (Homebrew)
```bash
brew install postgresql
brew services start postgresql
```
#### Ubuntu/Debian
```bash
sudo apt update
sudo apt install postgresql postgresql-contrib
sudo systemctl start postgresql
sudo systemctl enable postgresql
```
#### Windows
Download from [postgresql.org](https://www.postgresql.org/download/windows/)
### Database Creation
```bash
# Create database
createdb lightning_auth_example
# Or using psql
psql -U postgres
CREATE DATABASE lightning_auth_example;
\q
```
### Configuration
1. **Update `.env.local`**:
```env
DATABASE_URL="postgresql://username:password@localhost:5432/lightning_auth_example"
```
2. **Update Prisma schema**:
```bash
# Copy PostgreSQL schema
cp prisma/schema.postgresql.prisma prisma/schema.prisma
```
3. **Run setup**:
```bash
npm run db:generate
npm run db:migrate
npm run db:seed
```
### Connection String Format
```
postgresql://username:password@host:port/database
```
Examples:
```env
# Local development
DATABASE_URL="postgresql://postgres:password@localhost:5432/lightning_auth_example"
# Production (with SSL)
DATABASE_URL="postgresql://user:pass@prod-server.com:5432/lightning_auth_example?sslmode=require"
# With connection pooling
DATABASE_URL="postgresql://user:pass@prod-server.com:5432/lightning_auth_example?connection_limit=5&pool_timeout=20"
```
### Pros
- ✅ Production-ready
- ✅ Excellent performance
- ✅ ACID compliance
- ✅ Advanced features
- ✅ Scalable
- ✅ Concurrent access
### Cons
- ❌ Requires external server
- ❌ More complex setup
- ❌ Resource intensive
## 🐬 MySQL Setup
MySQL is supported via Prisma but requires additional configuration.
### Installation
#### macOS (Homebrew)
```bash
brew install mysql
brew services start mysql
```
#### Ubuntu/Debian
```bash
sudo apt update
sudo apt install mysql-server
sudo systemctl start mysql
sudo systemctl enable mysql
```
### Database Creation
```bash
# Login to MySQL
mysql -u root -p
# Create database
CREATE DATABASE lightning_auth_example;
CREATE USER 'lightning_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON lightning_auth_example.* TO 'lightning_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
```
### Configuration
1. **Update `.env.local`**:
```env
DATABASE_URL="mysql://lightning_user:your_password@localhost:3306/lightning_auth_example"
```
2. **Update Prisma schema**:
```prisma
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
```
3. **Run setup**:
```bash
npm run db:generate
npm run db:migrate
npm run db:seed
```
## 🔧 Database Management
### Prisma Studio
Visual database management:
```bash
npm run db:studio
```
Opens at [http://localhost:5555](http://localhost:5555)
### Common Commands
```bash
# Generate Prisma client
npm run db:generate
# Run migrations
npm run db:migrate
# Reset database (⚠️ DESTROYS ALL DATA)
npm run db:reset
# Seed with sample data
npm run db:seed
# Complete setup
npm run db:setup
```
### Migration Management
```bash
# Create new migration
npx prisma migrate dev --name add_new_field
# Apply migrations
npx prisma migrate deploy
# Reset and apply all migrations
npx prisma migrate reset
```
## 🚀 Production Considerations
### Connection Pooling
For production, consider using connection pooling:
```env
# With connection pooling
DATABASE_URL="postgresql://user:pass@host:5432/db?connection_limit=5&pool_timeout=20"
```
### SSL Configuration
```env
# Require SSL
DATABASE_URL="postgresql://user:pass@host:5432/db?sslmode=require"
# Allow SSL
DATABASE_URL="postgresql://user:pass@host:5432/db?sslmode=prefer"
```
### Environment Variables
```env
# Development
DATABASE_URL="file:./dev.db"
# Staging
DATABASE_URL="postgresql://user:pass@staging-host:5432/lightning_auth_staging"
# Production
DATABASE_URL="postgresql://user:pass@prod-host:5432/lightning_auth_prod?sslmode=require"
```
## 🔍 Troubleshooting
### Common Issues
1. **Connection refused**: Check if database server is running
2. **Authentication failed**: Verify username/password
3. **Database does not exist**: Create the database first
4. **Permission denied**: Check user permissions
5. **SSL errors**: Configure SSL settings
### Debug Commands
```bash
# Test database connection
npx prisma db pull
# Check Prisma client
npx prisma generate
# Validate schema
npx prisma validate
```
### Logs
Enable Prisma logging:
```env
# Add to .env.local
DEBUG=prisma:*
```
## 📚 Additional Resources
- [Prisma Documentation](https://www.prisma.io/docs)
- [PostgreSQL Documentation](https://www.postgresql.org/docs/)
- [MySQL Documentation](https://dev.mysql.com/doc/)
- [SQLite Documentation](https://www.sqlite.org/docs.html)