Skip to content

Commit d9cb549

Browse files
committed
feat: add deferred and multi-db docs
1 parent 42c088f commit d9cb549

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

src/docs/database/index.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,24 +164,32 @@ db()->connect([
164164

165165
:::
166166

167-
## Deferred database connection <Badge text="NEW" type="tip" />
167+
Leaf DB will not connect to your database until you run a query. This means that you can pass in your database connection at the beginning of your application and only connect when you need to run a query which is a great way to save resources.
168168

169-
In a lot of cases, your application may have other routes that don't need a database connection, but popping up a connection before the route is hit can be a waste of resources. Leaf DB now allows you to defer your database connection until you actually need it. Here's how you can defer your database connection:
169+
## Multi-DB Connections <Badge text="NEW" type="tip" />
170+
171+
Some applications may need to connect to multiple databases for things like queues and logs, and Leaf DB allows you to keep multiple connections open and query them independently. Here's how you can connect to multiple databases:
170172

171173
```php
172-
db()->load([
173-
'dbtype' => '...',
174-
'charset' => '...',
175-
'port' => '...',
176-
'unixSocket' => '...',
177-
'host' => '...',
178-
'username' => '...',
179-
'password' => '...',
180-
'dbname' => '...',
181-
]);
174+
db()->addConnections([
175+
'conn1' => [
176+
'dbtype' => '...',
177+
...
178+
],
179+
'conn2' => [
180+
'dbtype' => '...',
181+
...
182+
],
183+
], 'conn1');
184+
```
185+
186+
The `addConnections()` method takes an array of connection details for your databases as its first argument and the default connection name as its second argument. You can then switch between connections using the `useConnection()` method:
187+
188+
```php
189+
db('conn2')->select('users')->all();
182190
```
183191

184-
It takes in the same arguments as `connect()`, but it doesn't connect to the database immediately. It only connects when you run a query.
192+
If no connection name is provided, Leaf DB will use the default connection.
185193

186194
## Writing simple queries
187195

0 commit comments

Comments
 (0)