jacked-php / lite-connect
此包的最新版本(0.0.1)没有可用的许可信息。
一个带有迁移和模型抽象的 SQLite 连接池管理器
0.0.1
2024-09-02 21:15 UTC
Requires
- php: >=8.3
- ext-pdo: *
- ext-sqlite3: *
Requires (Dev)
- fakerphp/faker: ^1.23
- pestphp/pest: ^2.35
This package is auto-updated.
Last update: 2024-09-02 21:19:58 UTC
README
LiteConnect 是一个简单、轻量级的 PHP SQLite 包,没有全局变量。它旨在简化 SQLite 数据库的交互。它非常适合需要嵌入式数据库解决方案的小型到中型项目。此包提供用于管理 SQLite 连接、运行迁移和与数据模型交互的干净 API。
特性
- 连接管理:创建和管理 SQLite 连接。
- 迁移管理:运行迁移以设置数据库模式。
- 模型交互:通过直观的 API 执行常见的数据库操作,如
create
、find
、update
、delete
、where
和orderBy
。
安装
要安装 LiteConnect,您可以通过 Composer 引入它
composer require jacked-php/lite-connect
基本用法
连接到 SQLite 数据库
use JackedPhp\LiteConnect\Connection\Connection; use JackedPhp\LiteConnect\SQLiteFactory; /** @var Connection $connection */ $connection = SQLiteFactory::make([ 'database' => 'path/to/your/database.db', ]); // When you're done with the connection: $connection->close();
运行迁移
要设置数据库模式,请使用 MigrationManager
运行迁移。
带有 "users" 表迁移的示例
use JackedPhp\LiteConnect\Migration\MigrationManager; class CreateUsersTable implements Migration { public function up(PDO $pdo): void { $pdo->exec('CREATE TABLE users ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NULL, email TEXT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )'); } public function down(PDO $pdo): void { $pdo->exec('DROP TABLE users'); } } $migrationManager = new MigrationManager($connection); $migrationManager->runMigrations([ new CreateUsersTable(), ]);
与模型交互
您可以使用模型类与数据交互。以下是一个 User
模型的示例
use JackedPhp\LiteConnect\Model\BaseModel; class User extends BaseModel { protected string $table = 'users'; protected ?string $primaryKey = 'id'; /** * @var string[] $fillable */ protected array $fillable = [ 'name', 'email', ]; } // Creating a new user /** @var User $newUser */ $newUser = (new User($connection))->create([ 'name' => 'John Doe', 'email' => 'john.doe@example.com', ]); // Finding a user by ID /** @var User $foundUser */ $foundUser = (new User($connection))->find($newUser->id); // Updating a user $foundUser->update([ 'email' => 'john.doe@newdomain.com', ]); // Deleting a user $foundUser->delete(); // or (new User($connection))->where('name', '=', 'John Doe')->delete();
查询数据
您可以使用 where
、orderBy
等查询方法来过滤和排序数据
$users = new User($connection); $filteredUsers = $users->where('name', '=', 'John Doe')->get(); // or $orderedUsers = $users->orderBy('id', 'desc')->get();
测试
您可以通过在克隆存储库并安装依赖项后运行以下命令来运行测试
vendor/bin/pest
贡献
如果您想为 LiteConnect 贡献,请随时提交拉取请求或在 GitHub 存储库 上打开问题。
许可
LiteConnect 是开源软件,受 MIT 许可证 的许可。