2017 © Pedro Peláez
 

library pgasync

Async Reactive Postgres Driver for PHP (Non-blocking)

image

voryx/pgasync

Async Reactive Postgres Driver for PHP (Non-blocking)

  • Wednesday, April 18, 2018
  • by mbonneau
  • Repository
  • 8 Watchers
  • 41 Stars
  • 5,219 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 12 Forks
  • 2 Open issues
  • 12 Versions
  • 5 % Grown

The README.md

Build Status, (*1)

PgAsync

Asynchronous Reactive Postgres Library for PHP (Non-blocking), (*2)

What it is

This is an asynchronous Postgres library for PHP. Observables are returned by the query methods allowing asynchronous row-by-row data handling (and other Rx operators on the data) See Rx.PHP. Network and event processing is handled by ReactPHP., (*3)

This is a pure PHP implementation (you don't need Postgres extensions to use it)., (*4)

Example - Simple Query


$client = new PgAsync\Client([ "host" => "127.0.0.1", "port" => "5432", "user" => "matt", "database" => "matt" ]); $client->query('SELECT * FROM channel')->subscribe( function ($row) { var_dump($row); }, function ($e) { echo "Failed.\n"; }, function () { echo "Complete.\n"; } );

Example - parameterized query


$client = new PgAsync\Client([ "host" => "127.0.0.1", "port" => "5432", "user" => "matt", "database" => "matt", "auto_disconnect" => true //This option will force the client to disconnect as soon as it completes. The connection will not be returned to the connection pool. ]); $client->executeStatement('SELECT * FROM channel WHERE id = $1', ['5']) ->subscribe( function ($row) { var_dump($row); }, function ($e) { echo "Failed.\n"; }, function () { echo "Complete.\n"; } );

Example - LISTEN/NOTIFY

$client = new PgAsync\Client([
     "host" => "127.0.0.1",
     "port" => "5432",
     "user"     => "matt",
     "database" => "matt"
]);

$client->listen('some_channel')
    ->subscribe(function (\PgAsync\Message\NotificationResponse $message) {
        echo $message->getChannelName() . ': ' . $message->getPayload() . "\n";
    });

$client->query("NOTIFY some_channel, 'Hello World'")->subscribe();

Install

With composer install into you project with:, (*5)

Install pgasync: composer require voryx/pgasync, (*6)

What it can do

  • Run queries (CREATE, UPDATE, INSERT, SELECT, DELETE)
  • Queue commands
  • Return results asynchronously (using Observables - you get data one row at a time as it comes from the db server)
  • Prepared statements (as parameterized queries)
  • Connection pooling (basic pooling)

What it can't quite do yet

  • Transactions (Actually though, just grab a connection and you can run your transaction on that single connection)

What's next

  • Add more testing
  • Transactions
  • Take over the world

Keep in mind

This is an asynchronous library. If you begin 3 queries (subscribe to their observable):, (*7)

$client->query("SELECT * FROM table1")->subscribe(...);
$client->query("SELECT * FROM table2")->subscribe(...);
$client->query("SELECT * FROM table3")->subscribe(...);

It will start all of them almost simultaneously (and you will begin receiving rows on all 3 before any of them have completed). This can be great if you want to run 3 queries at the same time, but if you have some queries that need information that was modified by other statements, this can cause a race condition:, (*8)

$client->query("INSERT INTO invoices(inv_no, customer_id, amount) VALUES('1234A', 1, 35.75)")->subscribe(...);
$client->query("SELECT SUM(amount) AS balance FROM invoices WHERE customer_id = 1")->subscribe(...);

In the above situation, your balance may or may not include the invoice inserted on the first line., (*9)

You can avoid this by using the Rx concat* operator to only start up the second observable after the first has completed:, (*10)

$insert = $client->query("INSERT INTO invoices(inv_no, customer_id, amount) VALUES('1234A', 1, 35.75)");
$select = $client->query("SELECT SUM(amount) AS balance FROM invoices WHERE customer_id = 1");

$insert
    ->concat($select)
    ->subscribe(...);

Testing

We use docker to run a postgresql instance for testing. To run locally, just install docker and run the following command from the project root:, (*11)

docker-compose -f docker/docker-compose.yml up -d

If you need to reset the database, just stop the docker instance and delete the docker/database directory. Restart the docker with the above command and it will initialize the database again., (*12)

The tests do not change the ending structure of the database, so you should not normally need to do this., (*13)

The Versions

18/04 2018

dev-master

9999999-dev

Async Reactive Postgres Driver for PHP (Non-blocking)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Matt Bonneau
by David Dan

reactive postgresql postgres driver async pgsql react rx.php

18/04 2018

2.0.1

2.0.1.0

Async Reactive Postgres Driver for PHP (Non-blocking)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Matt Bonneau
by David Dan

reactive postgresql postgres driver async pgsql react rx.php

15/11 2017

dev-cancellation

dev-cancellation

Async Reactive Postgres Driver for PHP (Non-blocking)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Matt Bonneau
by David Dan

reactive postgresql postgres driver async pgsql react rx.php

01/08 2017

2.0.0

2.0.0.0

Async Reactive Postgres Driver for PHP (Non-blocking)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Matt Bonneau
by David Dan

reactive postgresql postgres driver async pgsql react rx.php

23/06 2017

dev-rx2

dev-rx2

Async Reactive Postgres Driver for PHP (Non-blocking)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Matt Bonneau
by David Dan

reactive postgresql postgres driver async pgsql react rx.php

31/12 2016

1.0.3

1.0.3.0

Async Reactive Postgres Driver for PHP (Non-blocking)

  Sources   Download

MIT

The Requires

 

by Matt Bonneau
by David Dan

reactive postgresql postgres driver async pgsql react rx.php

06/12 2016

1.0.2

1.0.2.0

Async Reactive Postgres Driver for PHP (Non-blocking)

  Sources   Download

MIT

The Requires

 

by Matt Bonneau
by David Dan

reactive postgresql postgres driver async pgsql react rx.php

05/10 2016

1.0.1

1.0.1.0

Async Reactive Postgres Driver for PHP (Non-blocking)

  Sources   Download

MIT

The Requires

 

by Matt Bonneau
by David Dan

reactive postgresql postgres driver async pgsql react rx.php

26/08 2016

1.0.0

1.0.0.0

Async Reactive Postgres Driver for PHP (Non-blocking)

  Sources   Download

MIT

The Requires

 

by Matt Bonneau
by David Dan

reactive postgresql postgres driver async pgsql react rx.php

27/07 2016

0.1.2

0.1.2.0

Async Reactive Postgres Driver for PHP (Non-blocking)

  Sources   Download

MIT

The Requires

 

by Matt Bonneau
by David Dan

reactive postgresql postgres driver async pgsql react rx.php

25/02 2016

0.1.1

0.1.1.0

Async Reactive Postgres Driver for PHP (Non-blocking)

  Sources   Download

MIT

The Requires

 

by Matt Bonneau
by David Dan

reactive postgresql postgres driver async pgsql react rx.php

10/12 2015

0.1.0

0.1.0.0

Async Reactive Postgres Driver for PHP (Non-blocking)

  Sources   Download

MIT

The Requires

 

by Matt Bonneau
by David Dan

reactive postgresql postgres driver async pgsql react rx.php