forked from prisma/prisma
/
convertCredentials.ts
182 lines (154 loc) · 4.53 KB
/
convertCredentials.ts
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { DatabaseCredentials } from './types'
import * as NodeURL from 'url'
import { ConnectorType } from '@prisma/generator-helper'
import path from 'path'
export function credentialsToUri(credentials: DatabaseCredentials): string {
const type = databaseTypeToProtocol(credentials.type)
if (credentials.type === 'mongodb') {
return credentials.uri!
}
const url = new NodeURL.URL(type + '//')
if (credentials.host) {
url.hostname = credentials.host
}
if (credentials.type === 'postgresql') {
if (credentials.database) {
url.pathname = '/' + credentials.database
}
if (credentials.schema) {
url.searchParams.set('schema', credentials.schema)
}
if (credentials.socket) {
url.host = credentials.socket
}
} else if (credentials.type === 'mysql') {
url.pathname = '/' + (credentials.database || credentials.schema || '')
if (credentials.socket) {
url.searchParams.set('socket', credentials.socket)
}
}
if (credentials.ssl) {
url.searchParams.set('sslmode', 'prefer')
}
if (credentials.user) {
url.username = credentials.user
}
if (credentials.password) {
url.password = credentials.password
}
if (credentials.port) {
url.port = String(credentials.port)
}
url.host = `${url.hostname}${url.port ? `:${url.port}` : ''}`
if (credentials.extraFields) {
for (const [key, value] of Object.entries(credentials.extraFields)) {
url.searchParams.set(key, value)
}
}
// trim away empty pathnames
if (url.pathname === '/') {
url.pathname = ''
}
if (credentials.type === 'sqlite') {
// if `file:../parent-dev.db` return as it is
return credentials.uri!
}
return url.toString()
}
export function uriToCredentials(
connectionString: string,
): DatabaseCredentials {
let uri: NodeURL.URL
try {
uri = new NodeURL.URL(connectionString)
} catch (e) {
throw new Error(
'Invalid data source URL, see https://www.prisma.io/docs/reference/database-reference/connection-urls',
)
}
const type = protocolToConnectorType(uri.protocol)
// needed, as the URL implementation adds empty strings
const exists = (str): boolean => str && str.length > 0
if (type === 'mongodb') {
return {
type,
uri: connectionString, // todo: set authsource as database if not provided explicitly
}
}
const extraFields = {}
const schema = uri.searchParams.get('schema')
const socket = uri.searchParams.get('socket')
for (const [name, value] of uri.searchParams) {
if (!['schema', 'socket'].includes(name)) {
extraFields[name] = value
}
}
let database: string | undefined = undefined
let defaultSchema: string | undefined = undefined
if (type === 'sqlite' && uri.pathname) {
if (uri.pathname.startsWith('file:')) {
database = uri.pathname.slice(5)
}
if (uri.pathname.startsWith('sqlite:')) {
database = uri.pathname.slice(7)
} else {
database = path.basename(uri.pathname)
}
} else if (uri.pathname.length > 1) {
database = uri.pathname.slice(1)
if (type === 'postgresql' && !database) {
// use postgres as default, it's 99% accurate
// could also be template1 for example in rare cases
database = 'postgres'
}
}
if (type === 'postgresql' && !schema) {
// default to public schema
defaultSchema = 'public'
}
return {
type,
host: exists(uri.hostname) ? uri.hostname : undefined,
user: exists(uri.username) ? uri.username : undefined,
port: exists(uri.port) ? Number(uri.port) : undefined,
password: exists(uri.password) ? uri.password : undefined,
database,
schema: schema || defaultSchema,
uri: connectionString,
ssl: Boolean(uri.searchParams.get('sslmode')),
socket: socket || undefined,
extraFields,
}
}
function databaseTypeToProtocol(databaseType: ConnectorType): string {
switch (databaseType) {
case 'postgresql':
return 'postgresql:'
case 'mysql':
return 'mysql:'
case 'mongodb':
return 'mongodb:'
case 'sqlite':
return 'sqlite:'
case 'sqlserver':
return 'sqlserver:'
}
}
export function protocolToConnectorType(protocol: string): ConnectorType {
switch (protocol) {
case 'postgresql:':
case 'postgres:':
return 'postgresql'
case 'mongodb:':
return 'mongodb'
case 'mysql:':
return 'mysql'
case 'file:':
case 'sqlite:':
return 'sqlite'
case 'sqlserver:':
case 'jdbc:sqlserver:':
return 'sqlserver'
}
throw new Error(`Unknown database type ${protocol}`)
}