> For the complete documentation index, see [llms.txt](https://quantumphp.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://quantumphp.gitbook.io/docs/advanced-features/database.md).

# Database

## Overview

Quantum resolves the active database adapter from `config/database.php` (`database.default`) and connects lazily through `Quantum\Database\Database`.

Supported adapter keys:

* `mysql` → `IdiormDbal`
* `sqlite` → `IdiormDbal`
* `pgsql` → `IdiormDbal`
* `sleekdb` → `SleekDbal`

## Configuration

A typical `config/database.php` uses this shape:

```php
return [
    'default' => 'sleekdb', // or mysql/sqlite/pgsql

    'mysql' => [
        'driver' => 'mysql',
        'host' => 'localhost',
        'dbname' => 'app',
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',
    ],

    'sqlite' => [
        'driver' => 'sqlite',
        'database' => 'database.sqlite',
    ],

    'sleekdb' => [
        'database_dir' => base_dir() . DS . 'shared' . DS . 'store',
        'config' => [/* SleekDB options */],
    ],
];
```

## Accessing Database Runtime

Use the helper:

```php
$db = db(); // returns Quantum\Database\Database
$adapterClass = $db->getOrmClass();
$config = $db->getConfigs();
```

## SQL-only Raw Query APIs

These `Database` static methods are forwarded to the active adapter:

* `Database::execute($sql, $params)`
* `Database::query($sql, $params)`
* `Database::fetchColumns($table)`
* `Database::lastQuery()`
* `Database::queryLog()`

Important: these APIs are implemented by `IdiormDbal` (SQL adapters). If `database.default` is `sleekdb`, SQL-only methods are not supported.

## Transactions

`Database::beginTransaction()`, `commit()`, `rollback()`, and `transaction(callable)` are available when using SQL adapters (`mysql`, `sqlite`, `pgsql`).

## Querying Data (recommended path)

Use `DbModel` via `model()` helper for app-level data access:

```php
$user = model(\Modules\App\Models\User::class)
    ->criteria('email', '=', 'john@example.com')
    ->first();
```

This keeps adapter-specific details behind model and service layers.

## Best Practices

* Keep `database.default` explicit per environment.
* Use models/services for business queries; use raw SQL only when needed.
* Use migrations for schema changes.

## What to read next

After database setup and usage, continue with:

* [Migration](/docs/advanced-features/migration.md)
* [Models](/docs/core-concepts/models.md)
* [Services](/docs/core-concepts/services.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://quantumphp.gitbook.io/docs/advanced-features/database.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
