deckhearth/api/test-db-local.js
Randall Stillwell d71d85ee43 Add database test endpoint for local debugging
🔧 Local Development Debugging:
- Add api/test-db-local.js for database connection testing
- Helps diagnose local environment issues
- Tests table existence and user data

Note: Local Vercel dev having database connection issues
Consider testing on production environment instead
2025-07-22 08:13:20 -05:00

63 lines
No EOL
1.8 KiB
JavaScript

const { Pool } = require('pg');
export default async function handler(req, res) {
try {
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: process.env.NODE_ENV === 'production' ? { rejectUnauthorized: false } : false,
});
const client = await pool.connect();
// Test basic connection
const result = await client.query('SELECT NOW() as current_time');
// Test if users table exists
const tableCheck = await client.query(`
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name IN ('users', 'roles', 'user_roles')
`);
// If users table exists, check for test user
let userCheck = null;
if (tableCheck.rows.some(row => row.table_name === 'users')) {
try {
userCheck = await client.query(
'SELECT id, username, email FROM users WHERE username = $1',
['testuser']
);
} catch (err) {
userCheck = { error: err.message };
}
}
client.release();
res.status(200).json({
success: true,
message: 'Database connection test',
timestamp: result.rows[0].current_time,
environment: {
NODE_ENV: process.env.NODE_ENV,
has_database_url: !!process.env.DATABASE_URL,
has_jwt_secret: !!process.env.JWT_SECRET
},
tables: tableCheck.rows.map(row => row.table_name),
testUser: userCheck ? {
exists: userCheck.rows ? userCheck.rows.length > 0 : false,
data: userCheck.rows ? userCheck.rows[0] : null,
error: userCheck.error || null
} : null
});
} catch (error) {
console.error('Database test error:', error);
res.status(500).json({
success: false,
error: error.message,
stack: error.stack
});
}
}