jacked-php/lite-connect

There is no license information available for the latest version (0.0.3) of this package.

A SQLite connection pool manager with migrations and model abstraction

Fund package maintenance!
jacked-php

0.0.3 2024-10-24 01:05 UTC

This package is auto-updated.

Last update: 2024-10-24 02:26:48 UTC


README

Tests

LiteConnect is a simple, lightweight SQLite package for PHP without globals. It is designed to facilitate easy and efficient SQLite database interactions. It is ideal for small to medium-sized projects that require an embedded database solution. This package provides a clean API for managing SQLite connections, running migrations, and interacting with your data models.

Features

  • Connection Management: Create and manage SQLite connections.
  • Migration Management: Run migrations to set up your database schema.
  • Model Interaction: Perform common database operations like create, find, update, delete, where, and orderBy through an intuitive API.

Installation

To install LiteConnect, you can require it via Composer:

composer require jacked-php/lite-connect

Basic Usage

Connecting to a SQLite Database

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();

Running Migrations

To set up your database schema, use the MigrationManager to run migrations.

Example with a "users" table migration:

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(),
]);

Interacting with Models

You can interact with your data using the model classes. Here is an example of a User model:

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();

Querying Data

You can use the where, orderBy, and other query methods to filter and order your data:

$users = new User($connection);

$filteredUsers = $users->where('name', '=', 'John Doe')->get();
// or
$orderedUsers = $users->orderBy('id', 'desc')->get();

Using a Connection Pool

This example demonstrate how to do so with OpenSwoole - that is not a requirement for this package.

If your project is using OpenSwoole\Core package (https://github.com/openswoole/core), here is how you accomplish it:

use OpenSwoole\Core\Coroutine\Pool\ClientPool;

$connectionPool = new ClientPool(
    factory: SQLiteFactory::class,
    config: [
        'database' => 'path/to/your/database.db',
    ],
    size: 1,
);

// here you get the connection:
$connection = $connectionPool->get();

// here you put back the connection:
$connectionPool->put($connection);

Testing

You can run tests by running the following after cloning the repository and installing dependencies:

vendor/bin/pest

Contributing

If you would like to contribute to LiteConnect, please feel free to submit pull requests or open issues on the GitHub repository.

License

LiteConnect is open-sourced software licensed under the MIT license.