Skip to content

Add connection string initialization #73

Closed
@theladyjaye

Description

@theladyjaye

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:[email protected]: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;
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions