Skip to content

Commit e5ddcbd

Browse files
committed
Multiple connections option
1 parent 9a72fe6 commit e5ddcbd

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/Connection.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ class Connection extends BaseConnection
2626
protected $rebuild = false;
2727
protected $allowIdSort = true;
2828
protected $errorLoggingIndex = false;
29+
protected $connectionName = 'opensearch';
2930

3031
public function __construct(array $config)
3132
{
33+
34+
$this->connectionName = $config['name'];
35+
3236
$this->config = $config;
3337

3438
$this->setOptions($config);
@@ -202,7 +206,7 @@ public function getAllowIdSort()
202206

203207
protected function buildConnection(): Client
204208
{
205-
$hosts = config('database.connections.opensearch.hosts') ?? null;
209+
$hosts = config('database.connections.'.$this->connectionName.'.hosts') ?? null;
206210

207211
$builder = ClientBuilder::create()->setHosts($hosts);
208212
$builder = $this->_buildOptions($builder);
@@ -217,8 +221,8 @@ protected function buildConnection(): Client
217221
protected function _buildAuth(ClientBuilder $builder): ClientBuilder
218222
{
219223

220-
$username = config('database.connections.opensearch.basic_auth.username') ?? null;
221-
$pass = config('database.connections.opensearch.basic_auth.password') ?? null;
224+
$username = config('database.connections.'.$this->connectionName.'.basic_auth.username') ?? null;
225+
$pass = config('database.connections.'.$this->connectionName.'.basic_auth.password') ?? null;
222226
if ($username && $pass) {
223227
$builder->setBasicAuthentication($username, $pass);
224228
}
@@ -228,9 +232,9 @@ protected function _buildAuth(ClientBuilder $builder): ClientBuilder
228232

229233
protected function _buildSigV4(ClientBuilder $builder): ClientBuilder
230234
{
231-
$provider = config('database.connections.opensearch.sig_v4.provider') ?? null;
232-
$region = config('database.connections.opensearch.sig_v4.region') ?? null;
233-
$service = config('database.connections.opensearch.sig_v4.service') ?? null;
235+
$provider = config('database.connections.'.$this->connectionName.'.sig_v4.provider') ?? null;
236+
$region = config('database.connections.'.$this->connectionName.'.sig_v4.region') ?? null;
237+
$service = config('database.connections.'.$this->connectionName.'.sig_v4.service') ?? null;
234238
if ($provider) {
235239
$builder->setSigV4CredentialProvider($provider);
236240
}
@@ -246,10 +250,10 @@ protected function _buildSigV4(ClientBuilder $builder): ClientBuilder
246250

247251
protected function _buildSSL(ClientBuilder $builder): ClientBuilder
248252
{
249-
$sslCert = config('database.connections.opensearch.ssl.cert') ?? null;
250-
$sslCertPassword = config('database.connections.opensearch.ssl.cert_password') ?? null;
251-
$sslKey = config('database.connections.opensearch.ssl.key') ?? null;
252-
$sslKeyPassword = config('database.connections.opensearch.ssl.key_password') ?? null;
253+
$sslCert = config('database.connections.'.$this->connectionName.'.ssl.cert') ?? null;
254+
$sslCertPassword = config('database.connections.'.$this->connectionName.'.ssl.cert_password') ?? null;
255+
$sslKey = config('database.connections.'.$this->connectionName.'.ssl.key') ?? null;
256+
$sslKeyPassword = config('database.connections.'.$this->connectionName.'.ssl.key_password') ?? null;
253257
if ($sslCert) {
254258
$builder->setSSLCert($sslCert, $sslCertPassword);
255259
}

src/Schema/Schema.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,18 @@ class Schema extends Facade
4747
*/
4848
public static function connection($name)
4949
{
50+
if ($name === null) {
51+
return static::getFacadeAccessor();
52+
}
53+
5054
return static::$app['db']->connection($name)->getSchemaBuilder();
5155
}
5256

57+
public static function on($name)
58+
{
59+
return static::connection($name);
60+
}
61+
5362
/**
5463
* Get a schema builder instance for the default connection.
5564
*

0 commit comments

Comments
 (0)