mysql-parse
Version:
Parse a mysql connection string and return access parameters that can be fed to the mysql or mysqldump command line client.
37 lines (34 loc) • 763 B
JavaScript
import { URL } from 'url'
export function parseUri(uri) {
const {
protocol = '',
username: user,
password,
port,
hostname: host,
pathname = '',
} = new URL(uri)
return {
scheme: protocol.replace(':', ''),
user,
password,
host,
port,
database: pathname.replace('/', ''),
}
}
export function buildMysqlParams(uri) {
const { user, password, host, port, database } = parseUri(uri)
return (
[
user ? `-u ${user}` : '',
password ? `-p${password}` : '',
host ? `-h ${host}` : '',
port ? `-P ${port}` : '',
database ? `${database}` : '',
]
// filter out falsy values to avoid extra spaces on the command line.
.filter((value) => value)
.join(' ')
)
}