https://github.com/nstwfdev/mysql-connection
ReactPHP MySQL transactional connection
https://github.com/nstwfdev/mysql-connection
connection mysql reactphp transaction
Last synced: 2 months ago
JSON representation
ReactPHP MySQL transactional connection
- Host: GitHub
- URL: https://github.com/nstwfdev/mysql-connection
- Owner: nstwfdev
- License: mit
- Created: 2022-12-15T10:08:35.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2025-08-12T15:08:54.000Z (5 months ago)
- Last Synced: 2025-08-12T22:05:38.394Z (5 months ago)
- Topics: connection, mysql, reactphp, transaction
- Language: PHP
- Homepage:
- Size: 108 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.MD
- License: LICENSE
Awesome Lists containing this project
README
# Reactphp MySQL transactional connection
[](https://github.com/nstwfdev/mysql-connection/actions?query=workflow%3Aci+branch%3Amaster)
[](https://codecov.io/gh/nstwfdev/mysql-connection)
[](https://packagist.org/packages/nstwf/mysql-connection)
Simple wrapper of [`ConnectionInterface`](https://github.com/friends-of-reactphp/mysql) that allows you to make transactions easier
> Read [main documentation](https://github.com/friends-of-reactphp/mysql) before
**Table of contents**
* [Quickstart example](#quickstart-example)
* [Usage](#usage)
* [Factory](#factory)
* [createConnection()](#createConnection)
* [ConnectionInterface](#connectioninterface)
* [query()](#query)
* [transaction()](#transaction)
* [begin()](#begin)
* [commit()](#commit)
* [rollback()](#rollback)
* [ping()](#ping)
* [quit()](#quit)
* [close()](#close)
* [Install](#install)
* [Tests](#tests)
* [License](#license)
## Quickstart example
```php
$factory = new \Nstwf\MysqlConnection\Factory\ConnectionFactory(new \React\MySQL\Factory());
$connection = $factory->createConnection('localhost:3306');
$connection
->transaction(function (\Nstwf\MysqlConnection\ConnectionInterface $connection) {
return $connection->query('update users set name = "Tim" where id = 3');
})
->then(function () {
echo 'OK';
}, function (\Throwable $throwable) {
echo $throwable->getMessage();
});
$connection->quit();
```
## Usage
### Factory
The main role of factory - creating `ConnectionInterface`, by wrapping [`Factory`](https://github.com/friends-of-reactphp/mysql#factory)
#### createConnection
Create connection using [lazy connection](https://github.com/friends-of-reactphp/mysql#createlazyconnection) for future operations.
```php
$factory = new \Nstwf\MysqlConnection\Factory\ConnectionFactory(new \React\MySQL\Factory());
$connection = $factory->createConnection('localhost:3306');
```
### ConnectionInterface
That's a wrapper of original `ConnectionInterface`.
> Currently main difference - wrapper does not support event emitter methods
[See original documentation](https://github.com/friends-of-reactphp/mysql#connectioninterface)
#### query
[See original documentation](https://github.com/friends-of-reactphp/mysql#query)
#### transaction
The `transaction(callable $callable): PromiseInterface` method can be used to perform a transaction.
```php
$connection
->transaction(function (\Nstwf\MysqlConnection\ConnectionInterface $connection) {
return $connection->query('update users set name = "Tim" where id = 3');
})
->then(function () {
echo 'OK';
}, function (\Throwable $throwable) {
echo $throwable->getMessage();
});
```
Equals to:
```php
$connection
->query("BEGIN")
->then(
fn() => $connection->query("COMMIT"),
function (\Throwable $throwable) use ($connection) {
return $connection->query("ROLLBACK")
->then(fn() => $throwable);
}
);
```
#### begin
The `begin(): PromiseInterface` method can be used to begin the transaction.
```php
$connection
->begin()
->then(function () {
echo 'Begin';
}, function (\Throwable $throwable) {
echo $throwable->getMessage();
});
```
Equals to:
> Sql case-insensitive
```php
$connection->query("BEGIN");
// or
$connection->query("START TRANSACTION");
```
#### commit
The `commit(): PromiseInterface` method can be used to commit the transaction.
```php
$connection
->commit()
->then(function () use ($connection) {
echo 'Commit';
}, function (\Throwable $throwable) {
echo $throwable->getMessage();
});
```
Equals to:
> sql case-insensitive
```php
$connection->query("COMMIT");
```
#### rollback
The `rollback(): PromiseInterface` method can be used to rollback the transaction.
```php
$connection
->rollback()
->then(function () use ($connection) {
echo 'Rollback';
}, function (\Throwable $throwable) {
echo $throwable->getMessage();
});
```
Equals to:
> Sql case-insensitive
```php
$connection->query("ROLLBACK");
```
#### ping
[See original documentation](https://github.com/friends-of-reactphp/mysql#ping)
#### close
[See original documentation](https://github.com/friends-of-reactphp/mysql#close)
#### quit
[See original documentation](https://github.com/friends-of-reactphp/mysql#quit)
## Install
The recommended way to install this library is [through Composer](https://getcomposer.org).
[New to Composer?](https://getcomposer.org/doc/00-intro.md)
This project follows [SemVer](https://semver.org/).
This will install the latest supported version:
```bash
composer require nstwf/mysql-connection
```
See also the [CHANGELOG](docs/CHANGELOG.md) for details about version upgrades.
It's *highly recommended to use PHP 8+* * for this project.
## Tests
To run the test suite, you first need to clone this repo and then install all
dependencies [through Composer](https://getcomposer.org):
```bash
composer install
```
To run the test suite, go to the project root and run:
```bash
vendor/bin/phpunit
```
## License
MIT, see [LICENSE file](LICENSE).
- [friends-of-reactphp/mysql](https://github.com/friends-of-reactphp/mysql) - main project