- Implement PostgreSQL database schema with users and bookmarks tables - Add database connection pooling with retry logic and error handling - Create migration system with automatic schema initialization - Add database CLI tools for management (init, status, validate, etc.) - Include comprehensive error handling and diagnostics - Add development seed data and testing utilities - Implement health monitoring and connection pool statistics - Create detailed documentation and troubleshooting guide Database features: - Users table with authentication fields and email verification - Bookmarks table with user association and metadata - Proper indexes for performance optimization - Automatic timestamp triggers - Transaction support with rollback handling - Connection pooling (20 max connections, 30s idle timeout) - Graceful shutdown handling CLI commands available: - npm run db:init - Initialize database - npm run db:status - Check database status - npm run db:validate - Validate schema - npm run db:test - Run database tests - npm run db:diagnostics - Full diagnostics
96 lines
3.1 KiB
JavaScript
Executable File
96 lines
3.1 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Database CLI utility for managing the bookmark manager database
|
|
* Usage: node scripts/db-cli.js <command>
|
|
*/
|
|
|
|
require('dotenv').config();
|
|
const dbInitializer = require('../src/database/init');
|
|
const dbConnection = require('../src/database/connection');
|
|
const dbUtils = require('../src/database/utils');
|
|
|
|
const commands = {
|
|
init: 'Initialize database with migrations',
|
|
status: 'Show database status and diagnostics',
|
|
reset: 'Reset database (development only)',
|
|
validate: 'Validate database schema',
|
|
cleanup: 'Clean up expired tokens and old data',
|
|
diagnostics: 'Run comprehensive database diagnostics',
|
|
help: 'Show this help message'
|
|
};
|
|
|
|
async function runCommand(command) {
|
|
try {
|
|
switch (command) {
|
|
case 'init':
|
|
console.log('🚀 Initializing database...');
|
|
await dbInitializer.initialize();
|
|
break;
|
|
|
|
case 'status':
|
|
console.log('📊 Getting database status...');
|
|
const status = await dbInitializer.getStatus();
|
|
console.log(JSON.stringify(status, null, 2));
|
|
break;
|
|
|
|
case 'reset':
|
|
if (process.env.NODE_ENV === 'production') {
|
|
console.error('❌ Reset is not allowed in production');
|
|
process.exit(1);
|
|
}
|
|
console.log('⚠️ Resetting database...');
|
|
await dbInitializer.reset();
|
|
break;
|
|
|
|
case 'validate':
|
|
console.log('🔍 Validating database schema...');
|
|
const validation = await dbUtils.validateSchema();
|
|
console.log(JSON.stringify(validation, null, 2));
|
|
|
|
if (validation.valid) {
|
|
console.log('✅ Schema validation passed');
|
|
} else {
|
|
console.log('❌ Schema validation failed');
|
|
process.exit(1);
|
|
}
|
|
break;
|
|
|
|
case 'cleanup':
|
|
console.log('🧹 Running database cleanup...');
|
|
await dbUtils.cleanup();
|
|
break;
|
|
|
|
case 'diagnostics':
|
|
console.log('🔍 Running database diagnostics...');
|
|
const diagnostics = await dbUtils.diagnostics();
|
|
console.log(JSON.stringify(diagnostics, null, 2));
|
|
break;
|
|
|
|
case 'help':
|
|
default:
|
|
console.log('📖 Database CLI Commands:');
|
|
console.log('');
|
|
Object.entries(commands).forEach(([cmd, desc]) => {
|
|
console.log(` ${cmd.padEnd(12)} - ${desc}`);
|
|
});
|
|
console.log('');
|
|
console.log('Usage: node scripts/db-cli.js <command>');
|
|
break;
|
|
}
|
|
} catch (error) {
|
|
console.error('❌ Command failed:', error.message);
|
|
process.exit(1);
|
|
} finally {
|
|
await dbConnection.close();
|
|
}
|
|
}
|
|
|
|
// Get command from command line arguments
|
|
const command = process.argv[2];
|
|
|
|
if (!command) {
|
|
runCommand('help');
|
|
} else {
|
|
runCommand(command);
|
|
} |