PHPackages                             keruald/database - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. keruald/database

ActiveLibrary[Database &amp; ORM](/categories/database)

keruald/database
================

Allow to query a database

0.6.1(6mo ago)030BSD-2-ClausePHP

Since Feb 6Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/keruald/database)[ Packagist](https://packagist.org/packages/keruald/database)[ RSS](/packages/keruald-database/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (10)Used By (0)

keruald/database
================

[](#kerualddatabase)

This library offers a simple layer of abstraction for database operations.

Configuration
-------------

[](#configuration)

To get a database instance, you need to pass configuration as an array. The properties and values depend on the engine you want to use.

### MySQLi

[](#mysqli)

KeyValueengineMySQLiEngine class referencehostThe MySQL hostname, e.g. "localhost"usernameThe MySQL user to use for connectionpasswordThe clear text password to usedatabaseThe default db to select for queries(optional)fetch\_modeThe default mode to fetch rows`MYSQLI_ASSOC`For example:

```
[
    'engine' => Keruald\Database\Engines\MySQLiEngine::class,
    'host' => 'localhost',
    'username' => 'app',
    'password' => 'someSecret',
    'database' => 'app',          // optional
]
```

#### About fetch\_mode parameter

[](#about-fetch_mode-parameter)

The `fetch_mode` parameter is used to determine how to represent results:

- `MYSQLI_ASSOC` will use column names
- `MYSQLI_NUM` will use an enumerated array (0, 1, 2, …)
- `MYSQLI_BOTH` will use both of them

The code offers `MYSQLI_ASSOC` as default value to allow to directly represent a row result as API output and encourage to take care of the column names for better code maintenance. If you wish to switch to default MySQLi behavior, use `MYSQLI_BOTH` instead.

Those constants are defined by the MySQLi extension.

### PDO Drivers - PostgreSQL

[](#pdo-drivers---postgresql)

Two PDO drivers for PostgreSQL are supported:

- PDO\\Pgsql for PHP 8.4+, as PgsqlPDOEngine
- The legacy PDO for other PHP versions, as PostgreSQLPDOEngine

KeyValueenginePgsqlPDOEngine class referencehostThe hostname, e.g. "localhost"usernameThe user to use for connectionpasswordThe clear text password to usedatabaseThe default db to select for queries(optional)fetch\_modeThe default mode to fetch rows`PDO::FETCH_ASSOC`For example:

```
[
    'engine' => Keruald\Database\Engines\PgsqlPDOEngine::class,
    'host' => 'localhost',
    'username' => 'app',
    'password' => 'someSecret',
    'database' => 'app',          // optional
]
```

#### About fetch\_mode parameter

[](#about-fetch_mode-parameter-1)

The `fetch_mode` parameter is used to determine how to represent results:

- `PDO::FETCH_ASSOC` or `2` will use column names
- `PDO::FETCH_NUM` or `3` will use an enumerated array (0, 1, 2, …)
- `PDO::FETCH_BOTH` or `4` will use both of them

The code offers `PDO::FETCH_ASSOC` as default value to allow to directly represent a row result as API output and encourage to take care of the column names for better code maintenance. If you wish to switch to default PDO behavior, use `PDO::FETCH_BOTH` instead.

If you need to use other PDO modes, the following methods aren't available:

- isExistingTable
- queryScalar

Other methods should work perfectly fine.

Those constants are defined by the PDO extension.

Legacy drivers
--------------

[](#legacy-drivers)

The mysql extension has been deprecated in PHP 5.7 and removed in PHP 7. As such, this extension isn't supported anymore. You can use straightforwardly replace 'MySQL' by 'MySQLi' as engine.

Specialized drivers for tests
-----------------------------

[](#specialized-drivers-for-tests)

### Blackhole

[](#blackhole)

The black hole engine does nothing and always returns `true` as query result.

This engine can be used for mocks:

- directly, when database behavior does not matter
- to build a mock by overriding behavior of query() or any other method

It can also be used with the loader, without any engine-specific configuration:

```
[
    'engine' => Keruald\Database\Engines\BlackholeEngine::class,
]
```

### MockDatabaseEngine

[](#mockdatabaseengine)

The mock database is a simple implementation of the black hole engine as mocking service to use when you want to return a deterministic response to known queries.

A benefit is you don't need a running database server for your unit tests.

You can pass to the `withQueries` method an array with one item per query:

- key: the SQL query
- value: an array with all rows for that query
-

For example:

```
    public function testGetFruits () : void {
        $queries = [
            "SELECT name, color FROM fruits" => [
                [ "name" => "strawberry", "color" => "red" ],
                [ "name" => "blueberry", "color" => "violet" ],
            ],
        ];

        $db = (new MockDatabaseEngine())
            ->withQueries($queries);

        // Inject $db to a class and test it
    }
```

To return only one row, you can use `[[…]]` to represent an array of one array:

```
        $queries = [
            "SELECT 1+1" => [[ "1+1" => 2 ]],
        ];
```

The queries results are then wrapped in the MockDatabaseResult class.

When the query doesn't exist, an exception is thrown.

We recommend a mixed approach of the Blackhole engine when results don't matter, and of this class when you need some control on it.

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance67

Regular maintenance activity

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~170 days

Recently: every ~3 days

Total

9

Last Release

197d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8fa275e3c93d0f423ef8b7c1a848e6e4ea325225ea268e5fcce744e8ce3dd43b?d=identicon)[dereckson](/maintainers/dereckson)

---

Top Contributors

[![dereckson](https://avatars.githubusercontent.com/u/135563?v=4)](https://github.com/dereckson "dereckson (22 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/keruald-database/health.svg)

```
[![Health](https://phpackages.com/badges/keruald-database/health.svg)](https://phpackages.com/packages/keruald-database)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
