Repository: sequelize/sequelize
File: packages/core/src/utils/url.ts (TypeScript)

Code before:
 * @param connectionString string value in format schema://username:password@host:port/database
 */
export function parseConnectionString(connectionString: string): Options {
  const options: Options = {};

  // The following connectionStrings are not valid URLs, but they are supported by sqlite.
  if (connectionString.toLowerCase() === 'sqlite://:memory:' || connectionString.toLowerCase() === 'sqlite::memory:') {
    options.dialect = 'sqlite';
    options.host = '';

    return options;
  }

  const urlObject = new URL(connectionString);

  if (urlObject.protocol) {
    let protocol = urlObject.protocol.replace(/:$/, '');
    if (protocol === 'postgresql') {
      protocol = 'postgres';
    }

    options.dialect = protocol as Dialect;
  }

  if (urlObject.hostname != null) {
    options.host = urlObject.hostname;
  }

  if (urlObject.pathname) {
    // decode the URI component from urlObject.pathname value
    options.database = decodeURIComponent(urlObject.pathname.replace(/^\//, ''));
  }

  if (urlObject.port) {
    options.port = urlObject.port;
  }

  if (urlObject.username) {
    options.username = decodeURIComponent(urlObject.username);
  }

  if (urlObject.password) {
    options.password = decodeURIComponent(urlObject.password);
  }

  if (options.dialect === 'sqlite' && urlObject.pathname) {
    const storagePath = path.join(options.host!, urlObject.pathname);
    options.storage = path.resolve(options.storage || storagePath);
  }

  if (urlObject.searchParams) {

Review comment (suggestion):
as a TODO, we should rename our `host` option to `hostname`, as `host` can accept a port while `hostname` can't

Code after:
 * Parses a connection string into an Options object with connection properties
 *
 * @param connectionString string value in format schema://username:password@host:port/database
 */
export function parseConnectionString(connectionString: string): Options {
  const options: Options = {};

  // The following connectionStrings are not valid URLs, but they are supported by sqlite.
  if (connectionString === 'sqlite://:memory:' || connectionString === 'sqlite::memory:') {
    options.dialect = 'sqlite';
    options.storage = ':memory:';

    return options;
  }

  const urlObject = new URL(connectionString);

  if (urlObject.protocol) {
    let protocol = urlObject.protocol.replace(/:$/, '');
    if (protocol === 'postgresql') {
      protocol = 'postgres';
    }

    if (!SUPPORTED_DIALECTS.includes(protocol as Dialect)) {
      throw new Error(`The protocol was set to ${JSON.stringify(protocol)}, which is not a supported dialect. Set it to one of ${SUPPORTED_DIALECTS.map(d => JSON.stringify(d)).join(', ')} instead.`);
    }

    options.dialect = protocol as Dialect;
  }

  if (urlObject.hostname != null) {
    // TODO: rename options.host to options.hostname, as host can accept a port while hostname can't
    options.host = decodeURIComponent(urlObject.hostname);
  }

  if (urlObject.pathname) {
    // decode the URI component from urlObject.pathname value
    options.database = decodeURIComponent(urlObject.pathname.replace(/^\//, ''));
  }

  if (urlObject.port) {
    options.port = urlObject.port;
  }

  if (urlObject.username) {
    options.username = decodeURIComponent(urlObject.username);
  }

  if (urlObject.password) {
    options.password = decodeURIComponent(urlObject.password);
  }