-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-db-connection.js
More file actions
70 lines (60 loc) · 2.18 KB
/
test-db-connection.js
File metadata and controls
70 lines (60 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { PrismaClient } from '@prisma/client';
import { withAccelerate } from '@prisma/extension-accelerate';
// Initialize Prisma Client with the same logic as the application
const initPrismaClient = () => {
try {
const databaseUrl = process.env.DATABASE_URL || '';
const directUrl = process.env.DIRECT_DATABASE_URL || '';
console.log('Database URL:', databaseUrl);
console.log('Direct URL:', directUrl);
console.log('Database URL type:', typeof databaseUrl);
console.log('Database URL starts with prisma://', databaseUrl.startsWith('prisma://'));
// Only use Accelerate extension if the URL starts with prisma://
if (databaseUrl.startsWith('prisma://')) {
console.log('Using Prisma Accelerate');
return new PrismaClient().$extends(withAccelerate());
} else {
console.log('Using standard Prisma client');
return new PrismaClient({
log: ['query', 'info', 'warn', 'error'],
});
}
} catch (error) {
console.error("Error initializing Prisma client:", error);
// Fallback to regular Prisma client without extension
return new PrismaClient();
}
};
const prisma = initPrismaClient();
async function testConnection() {
try {
console.log('Testing database connection...');
// Try to connect and run a simple query
const result = await prisma.$queryRaw`SELECT 1 as test`;
console.log('Connection successful!', result);
// Get database version
const version = await prisma.$queryRaw`SELECT version()`;
console.log('Database version:', version);
// Test a simple query to the User table
const userCount = await prisma.user.count();
console.log('User count:', userCount);
return { success: true, message: 'Database connection successful' };
} catch (error) {
console.error('Database connection failed:', error);
return { success: false, error: error.message };
} finally {
await prisma.$disconnect();
}
}
// Run the test
testConnection()
.then(result => {
console.log('Test completed:', result);
if (!result.success) {
process.exit(1);
}
})
.catch(error => {
console.error('Test failed:', error);
process.exit(1);
});