Skip to content

Commit 8a27c28

Browse files
authored
Merge pull request #104 from leafsphp/staging
feat: add deferred db docs
2 parents 46b5e05 + 7dac8f9 commit 8a27c28

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/docs/database/index.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ Databases are essential for most applications, as they help you store and retrie
5151

5252
## Leaf MVC + DB
5353

54-
Leaf MVC comes with built-in support for models which are a way to programmatically represent resources in your database using PHP classes. For that reason, you have no real need for this module unless you want to use Leaf Auth. If you choose to use Leaf DB in your MVC application, we have already set up everything for you. All you need to do is to head over to your `.env` file and set up your database connection details. Here are a few example connections:
54+
Leaf MVC comes with built-in support for models which are a way to programmatically represent resources in your database using PHP classes. For that reason, you have no real need to use the `db()` function unless you want to quickly run a query without creating a model. Still, everything has been set up for you and Leaf DB will use the default database connection details in your `.env` file.
55+
56+
Here are a few example connections:
5557

5658
::: code-group
5759

@@ -80,9 +82,7 @@ DB_DATABASE=/absolute/path/to/database.sqlite
8082

8183
:::
8284

83-
Remember to head over to `public/index.php` and uncomment the line that says `\Leaf\Database::initDb();`. This will automatically connect to your database using the details in your environment file.
84-
85-
You can safely skip the "Connecting to a database" section.
85+
You can skip the DB connection section: Leaf MVC sets up a deferred connection for you. This means that the connection will only be made when you run a query.
8686

8787
## Connecting to a database
8888

@@ -135,6 +135,25 @@ db()->connect([
135135

136136
:::
137137

138+
## Deferred database connection <Badge text="NEW" type="tip" />
139+
140+
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:
141+
142+
```php
143+
db()->load([
144+
'dbtype' => '...',
145+
'charset' => '...',
146+
'port' => '...',
147+
'unixSocket' => '...',
148+
'host' => '...',
149+
'username' => '...',
150+
'password' => '...',
151+
'dbname' => '...',
152+
]);
153+
```
154+
155+
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.
156+
138157
## Writing simple queries
139158

140159
Once you've connected to a database, you can start writing queries to interact with it. Queries are the commands you run on your database to get, insert, update or delete data. Leaf DB provides a simple way to run queries using the query builder, but also allows you to run raw SQL queries.

0 commit comments

Comments
 (0)