Migrate database from MySQL to Supabase PostgreSQL

- Added @supabase/supabase-js client library
- Rewrote database.js to use Supabase API
- Updated server.js health check for Supabase
- Updated db.config.example.js with Supabase format
- Created comprehensive SUPABASE_SETUP.md guide
- Added SQL schema files for easy deployment
- Updated README_DB_CONFIG.md for Supabase

Benefits:
- Managed PostgreSQL database
- Built-in Row Level Security
- Real-time capabilities
- Easy monitoring via dashboard
- Free tier for development
This commit is contained in:
2025-12-21 15:40:57 +11:00
parent 5238fc8d22
commit 054cbf3e77
9 changed files with 872 additions and 276 deletions

View File

@@ -1,125 +1,86 @@
# Database Configuration Setup
# Database Configuration Setup - Supabase
## Overview
Database credentials are stored in a separate configuration file (`db.config.js`) that is **NOT committed to GitHub** for security reasons.
This project now uses **Supabase** (PostgreSQL) instead of MySQL.
## Files
### 1. `db.config.example.js` (Committed to Git)
Template file showing the required configuration structure.
Template file showing the required Supabase configuration structure.
### 2. `db.config.js` (NOT Committed - in .gitignore)
Contains actual database credentials. This file must be created manually.
Contains actual Supabase credentials. This file must be created manually.
### 3. `.gitignore`
Ensures `db.config.js` is never committed to the repository.
## Setup Instructions
## Quick Setup
### For Local Development
See **[SUPABASE_SETUP.md](SUPABASE_SETUP.md)** for detailed step-by-step instructions.
1. **Copy the example file:**
```bash
cp db.config.example.js db.config.js
```
### Quick Start
2. **Edit `db.config.js` with your credentials:**
1. **Create Supabase project** at [app.supabase.com](https://app.supabase.com)
2. **Copy credentials** from Project Settings → API
3. **Update `db.config.js`:**
```javascript
module.exports = {
host: 'localhost', // or your database host
user: 'your_username',
password: 'your_password',
database: 'appgconnect5_db',
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
supabaseUrl: 'https://xxxxx.supabase.co',
supabaseAnonKey: 'eyJhbGci...',
supabasePassword: 't1hWsackxbYzRIPD'
};
```
3. **Start the server:**
```bash
npm start
```
### For Production Deployment
1. **Pull the latest code on your server:**
```bash
git pull origin main
```
2. **Create `db.config.js` on the production server:**
```bash
nano db.config.js
# or
vi db.config.js
```
3. **Add your production database credentials:**
```javascript
module.exports = {
host: 'your-production-db-host.com',
user: 'production_user',
password: 'secure_production_password',
database: 'appgconnect5_db',
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
};
```
4. **Save and restart the server:**
```bash
pm2 restart connect5
# or your restart command
```
4. **Run SQL schema** in Supabase SQL Editor (see SUPABASE_SETUP.md)
5. **Start server:** `npm start`
## Security Features
✅ **Credentials not in git** - `db.config.js` is in `.gitignore`
✅ **Template provided** - `db.config.example.js` shows the structure
✅ **Comments in code** - Clear instructions in `database.js`
✅ **Supabase RLS** - Row Level Security policies protect data
✅ **Separate config** - Easy to update without touching main code
## Troubleshooting
### Error: Cannot find module './db.config.js'
**Solution:** You need to create the `db.config.js` file:
**Solution:** Create the `db.config.js` file:
```bash
cp db.config.example.js db.config.js
# Then edit with your credentials
# Then edit with your Supabase credentials
```
### Error: Access denied for user
### Error: Invalid API key
**Solution:** Check your credentials in `db.config.js`:
- Verify username
- Verify password
- Check host address
- Ensure user has proper permissions
- Verify `supabaseUrl` is correct
- Verify `supabaseAnonKey` (should start with `eyJ...`)
- Get credentials from Supabase dashboard → Project Settings → API
### Connection timeout
### Error: Table 'players' does not exist
**Solution:**
- Check if MySQL server is running
- Verify firewall allows connection
- Check host address is correct
- Run the SQL schema in Supabase SQL Editor
- See SUPABASE_SETUP.md Step 4 for the complete schema
## Important Notes
⚠️ **NEVER commit `db.config.js` to git**
⚠️ **Keep production credentials secure**
⚠️ **Use different credentials for dev/prod**
⚠️ **Regularly rotate passwords**
⚠️ **Keep credentials secure**
⚠️ **Use different projects for dev/prod**
⚠️ **The anon key is safe for client-side use** (protected by RLS)
## File Structure
```
Connect-5/
├── db.config.example.js ← Template (in git)
├── db.config.js ← Your credentials (NOT in git)
├── .gitignore ← Protects db.config.js
├── database.js ← Imports from db.config.js
── README_DB_CONFIG.md ← This file
├── db.config.example.js ← Template (in git)
├── db.config.js ← Your credentials (NOT in git)
├── .gitignore ← Protects db.config.js
├── database.js ← Imports from db.config.js
── supabase-functions.sql ← Helper functions for Supabase
├── SUPABASE_SETUP.md ← Detailed setup guide
└── README_DB_CONFIG.md ← This file
```