Skip to content

Commit

Permalink
fix(core): respect database name in clientUrl of read replicas
Browse files Browse the repository at this point in the history
Closes #4813
  • Loading branch information
B4nan committed Oct 15, 2023
1 parent 92e1d6f commit 015d4f4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packages/core/src/connections/Connection.ts
Expand Up @@ -73,12 +73,22 @@ export abstract class Connection {

getConnectionOptions(): ConnectionConfig {
const ret: ConnectionConfig = {};
const url = new URL(this.options.clientUrl ?? this.config.getClientUrl());
this.options.host = ret.host = this.options.host ?? this.config.get('host', decodeURIComponent(url.hostname));
this.options.port = ret.port = this.options.port ?? this.config.get('port', +url.port);
this.options.user = ret.user = this.options.user ?? this.config.get('user', decodeURIComponent(url.username));
this.options.password = ret.password = this.options.password ?? this.config.get('password', decodeURIComponent(url.password));
this.options.dbName = ret.database = this.options.dbName ?? this.config.get('dbName', decodeURIComponent(url.pathname).replace(/^\//, ''));

if (this.options.clientUrl) {
const url = new URL(this.options.clientUrl);
this.options.host = ret.host = this.options.host ?? decodeURIComponent(url.hostname);
this.options.port = ret.port = this.options.port ?? +url.port;
this.options.user = ret.user = this.options.user ?? decodeURIComponent(url.username);
this.options.password = ret.password = this.options.password ?? decodeURIComponent(url.password);
this.options.dbName = ret.database = this.options.dbName ?? decodeURIComponent(url.pathname).replace(/^\//, '');
} else {
const url = new URL(this.config.getClientUrl());
this.options.host = ret.host = this.options.host ?? this.config.get('host', decodeURIComponent(url.hostname));
this.options.port = ret.port = this.options.port ?? this.config.get('port', +url.port);
this.options.user = ret.user = this.options.user ?? this.config.get('user', decodeURIComponent(url.username));
this.options.password = ret.password = this.options.password ?? this.config.get('password', decodeURIComponent(url.password));
this.options.dbName = ret.database = this.options.dbName ?? this.config.get('dbName', decodeURIComponent(url.pathname).replace(/^\//, ''));
}

return ret;
}
Expand Down
27 changes: 27 additions & 0 deletions tests/EntityManager.mysql.test.ts
Expand Up @@ -2436,4 +2436,31 @@ describe('EntityManagerMySql', () => {
await orm.em.flush();
});

test('clientUrl with replicas (GH issue #4813)', async () => {
const config = new Configuration({
driver: MySqlDriver,
clientUrl: 'mysql://usr:pswd@128.0.0.1:5433/foo',
preferReadReplicas: true,
replicas: [
{ clientUrl: 'mysql://usr2:pswd2@129.0.0.1:5434/bar' },
],
entities: ['src/**/*.entity.ts'],
}, false);
const driver = new MySqlDriver(config);
expect(driver.getConnection('write').getConnectionOptions()).toMatchObject({
database: 'foo',
host: '128.0.0.1',
password: 'pswd',
port: 5433,
user: 'usr',
});
expect(driver.getConnection('read').getConnectionOptions()).toMatchObject({
database: 'bar',
host: '129.0.0.1',
password: 'pswd2',
port: 5434,
user: 'usr2',
});
});

});

0 comments on commit 015d4f4

Please sign in to comment.