nixphp/database
最新稳定版本:v0.1.1
Composer 安装命令:
composer require nixphp/database
包简介
NixPHP Database Plugin to work with various storage solutions.
README 文档
README
nixphp/database
Simple, native PDO connection for your NixPHP application.
This plugin provides a shared PDO instance via the service container, supporting both MySQL/MariaDB and SQLite out of the box — with sensible defaults.
🧩 Part of the official NixPHP plugin collection. Install it when you need a native, PSR-compliant database connection — and nothing more.
📦 Features
- ✅ Shared
PDOinstance, ready to use - ✅ MySQL/MariaDB and SQLite support
- ✅ Uses sane defaults (error mode, fetch mode, UTF-8 charset)
- ✅ Works with memory databases (
sqlite::memory:) - ✅ Available via
database()helper or DI container
📥 Installation
composer require nixphp/database
Then add the following configuration to /app/config.php.
You can choose between MySQL/MariaDB or SQLite.
Example: MySQL
<?php return [ // ... 'database' => [ 'driver' => 'mysql', 'host' => '127.0.0.1', 'database' => 'myapp', 'username' => 'root', 'password' => '', 'charset' => 'utf8mb4', ] ];
Example: SQLite
<?php return [ // ... 'database' => [ 'driver' => 'sqlite', 'database' => __DIR__ . '/../storage/database.sqlite', ] ];
Or for in-memory SQLite:
return [ // ... 'database' => [ 'driver' => 'sqlite', 'database' => ':memory:', ] ];
🚀 Usage
Access the PDO instance globally via:
$pdo = database();
Or retrieve it manually from the service container:
$pdo = app()->container()->get(Database::class);
Use it as usual with native PDO:
$stmt = database()->prepare('SELECT * FROM users WHERE id = ?'); $stmt->execute([$id]); $user = $stmt->fetch(); // default: FETCH_ASSOC
⚙️ Defaults applied
The PDO instance comes with these options:
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]
🔍 Internals
- Loads config from
/app/config.phpfrom the keydatabase - Builds DSN based on a given driver (
mysql,sqlite) - Wraps PDO creation in a factory, handles exceptions gracefully
- Registers
databasein the container and provides thedatabase()helper
✅ Requirements
nixphp/framework>= 0.1.0
📄 License
MIT License.
统计信息
- 总下载量: 41
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-02