|
1
|
type ConnectionStringOptions = { |
|
2
|
user?: string |
|
3
|
password?: string |
|
4
|
host?: string |
|
5
|
db: string |
|
6
|
} |
|
7
|
|
|
8
|
export function encodeDbValue(value: string): string { |
|
9
|
return encodeURIComponent(value) |
|
10
|
} |
|
11
|
|
|
12
|
export default ({ user, password, host, db }: ConnectionStringOptions) => { |
|
13
|
let connection = `postgres://` |
|
14
|
if (user) { |
|
15
|
connection += encodeDbValue(user) |
|
16
|
} |
|
17
|
|
|
18
|
if (password) { |
|
19
|
connection += `:${encodeDbValue(password)}` |
|
20
|
} |
|
21
|
|
|
22
|
if (user || password) { |
|
23
|
connection += "@" |
|
24
|
} |
|
25
|
|
|
26
|
connection += `${host}/${db}` |
|
27
|
|
|
28
|
return connection |
|
29
|
} |