-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.ts
More file actions
89 lines (75 loc) · 2.48 KB
/
Copy pathdatabase.ts
File metadata and controls
89 lines (75 loc) · 2.48 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { Pool } from 'pg'
import mongoose from 'mongoose'
import dotenv from 'dotenv'
dotenv.config()
async function getDatabaseSecrets() {
try {
const VAULT_ADDR = process.env.VAULT_ADDR
const VAULT_TOKEN = process.env.VAULT_TOKEN
if (!VAULT_ADDR) {
throw new Error('❌ Vault address is incorrect')
}
if (!VAULT_TOKEN) {
throw new Error('❌ Vault environment variables/tokens are missing')
}
console.log('🔍 Fetching secrets from Vault...', `${VAULT_ADDR}`)
const response = await fetch(`${VAULT_ADDR}/v1/secret/database`, {
method: 'GET',
headers: {
'X-Vault-Token': VAULT_TOKEN,
Accept: 'application/json',
},
})
if (!response.ok) {
const errorText = await response.text()
throw new Error(
`❌ Vault API Error: ${response.status} ${response.statusText} - ${errorText}`
)
}
const data = await response.json()
console.log(
'🔍 Secrets fetched successfully in getDatabaseSecrets:',
data.data
)
return data.data
} catch (err) {
console.error('❌ Error fetching database secrets:', err)
throw err
}
}
const connectDatabases = async () => {
try {
const secrets = await getDatabaseSecrets()
console.log('🔍 Retrieved Secrets:', JSON.stringify(secrets, null, 2))
const {
PG_USER: pgUser,
PG_PASSWORD: pgPassword,
PG_DATABASE: pgDatabase,
MONGO_URI: mongoUri,
PG_HOST: pgHost,
} = secrets
if (!pgUser || !pgPassword || !pgDatabase || !pgHost) {
throw new Error('❌ Missing PostgreSQL credentials from Vault.')
}
if (!mongoUri) {
throw new Error('❌ Missing MongoDB URI from Vault.')
}
const pgPool = new Pool({
user: pgUser,
password: pgPassword,
host: pgHost,
database: pgDatabase,
port: 5432,
})
await pgPool.connect()
console.log('✅ PostgreSQL Connected Successfully')
await mongoose.connect(mongoUri)
console.log('✅ MongoDB Connected Successfully')
return { pgPool, mongoose }
} catch (err) {
console.error('❌ Database connection error:', err)
throw err
}
}
connectDatabases()
export { connectDatabases }