Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add connection string initialization #73

Closed
theladyjaye opened this issue Jul 29, 2017 · 5 comments
Closed

Add connection string initialization #73

theladyjaye opened this issue Jul 29, 2017 · 5 comments

Comments

@theladyjaye
Copy link

theladyjaye commented Jul 29, 2017

Per the docs:
http://docs.sequelizejs.com/manual/installation/getting-started.html#setting-up-a-connection

This should be allowed to be passed into the Sequelize constructor:

// Or you can simply use a connection uri
const sequelize = new Sequelize('postgres://user:pass@example.com:5432/dbname');

https://github.com/sequelize/sequelize/blob/master/lib/sequelize.js#L103-L134

This appears to work (I ported from their code):

function parseConnectionString(connectionString: String): any{
    let config: any = {};
    let options: any = {}

    const urlParts = url.parse(connectionString);

    options.dialect = urlParts.protocol.replace(/:$/, '');
    options.host = urlParts.hostname;

    if (options.dialect === 'sqlite' && urlParts.pathname && urlParts.pathname.indexOf('/:memory') !== 0) {
        const path = Path.join(options.host, urlParts.pathname);
        options.storage = options.storage || path;
    }

    if (urlParts.pathname) {
        config.database = urlParts.pathname.replace(/^\//, '');
    }

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

    if (urlParts.auth) {
        const authParts = urlParts.auth.split(':');

        config.username = authParts[0];

        if (authParts.length > 1)
            config.password = authParts.slice(1).join(':');
    }

    let result = Object.assign({}, config, options);

    return result;
}
@hiradimir
Copy link
Contributor

+1

But, now I am using "parse-database-url".

import * as parseDbUrl from "parse-database-url";
---
      let dbConfig = parseDbUrl(process.env["DATABASE_URL"]);

      const sequelize = new Sequelize({
        name: dbConfig.database,
        username: dbConfig.user,
        password: dbConfig.password,
        dialect: dbConfig.driver,
        host: dbConfig.host,
        port: dbConfig.port,
        modelPaths: [__dirname + '/../main/models']
      });

@theladyjaye
Copy link
Author

Winning! I'll take that all day. Personal preference for simple stuff like that which is only a few LOC i'm dependency averse. At the same time, whatever works ;)

@RobinBuschmann
Copy link
Member

@aventurella thanks for raising this. I will consider this for the next releases. But if you're like to create a PR for that, feel free to do so. Any help is appreciated :)

@kukoo1
Copy link
Contributor

kukoo1 commented Jul 30, 2017

@RobinBuschmann I've recently tried implementing it using static factory function, without parser dependency. But then I realized that it breaks backward compatibility because I had to remove the constructor.
Typescript doesn't allow neither passing spread arguments into another function nor conditional super() call.
I wonder that we should go with that path or integrate with external parser instead.

@RobinBuschmann
Copy link
Member

Hey @kukoo1, can you show me how your approach looks like? Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants