-
Notifications
You must be signed in to change notification settings - Fork 0
Database Configuration
github-actions[bot] edited this page Sep 22, 2025
·
2 revisions
@waforix/mocha supports multiple database systems: SQLite, PostgreSQL, and MySQL. This guide covers setup, configuration, and optimization for each database type.
The database system handles:
- Event storage (messages, voice, reactions, etc.)
- User and guild statistics
- Configuration data
- Analytics and metrics
- Automatic migrations
SQLite is perfect for development and small to medium deployments.
import { Client } from '@waforix/mocha';
const client = new Client({
token: process.env.DISCORD_TOKEN!,
database: {
type: 'sqlite',
path: './data/stats.db'
}
});database: {
type: 'sqlite',
path: './data/stats.db',
// Optional SQLite-specific options
options: {
busyTimeout: 30000,
journal: 'WAL', // Write-Ahead Logging for better performance
synchronous: 'NORMAL',
cacheSize: 2000,
tempStore: 'MEMORY'
}
}- Use WAL mode for better concurrent access
- Place database file on fast storage (SSD)
- Regular VACUUM operations for maintenance
- Consider file-based backups
PostgreSQL is recommended for production deployments with high load.
# Ubuntu/Debian
sudo apt update
sudo apt install postgresql postgresql-contrib
# CentOS/RHEL
sudo yum install postgresql postgresql-server postgresql-contrib
# macOS
brew install postgresql# Switch to postgres user
sudo -u postgres psql
-- Create database
CREATE DATABASE mocha_stats;
-- Create user
CREATE USER mocha_user WITH PASSWORD 'secure_password';
-- Grant privileges
GRANT ALL PRIVILEGES ON DATABASE mocha_stats TO mocha_user;
-- Exit
\qdatabase: {
type: 'postgres',
host: 'localhost',
port: 5432,
database: 'mocha_stats',
username: 'mocha_user',
password: 'secure_password'
}database: {
type: 'postgres',
host: process.env.DB_HOST || 'localhost',
port: parseInt(process.env.DB_PORT || '5432'),
database: process.env.DB_NAME || 'mocha_stats',
username: process.env.DB_USER || 'mocha_user',
password: process.env.DB_PASSWORD!,
// SSL Configuration
ssl: process.env.NODE_ENV === 'production' ? {
rejectUnauthorized: false
} : false,
// Connection Pool Settings
pool: {
min: 2,
max: 10,
acquireTimeoutMillis: 30000,
idleTimeoutMillis: 30000
},
// Additional options
options: {
connectTimeout: 60000,
commandTimeout: 30000,
requestTimeout: 30000
}
}-- Recommended postgresql.conf settings
shared_buffers = 256MB
effective_cache_size = 1GB
maintenance_work_mem = 64MB
checkpoint_completion_target = 0.9
wal_buffers = 16MB
default_statistics_target = 100
random_page_cost = 1.1
effective_io_concurrency = 200MySQL is supported for organizations already using MySQL infrastructure.
# Ubuntu/Debian
sudo apt update
sudo apt install mysql-server
# CentOS/RHEL
sudo yum install mysql-server
# macOS
brew install mysql-- Connect to MySQL
mysql -u root -p
-- Create database
CREATE DATABASE mocha_stats CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- Create user
CREATE USER 'mocha_user'@'localhost' IDENTIFIED BY 'secure_password';
-- Grant privileges
GRANT ALL PRIVILEGES ON mocha_stats.* TO 'mocha_user'@'localhost';
FLUSH PRIVILEGES;
-- Exit
EXIT;database: {
type: 'mysql',
host: 'localhost',
port: 3306,
database: 'mocha_stats',
username: 'mocha_user',
password: 'secure_password'
}database: {
type: 'mysql',
host: process.env.DB_HOST || 'localhost',
port: parseInt(process.env.DB_PORT || '3306'),
database: process.env.DB_NAME || 'mocha_stats',
username: process.env.DB_USER || 'mocha_user',
password: process.env.DB_PASSWORD!,
// SSL Configuration
ssl: process.env.NODE_ENV === 'production' ? {
ca: process.env.DB_SSL_CA,
cert: process.env.DB_SSL_CERT,
key: process.env.DB_SSL_KEY
} : undefined,
// Connection Pool Settings
pool: {
min: 2,
max: 10,
acquireTimeoutMillis: 30000,
idleTimeoutMillis: 30000
},
// MySQL-specific options
options: {
charset: 'utf8mb4',
timezone: 'Z',
acquireTimeout: 60000,
timeout: 60000,
reconnect: true
}
}Use environment variables for different environments:
// .env file
DB_TYPE=postgres
DB_HOST=localhost
DB_PORT=5432
DB_NAME=mocha_stats
DB_USER=mocha_user
DB_PASSWORD=secure_password
DB_SSL=true
// Configuration
database: {
type: process.env.DB_TYPE as 'sqlite' | 'postgres' | 'mysql',
host: process.env.DB_HOST,
port: parseInt(process.env.DB_PORT || '5432'),
database: process.env.DB_NAME!,
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
path: process.env.DB_PATH, // For SQLite
ssl: process.env.DB_SSL === 'true'
}@waforix/mocha automatically handles database migrations:
const client = new Client({
token: process.env.DISCORD_TOKEN!,
database: {
type: 'postgres',
// ... connection details
}
});
// Migrations run automatically on client initialization
await client.connect();// Get database instance for advanced operations
const db = await client.getDatabase();
// Migrations are handled automatically by the client
// For manual database operations, use the database instance directly# Simple file copy (when database is not in use)
cp ./data/stats.db ./backups/stats-$(date +%Y%m%d).db
# Online backup using SQLite command
sqlite3 ./data/stats.db ".backup ./backups/stats-$(date +%Y%m%d).db"# Full database backup
pg_dump -h localhost -U mocha_user mocha_stats > backup-$(date +%Y%m%d).sql
# Compressed backup
pg_dump -h localhost -U mocha_user mocha_stats | gzip > backup-$(date +%Y%m%d).sql.gz
# Restore
psql -h localhost -U mocha_user mocha_stats < backup.sql# Full database backup
mysqldump -h localhost -u mocha_user -p mocha_stats > backup-$(date +%Y%m%d).sql
# Compressed backup
mysqldump -h localhost -u mocha_user -p mocha_stats | gzip > backup-$(date +%Y%m%d).sql.gz
# Restore
mysql -h localhost -u mocha_user -p mocha_stats < backup.sql// Get database statistics
const stats = await client.getDatabaseStats();
console.log('Database Statistics:', {
totalEvents: stats.totalEvents,
totalUsers: stats.totalUsers,
totalGuilds: stats.totalGuilds,
databaseSize: stats.databaseSize,
lastOptimized: stats.lastOptimized
});// Enable query logging (development only)
const client = new Client({
database: {
// ... connection details
options: {
logging: process.env.NODE_ENV === 'development'
}
}
});Connection Timeout:
database: {
// ... other options
options: {
connectTimeout: 60000,
acquireTimeout: 60000
}
}Too Many Connections:
database: {
// ... other options
pool: {
min: 1,
max: 5 // Reduce max connections
}
}SSL Certificate Issues:
database: {
// ... other options
ssl: {
rejectUnauthorized: false // Only for development
}
}// Check database connectivity
async function checkDatabaseHealth() {
try {
const db = await client.getDatabase();
await db.select().from('users').limit(1);
console.log('Database connection: OK');
return true;
} catch (error) {
console.error('Database connection: FAILED', error);
return false;
}
}
// Run health check periodically
setInterval(checkDatabaseHealth, 60000); // Every minute- Use Environment Variables - Never hardcode credentials
- Enable SSL in Production - Encrypt database connections
- Configure Connection Pooling - Optimize connection usage
- Regular Backups - Implement automated backup strategies
- Monitor Performance - Track query performance and database size
- Use Appropriate Database - SQLite for development, PostgreSQL/MySQL for production
- Keep Updated - Regularly update database software
- Security - Use strong passwords and limit access
For more information on client configuration, see Client Configuration.