{"id":21057791,"url":"https://github.com/driftphp/reactphp-dbal","last_synced_at":"2025-05-15T23:33:58.558Z","repository":{"id":37544285,"uuid":"238984081","full_name":"driftphp/reactphp-dbal","owner":"driftphp","description":"DBAL for ReactPHP on top of Doctrine","archived":false,"fork":false,"pushed_at":"2023-06-19T18:48:32.000Z","size":104,"stargazers_count":43,"open_issues_count":5,"forks_count":6,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-21T20:46:19.336Z","etag":null,"topics":["dbal","doctrine","mysql","postgresql","reactphp","sqlite"],"latest_commit_sha":null,"homepage":"https://driftphp.io","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/driftphp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-07T17:19:48.000Z","updated_at":"2025-02-27T12:47:52.000Z","dependencies_parsed_at":"2024-06-19T05:31:51.426Z","dependency_job_id":"e7601fd6-ebc3-4b71-99ee-aedf96f0b49a","html_url":"https://github.com/driftphp/reactphp-dbal","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/driftphp%2Freactphp-dbal","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/driftphp%2Freactphp-dbal/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/driftphp%2Freactphp-dbal/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/driftphp%2Freactphp-dbal/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/driftphp","download_url":"https://codeload.github.com/driftphp/reactphp-dbal/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254442429,"owners_count":22071864,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dbal","doctrine","mysql","postgresql","reactphp","sqlite"],"created_at":"2024-11-19T17:04:45.147Z","updated_at":"2025-05-15T23:33:58.535Z","avatar_url":"https://github.com/driftphp.png","language":"PHP","readme":"# DBAL for ReactPHP\n\n[![CircleCI](https://circleci.com/gh/driftphp/reactphp-dbal.svg?style=svg)](https://circleci.com/gh/driftphp/reactphp-dbal)\n\nThis is a DBAL on top of ReactPHP SQL clients and Doctrine model implementation.\nYou will be able to use\n\n- Doctrine QueryBuilder model\n- Doctrine Schema model\n- Easy-to-use shortcuts for common operations\n- and much more support is being added right now\n\n\u003e Attention. Only for proof of concept ATM. Do not use this library on\n\u003e production until the first stable version is tagged. \n\n## Example\n\nLet's create an example of what this library can really do. For this example, we\nwill create an adapter for Mysql, and will use Doctrine QueryBuilder to create a\nnew element in database and query for some rows.\n\nBecause we will use Mysql adapter, you should have installed the ReactPHP based\nmysql library `react/mysql`. Because this library is on development stage, all\nadapters dependencies will be loaded for testing purposes.\n\nFirst of all, we need to create a Connection instance with the selected platform\ndriver. We will have to create as well a Credentials instance with all the\nconnection data.\n\n```php\nuse Doctrine\\DBAL\\Platforms\\MySqlPlatform;\nuse Drift\\DBAL\\Connection;\nuse Drift\\DBAL\\Driver\\Mysql\\MysqlDriver;\nuse Drift\\DBAL\\Credentials;\nuse React\\EventLoop\\Factory as LoopFactory;\n\n$loop = LoopFactory::create();\n$mysqlPlatform = new MySqlPlatform();\n$mysqlDriver = new MysqlDriver($loop);\n$credentials = new Credentials(\n   '127.0.0.1',\n   '3306',\n   'root',\n   'root',\n   'test'\n);\n\n$connection = Connection::createConnected(\n    $mysqlDriver,\n    $credentials,\n    $mysqlPlatform\n);\n```\n\nOnce we have the connection, we can create a new register in the database by\nusing the Doctrine QueryBuilder or direct built-in methods. The result of all\nthese calls will be a Promise interface that, eventually, will return a Result\ninstance.\n\n```php\nuse Drift\\DBAL\\Connection;\nuse Drift\\DBAL\\Result;\n\n/**\n* @var Connection $connection\n */\n$promise = $connection\n    -\u003einsert('test', [\n        'id' =\u003e '1',\n        'field1' =\u003e 'val1',\n        'field2' =\u003e 'val2',\n    ])\n    -\u003ethen(function(Result $_) use ($connection) {\n        $queryBuilder = $connection-\u003ecreateQueryBuilder();\n        \n        return $connection\n            -\u003equery($queryBuilder)\n            -\u003eselect('*')\n            -\u003efrom('test', 't')\n            -\u003ewhere($queryBuilder-\u003eexpr()-\u003eorX(\n                $queryBuilder-\u003eexpr()-\u003eeq('t.id', '?'),\n                $queryBuilder-\u003eexpr()-\u003eeq('t.id', '?')\n            ))\n            -\u003esetParameters(['1', '2']);\n    })\n    -\u003ethen(function(Result $result) {\n        $numberOfRows = $result-\u003efetchCount();\n        $firstRow = $result-\u003efetchFirstRow();\n        $allRows = $result-\u003efetchAllRows();\n    });\n```\n\nYou can use, at this moment, adapters for `mysql`, `postgresql`, and `sqlite`.\n\n## Connection shortcuts\n\nThis DBAL introduce some shortcuts useful for your projects on top of Doctrine\nquery builder and escaping parametrization.\n\n### Insert\n\nInserts a new row in a table. Needs the table and an array with fields and their\nvalues. Returns a Promise.\n\n```php\n$connection-\u003einsert('test', [\n    'id' =\u003e '1',\n    'field1' =\u003e 'value1'\n]);\n```\n\n### Update\n\nUpdates an existing row from a table. Needs the table, an identifier as array\nand an array of fields with their values. Returns a Promise.\n\n```php\n$connection-\u003eupdate(\n    'test',\n    ['id' =\u003e '1'],\n    [\n        'field1' =\u003e 'value1',\n        'field2' =\u003e 'value2',\n    ]\n);\n```\n\n### Upsert\n\nInsert a row if not exists. Otherwise, it will update the existing row with\ngiven values. Needs the table, an identifier as array and an array of fields\nwith their values. Returns a Promise.\n\n```php\n$connection-\u003eupsert(\n    'test',\n    ['id' =\u003e '1'],\n    [\n        'field1' =\u003e 'value1',\n        'field2' =\u003e 'value2',\n    ]\n);\n```\n\n### Delete\n\nDeletes a row if exists. Needs the table and the identifier as array. Returns a\nPromise.\n\n```php\n$connection-\u003edelete('test', [\n    'id' =\u003e '1'\n]);\n```\n\n### Find one by\n\nFind a row given a where clause. Needs the table and an array of fields with \ntheir values. Returns a Promise with, eventually, the result as array of all\nfound rows.\n\n```php\n$connection\n    -\u003efindOneById('test', [\n        'id' =\u003e '1'\n    ])\n    -\u003ethen(function(?array $result) {\n        if (is_null($result)) {\n            // Row with ID=1 not found\n        } else {\n            // Row with ID=1 found.\n            echo $result['id'];\n        }   \n    });\n```\n\n### Find by\n\nFind all rows given an array of where clauses. Needs the table and an array of\nfields with their values. Returns a Promise with, eventually, the result as\narray of all found rows.\n\n```php\n$connection\n    -\u003efindBy('test', [\n        'age' =\u003e '33'\n    ])\n    -\u003ethen(function(array $result) {\n        echo 'Found ' . count($result) . ' rows'; \n    });\n```\n\n### Create table\n\nYou can easily create a new table with basic information. Needs the table name\nand an array of fields and types. Strings are considered with length `255`.\nFirst field position in the array will be considered as primary key. Returns a \nPromise with, eventually, the connection.\n\n```php\n$connection-\u003ecreateTable('test', [\n    'id' =\u003e 'string',\n    'name' =\u003e 'string',\n]);\n```\n\nThis is a basic table creation method. To create more complex tables, you can\nuse Doctrine's Schema model. You can execute all Schema SQLs generated by using\nthis method inside Connection named `executeSchema`. You'll find more\ninformation about this Schema model in\n[Doctrine documentation](https://www.doctrine-project.org/projects/doctrine-dbal/en/2.10/reference/schema-representation.html)\n\n```php\n$schema = new Schema();\n$table = $schema-\u003ecreateTable('test');\n$table-\u003eaddColumn('id', 'string');\n// ...\n\n$connection-\u003eexecuteSchema($schema);\n```\n\n### Drop table\n\nYou can easily drop an existing table. Needs just the table name, and returns,\neventually, the connection.\n\n```php\n$connection-\u003edropTable('test');\n```\n\n### Truncate table\n\nYou can easily truncate an existing table. Needs just the table name, and returns,\neventually, the connection.\n\n```php\n$connection-\u003etruncateTable('test');\n```\n\n## Tests\n\nYou can run tests by running `docker-compose up` and by doing\n`php vendor/bin/phpunit`.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdriftphp%2Freactphp-dbal","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdriftphp%2Freactphp-dbal","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdriftphp%2Freactphp-dbal/lists"}