DatabaseSettings.utils.ts 4.2 KB

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