Skip to content

Commit

Permalink
feat(sequelize): allow passing dialectOptions.options from url (#12404)
Browse files Browse the repository at this point in the history
  • Loading branch information
yorek committed Jun 23, 2020
1 parent c6e4192 commit 663261b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,20 @@ class Sequelize {
options.host = urlParts.query.host;
}

if (options.dialectOptions)
if (options.dialectOptions) {
Object.assign(options.dialectOptions, urlParts.query);
else
} else {
options.dialectOptions = urlParts.query;
if (urlParts.query.options) {
try {
const o = JSON.parse(urlParts.query.options);
options.dialectOptions.options = o;
} catch (e) {
// Nothing to do, string is not a valid JSON
// an thus does not need any further processing
}
}
}
}
} else {
// new Sequelize(database, username, password, { ... options })
Expand Down
6 changes: 6 additions & 0 deletions test/unit/configuration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ describe('Sequelize', () => {
expect(dialectOptions.ssl).to.equal('true');
});

it('should handle JSON options', () => {
const sequelizeWithOptions = new Sequelize('mysql://example.com:9821/dbname?options={"encrypt":true}&anotherOption=1');
expect(sequelizeWithOptions.options.dialectOptions.options.encrypt).to.be.true;
expect(sequelizeWithOptions.options.dialectOptions.anotherOption).to.equal('1');
});

it('should use query string host if specified', () => {
const sequelize = new Sequelize('mysql://localhost:9821/dbname?host=example.com');

Expand Down

0 comments on commit 663261b

Please sign in to comment.