DatabaseSettings.utils.ts135 lines · main
| 1 | type ConnectionStrings = { |
| 2 | psql: string |
| 3 | uri: string |
| 4 | golang: string |
| 5 | jdbc: string |
| 6 | dotnet: string |
| 7 | nodejs: string |
| 8 | php: string |
| 9 | python: string |
| 10 | sqlalchemy: string |
| 11 | } |
| 12 | |
| 13 | export const getConnectionStrings = ({ |
| 14 | connectionInfo, |
| 15 | poolingInfo, |
| 16 | metadata, |
| 17 | }: { |
| 18 | connectionInfo: { |
| 19 | db_user: string |
| 20 | db_port: number |
| 21 | db_host: string |
| 22 | db_name: string |
| 23 | } |
| 24 | poolingInfo?: { |
| 25 | connectionString: string |
| 26 | db_user: string |
| 27 | db_port: number |
| 28 | db_host: string |
| 29 | db_name: string |
| 30 | } |
| 31 | metadata: { |
| 32 | projectRef?: string |
| 33 | pgVersion?: string |
| 34 | } |
| 35 | }): { |
| 36 | direct: ConnectionStrings |
| 37 | pooler: ConnectionStrings |
| 38 | } => { |
| 39 | const isMd5 = poolingInfo?.connectionString.includes('options=reference') |
| 40 | const { projectRef } = metadata |
| 41 | const password = '[YOUR-PASSWORD]' |
| 42 | |
| 43 | // Direct connection variables |
| 44 | const directUser = connectionInfo.db_user |
| 45 | const directPort = connectionInfo.db_port |
| 46 | const directHost = connectionInfo.db_host |
| 47 | const directName = connectionInfo.db_name |
| 48 | |
| 49 | // Pooler connection variables |
| 50 | const poolerUser = poolingInfo?.db_user |
| 51 | const poolerPort = poolingInfo?.db_port |
| 52 | const poolerHost = poolingInfo?.db_host |
| 53 | const poolerName = poolingInfo?.db_name |
| 54 | |
| 55 | // Direct connection strings |
| 56 | const directPsqlString = isMd5 |
| 57 | ? `psql "postgresql://${directUser}:${password}@${directHost}:${directPort}/${directName}"` |
| 58 | : `psql -h ${directHost} -p ${directPort} -d ${directName} -U ${directUser}` |
| 59 | |
| 60 | const directUriString = `postgresql://${directUser}:${password}@${directHost}:${directPort}/${directName}` |
| 61 | |
| 62 | const directGolangString = `DATABASE_URL=${directUriString}` |
| 63 | |
| 64 | const directJdbcString = `jdbc:postgresql://${directHost}:${directPort}/${directName}?user=${directUser}&password=${password}` |
| 65 | |
| 66 | // User Id=${directUser};Password=${password};Server=${directHost};Port=${directPort};Database=${directName}` |
| 67 | const directDotNetString = `{ |
| 68 | "ConnectionStrings": { |
| 69 | "DefaultConnection": "Host=${directHost};Database=${directName};Username=${directUser};Password=${password};SSL Mode=Require;Trust Server Certificate=true" |
| 70 | } |
| 71 | }` |
| 72 | |
| 73 | // `User Id=${poolerUser};Password=${password};Server=${poolerHost};Port=${poolerPort};Database=${poolerName}${isMd5 ? `;Options='reference=${projectRef}'` : ''}` |
| 74 | const poolerDotNetString = `{ |
| 75 | "ConnectionStrings": { |
| 76 | "DefaultConnection": "User Id=${poolerUser};Password=${password};Server=${poolerHost};Port=${poolerPort};Database=${poolerName}${isMd5 ? `;Options='reference=${projectRef}'` : ''}" |
| 77 | } |
| 78 | }` |
| 79 | |
| 80 | const directNodejsString = `DATABASE_URL=${directUriString}` |
| 81 | |
| 82 | // Pooler connection strings |
| 83 | const poolerPsqlString = isMd5 |
| 84 | ? `psql "postgresql://${poolerUser}:${password}@${poolerHost}:${poolerPort}/${poolerName}?options=reference%3D${projectRef}"` |
| 85 | : `psql -h ${poolerHost} -p ${poolerPort} -d ${poolerName} -U ${poolerUser}` |
| 86 | |
| 87 | const poolerUriString = poolingInfo?.connectionString ?? '' |
| 88 | |
| 89 | const nodejsPoolerUriString = `DATABASE_URL=${poolingInfo?.connectionString ?? ''}` |
| 90 | |
| 91 | const poolerGolangString = `user=${poolerUser} |
| 92 | password=${password} |
| 93 | host=${poolerHost} |
| 94 | port=${poolerPort} |
| 95 | dbname=${poolerName}${isMd5 ? `options=reference=${projectRef}` : ''}` |
| 96 | |
| 97 | const poolerJdbcString = `jdbc:postgresql://${poolerHost}:${poolerPort}/${poolerName}?user=${poolerUser}${isMd5 ? `&options=reference%3D${projectRef}` : ''}&password=${password}` |
| 98 | |
| 99 | const sqlalchemyString = `user=${directUser} |
| 100 | password=${password} |
| 101 | host=${directHost} |
| 102 | port=${directPort} |
| 103 | dbname=${directName}` |
| 104 | |
| 105 | const poolerSqlalchemyString = `user=${poolerUser} |
| 106 | password=${password} |
| 107 | host=${poolerHost} |
| 108 | port=${poolerPort} |
| 109 | dbname=${poolerName}` |
| 110 | |
| 111 | return { |
| 112 | direct: { |
| 113 | psql: directPsqlString, |
| 114 | uri: directUriString, |
| 115 | golang: directGolangString, |
| 116 | jdbc: directJdbcString, |
| 117 | dotnet: directDotNetString, |
| 118 | nodejs: directNodejsString, |
| 119 | php: directGolangString, |
| 120 | python: directGolangString, |
| 121 | sqlalchemy: sqlalchemyString, |
| 122 | }, |
| 123 | pooler: { |
| 124 | psql: poolerPsqlString, |
| 125 | uri: poolerUriString, |
| 126 | golang: poolerGolangString, |
| 127 | jdbc: poolerJdbcString, |
| 128 | dotnet: poolerDotNetString, |
| 129 | nodejs: nodejsPoolerUriString, |
| 130 | php: poolerGolangString, |
| 131 | python: poolerGolangString, |
| 132 | sqlalchemy: poolerSqlalchemyString, |
| 133 | }, |
| 134 | } |
| 135 | } |