UNPKG

sequelize-parse-url

Version:

Parses a sequelize url into a configuration object

32 lines (31 loc) 1.1 kB
import path from 'path'; import url from 'url'; export default function parseUrl(sourceUrl, options = {}) { const urlParts = url.parse(sourceUrl, true); const parsed = { dialect: urlParts.protocol.replace(/:$/, ''), host: urlParts.hostname }; if (parsed.dialect === 'sqlite' && urlParts.pathname && urlParts.pathname.indexOf('/:memory') !== 0) { parsed.storage = path.resolve(options.storage || path.join(parsed.host, urlParts.pathname)); } if (urlParts.pathname) { parsed.database = urlParts.pathname.replace(/^\//, ''); } if (urlParts.port) { parsed.port = urlParts.port; } if (urlParts.auth) { const authParts = urlParts.auth.split(':'); parsed.username = authParts[0]; if (authParts.length > 1) parsed.password = authParts.slice(1).join(':'); } if (urlParts.query) { if (parsed.dialectOptions) parsed.dialectOptions = { ...parsed.dialectOptions, ...urlParts.query }; else parsed.dialectOptions = urlParts.query; } return parsed; }