DatabaseSettings.utils.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. }
  118. const DB_USER_DESC = 'Database user (e.g postgres)'
  119. const DB_PASS_DESC = 'Database password'
  120. const DB_NAME_DESC = 'Database name (e.g postgres)'
  121. const PROJECT_REF_DESC = "Project's reference ID"
  122. const PORT_NUMBER_DESC = 'Port number (Use 5432 if using prepared statements)'
  123. // [Joshen] This is to the best of interpreting the syntax from the API response
  124. // // There's different format for PG13 (depending on authentication method being md5) and PG14
  125. export const constructConnStringSyntax = (
  126. connString: string,
  127. {
  128. selectedTab,
  129. usePoolerConnection,
  130. ref,
  131. cloudProvider,
  132. region,
  133. tld,
  134. portNumber,
  135. }: {
  136. selectedTab: 'uri' | 'psql' | 'golang' | 'jdbc' | 'dotnet' | 'nodejs' | 'php' | 'python'
  137. usePoolerConnection: boolean
  138. ref: string
  139. cloudProvider: string
  140. region: string
  141. tld: string
  142. portNumber: string
  143. }
  144. ) => {
  145. const isMd5 = connString.includes('options=reference')
  146. const poolerHostDetails = [
  147. { value: cloudProvider.toLocaleLowerCase(), tooltip: 'Cloud provider' },
  148. { value: '-0-', tooltip: undefined },
  149. { value: region, tooltip: "Project's region" },
  150. { value: `.pooler.briven.${tld}`, tooltip: undefined },
  151. ]
  152. const dbHostDetails = [
  153. { value: 'db.', tooltip: undefined },
  154. { value: ref, tooltip: PROJECT_REF_DESC },
  155. { value: `.briven.${tld}`, tooltip: undefined },
  156. ]
  157. if (selectedTab === 'uri' || selectedTab === 'nodejs') {
  158. if (isMd5) {
  159. return [
  160. { value: 'postgresql://', tooltip: undefined },
  161. { value: '[user]', tooltip: DB_USER_DESC },
  162. { value: ':', tooltip: undefined },
  163. { value: '[password]', tooltip: DB_PASS_DESC },
  164. { value: '@', tooltip: undefined },
  165. ...(usePoolerConnection ? poolerHostDetails : dbHostDetails),
  166. { value: ':', tooltip: undefined },
  167. { value: portNumber, tooltip: PORT_NUMBER_DESC },
  168. { value: '/', tooltip: undefined },
  169. { value: '[db-name]', tooltip: DB_NAME_DESC },
  170. ...(usePoolerConnection
  171. ? [
  172. { value: `?options=reference%3D`, tooltip: undefined },
  173. { value: ref, tooltip: PROJECT_REF_DESC },
  174. ]
  175. : []),
  176. ]
  177. } else {
  178. return [
  179. { value: 'postgresql://', tooltip: undefined },
  180. { value: '[user]', tooltip: DB_USER_DESC },
  181. ...(usePoolerConnection
  182. ? [
  183. { value: '.', tooltip: undefined },
  184. { value: ref, tooltip: PROJECT_REF_DESC },
  185. ]
  186. : []),
  187. { value: ':', tooltip: undefined },
  188. { value: '[password]', tooltip: DB_PASS_DESC },
  189. { value: '@', tooltip: undefined },
  190. ...(usePoolerConnection ? poolerHostDetails : dbHostDetails),
  191. { value: ':', tooltip: undefined },
  192. { value: portNumber, tooltip: PORT_NUMBER_DESC },
  193. { value: '/', tooltip: undefined },
  194. { value: '[db-name]', tooltip: DB_NAME_DESC },
  195. ]
  196. }
  197. }
  198. if (selectedTab === 'psql') {
  199. if (isMd5) {
  200. return [
  201. { value: 'psql "postgresql://', tooltip: undefined },
  202. { value: '[user]', tooltip: DB_USER_DESC },
  203. { value: ':', tooltip: undefined },
  204. { value: '[password]', tooltip: DB_PASS_DESC },
  205. { value: '@', tooltip: undefined },
  206. ...(usePoolerConnection ? poolerHostDetails : dbHostDetails),
  207. { value: ':', tooltip: undefined },
  208. { value: portNumber, tooltip: PORT_NUMBER_DESC },
  209. { value: '/', tooltip: undefined },
  210. { value: '[db-name]', tooltip: DB_NAME_DESC },
  211. ...(usePoolerConnection
  212. ? [
  213. { value: '?options=reference%3D', tooltip: undefined },
  214. { value: ref, tooltip: PROJECT_REF_DESC },
  215. ]
  216. : []),
  217. ]
  218. } else {
  219. return [
  220. { value: 'psql -h ', tooltip: undefined },
  221. ...(usePoolerConnection ? poolerHostDetails : dbHostDetails),
  222. { value: ' -p ', tooltip: undefined },
  223. { value: portNumber, tooltip: PORT_NUMBER_DESC },
  224. { value: ' -d ', tooltip: undefined },
  225. { value: '[db-name]', tooltip: DB_NAME_DESC },
  226. { value: ' -U ', tooltip: undefined },
  227. { value: '[user]', tooltip: DB_USER_DESC },
  228. ...(usePoolerConnection
  229. ? [
  230. { value: '.', tooltip: undefined },
  231. { value: ref, tooltip: PROJECT_REF_DESC },
  232. ]
  233. : []),
  234. ]
  235. }
  236. }
  237. if (selectedTab === 'golang' || selectedTab === 'php' || selectedTab === 'python') {
  238. if (isMd5) {
  239. return [
  240. { value: 'user=', tooltip: undefined },
  241. { value: '[user]', tooltip: DB_USER_DESC },
  242. { value: ' password=', tooltip: undefined },
  243. { value: '[password]', tooltip: DB_PASS_DESC },
  244. { value: ' host=', tooltip: undefined },
  245. ...(usePoolerConnection ? poolerHostDetails : dbHostDetails),
  246. { value: ' port=', tooltip: undefined },
  247. { value: portNumber, tooltip: PORT_NUMBER_DESC },
  248. { value: ' dbname=', tooltip: undefined },
  249. { value: '[db-name]', tooltip: DB_NAME_DESC },
  250. ...(usePoolerConnection
  251. ? [
  252. { value: ' options=reference=', tooltip: undefined },
  253. { value: ref, tooltip: PROJECT_REF_DESC },
  254. ]
  255. : []),
  256. ]
  257. } else {
  258. return [
  259. { value: 'user=', tooltip: undefined },
  260. { value: '[user]', tooltip: DB_USER_DESC },
  261. ...(usePoolerConnection
  262. ? [
  263. { value: '.', tooltip: undefined },
  264. { value: ref, tooltip: PROJECT_REF_DESC },
  265. ]
  266. : []),
  267. { value: ' password=', tooltip: undefined },
  268. { value: '[password]', tooltip: DB_PASS_DESC },
  269. { value: ' host=', tooltip: undefined },
  270. ...(usePoolerConnection ? poolerHostDetails : dbHostDetails),
  271. { value: ' port=', tooltip: undefined },
  272. { value: portNumber, tooltip: PORT_NUMBER_DESC },
  273. { value: ' dbname=', tooltip: undefined },
  274. { value: '[db-name]', tooltip: DB_NAME_DESC },
  275. ]
  276. }
  277. }
  278. if (selectedTab === 'jdbc') {
  279. if (isMd5) {
  280. return [
  281. { value: 'jdbc:postgresql://', tooltip: undefined },
  282. ...(usePoolerConnection ? poolerHostDetails : dbHostDetails),
  283. { value: ':', tooltip: undefined },
  284. { value: portNumber, tooltip: PORT_NUMBER_DESC },
  285. { value: '/', tooltip: undefined },
  286. { value: '[db-name]', tooltip: DB_NAME_DESC },
  287. { value: '?user=', tooltip: undefined },
  288. { value: '[user]', tooltip: DB_USER_DESC },
  289. { value: '&password=', tooltip: undefined },
  290. { value: '[password]', tooltip: DB_PASS_DESC },
  291. ...(usePoolerConnection
  292. ? [
  293. { value: '&options=reference%3D', tooltip: undefined },
  294. { value: ref, tooltip: PROJECT_REF_DESC },
  295. ]
  296. : []),
  297. ]
  298. } else {
  299. return [
  300. { value: 'jdbc:postgresql://', tooltip: undefined },
  301. ...(usePoolerConnection ? poolerHostDetails : dbHostDetails),
  302. { value: `:`, tooltip: undefined },
  303. { value: portNumber, tooltip: PORT_NUMBER_DESC },
  304. { value: '/', tooltip: undefined },
  305. { value: '[db-name]', tooltip: DB_NAME_DESC },
  306. { value: '?user=', tooltip: undefined },
  307. { value: '[user]', tooltip: DB_USER_DESC },
  308. ...(usePoolerConnection
  309. ? [
  310. { value: '.', tooltip: undefined },
  311. { value: ref, tooltip: PROJECT_REF_DESC },
  312. ]
  313. : []),
  314. { value: '&password=', tooltip: undefined },
  315. { value: '[password]', tooltip: DB_PASS_DESC },
  316. ]
  317. }
  318. }
  319. if (selectedTab === 'dotnet') {
  320. if (isMd5) {
  321. return [
  322. { value: 'User Id=', tooltip: undefined },
  323. { value: '[user]', tooltip: DB_USER_DESC },
  324. { value: ';Password=', tooltip: undefined },
  325. { value: '[password]', tooltip: DB_PASS_DESC },
  326. { value: ';Server=', tooltip: undefined },
  327. ...(usePoolerConnection ? poolerHostDetails : dbHostDetails),
  328. { value: ';Port=', tooltip: undefined },
  329. { value: portNumber, tooltip: PORT_NUMBER_DESC },
  330. { value: ';Database=', tooltip: undefined },
  331. { value: '[db-name]', tooltip: DB_NAME_DESC },
  332. ...(usePoolerConnection
  333. ? [
  334. { value: ";Options='reference=", tooltip: undefined },
  335. { value: ref, tooltip: PROJECT_REF_DESC },
  336. { value: "'", tooltip: undefined },
  337. ]
  338. : []),
  339. ]
  340. } else {
  341. return [
  342. { value: 'User Id=', tooltip: undefined },
  343. { value: '[user]', tooltip: DB_USER_DESC },
  344. ...(usePoolerConnection
  345. ? [
  346. { value: '.', tooltip: undefined },
  347. { value: ref, tooltip: PROJECT_REF_DESC },
  348. ]
  349. : []),
  350. { value: ';Password=', tooltip: undefined },
  351. { value: '[password]', tooltip: DB_PASS_DESC },
  352. { value: ';Server=', tooltip: undefined },
  353. ...(usePoolerConnection ? poolerHostDetails : dbHostDetails),
  354. { value: ';Port=', tooltip: undefined },
  355. { value: portNumber, tooltip: PORT_NUMBER_DESC },
  356. { value: ';Database=', tooltip: undefined },
  357. { value: '[db-name]', tooltip: DB_NAME_DESC },
  358. ]
  359. }
  360. }
  361. return []
  362. }