{"id":15027620,"url":"https://github.com/compositephp/db","last_synced_at":"2025-04-05T07:06:52.886Z","repository":{"id":61517327,"uuid":"534980507","full_name":"compositephp/db","owner":"compositephp","description":"Fast and Lightweight PHP 8.1+ DataMapper and Table Gateway with automatic data caching, smart entities, migrations and many more","archived":false,"fork":false,"pushed_at":"2024-12-23T22:43:27.000Z","size":297,"stargazers_count":81,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-29T06:08:19.802Z","etag":null,"topics":["database","datamapper","doctrine","entity","fast","hydration","lightweight","mysql","php","php81","posgresql","sqlite","tablegateway"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/compositephp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2022-09-10T11:42:21.000Z","updated_at":"2025-01-28T23:09:54.000Z","dependencies_parsed_at":"2023-10-15T11:44:21.715Z","dependency_job_id":"392812b0-f677-4db8-8056-5cb1c707993a","html_url":"https://github.com/compositephp/db","commit_stats":{"total_commits":28,"total_committers":3,"mean_commits":9.333333333333334,"dds":0.2857142857142857,"last_synced_commit":"3ffda2b0b282f8f3ee9dde1edd052629c2b41d76"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compositephp%2Fdb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compositephp%2Fdb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compositephp%2Fdb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/compositephp%2Fdb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/compositephp","download_url":"https://codeload.github.com/compositephp/db/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247299832,"owners_count":20916190,"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":["database","datamapper","doctrine","entity","fast","hydration","lightweight","mysql","php","php81","posgresql","sqlite","tablegateway"],"created_at":"2024-09-24T20:06:47.824Z","updated_at":"2025-04-05T07:06:52.861Z","avatar_url":"https://github.com/compositephp.png","language":"PHP","readme":"# What is Composite DB\n[![Latest Stable Version](https://poser.pugx.org/compositephp/db/v/stable)](https://packagist.org/packages/compositephp/db)\n[![Build Status](https://github.com/compositephp/db/actions/workflows/main.yml/badge.svg)](https://github.com/compositephp/db/actions)\n[![Codecov](https://codecov.io/gh/compositephp/db/branch/master/graph/badge.svg)](https://codecov.io/gh/compositephp/db/)\n\nComposite DB is lightweight and fast PHP DataMapper and Table Gateway which allows you to represent your SQL tables \nscheme in OOP style using full power of PHP 8.1+ class syntax. \n\nIt also gives you CRUD, query builder and automatic caching out of the box, so you can start\nto work with your database from php code in a minutes!\n\nOverview:\n* [Features](#features)\n* [Requirements](#requirements)\n* [Installation](#installation)\n* [Quick example](#quick-example)\n* [Documentation](doc/README.md)\n\n## Features\n\n* **Lightweight** - easier entity schema, no getters and setters, you don't need attributes for each column definition, \njust use native php class syntax.\n* **Speed** - it's 1.5x faster in pure SQL queries mode and many times faster in automatic caching mode (see [benchmark](https://github.com/compositephp/php-orm-benchmark)).\n* **Easy caching** - gives you CRUD operations caching out of the box and in general it's much easier to work with cached \"selects\".\n* **Strict types** - Composite DB forces you to be more strict typed and makes your IDE happy.\n* **Hydration** - you can serialize your Entities to plain array or json and deserialize them back.\n* **Flexibility** - gives you more freedom to extend Repositories, for example it's easier to build sharding tables.\n* **Code generation** - you can generate Entity and Repository classes from your SQL tables.\n* **Division of responsibility** - every Entity has its own Repository class, and it's the only entry point to make queries to your table.\n\nIt also has many popular features such as:\n\n* **Query Builder** - build your queries with constructor, based on [doctrine/dbal](https://github.com/doctrine/dbal)\n* **Migrations** - synchronise your php entities with database tables\n\n## Requirements\n\n* PHP 8.1+\n* PDO Extension with desired database drivers\n\n## Installation\n\n1. Install package via composer:\n    ```shell\n    $ composer require compositephp/db\n    ```\n2. Configure `Composite\\DB\\ConnectionManager` ([example](./doc/configuration.md#configure-connectionmanager))\n3. (Optional) Configure [symfony/console](https://symfony.com/doc/current/components/console.html#creating-a-console-application) \ncommands to use automatic class generators ([example](./doc/configuration.md#configure-console-commands))\n4. (Optional) Install and configure any PSR-16 (simple cache) package to use automatic caching\n\n## Quick example\nImagine you have simple table `Users`\n\n```mysql\ncreate table Users\n(\n    `id`         int auto_increment,\n    `email`      varchar(255)                                         not null,\n    `name`       varchar(255)               default NULL              null,\n    `is_test`    tinyint(1)                 default 0                 not null,\n    `languages`  json                       default '[]'              not null,\n    `status`     enum (\"ACTIVE\", \"BLOCKED\") default \"ACTIVE\"          null,\n    `created_at` TIMESTAMP                  default CURRENT_TIMESTAMP not null,\n    constraint Users_pk primary key (id)\n);\n```\n\nFirst, you need to do is to execute command to generate php entity:\n\n```shell\n$ php console.php composite-db:generate-entity dbName Users 'App\\User'\n```\n\nNew entity class `User` and enum `Status` will be automatically generated:\n\n```php\n#[Table(db: 'dbName', name: 'Users')]\nclass User extends AbstractEntity\n{\n    #[PrimaryKey(autoIncrement: true)]\n    public readonly int $id;\n\n    public function __construct(\n        public string $email,\n        public ?string $name = null,\n        public bool $is_test = false,\n        public array $languages = [],\n        public Status $status = Status::ACTIVE,\n        public readonly \\DateTimeImmutable $created_at = new \\DateTimeImmutable(),\n    ) {}\n}\n```\n\n```php\nenum Status\n{\n    case ACTIVE;\n    case BLOCKED;\n}\n```\n\nSecond step, is to generate a table class (repository) for your entity:\n\n```shell\n$ php console.php composite-db:generate-table 'App\\User' 'App\\UsersTable'\n```\n\nAnd that's it, now you have CRUD for your SQL table and simple selects:\n\n```php\n$table = new UsersTable();\n\n//Create\n$user = new User(\n    email: 'user@example.com',\n    name: 'John',\n    languages: ['en', 'fr'],\n);\n$table-\u003esave($user);\n\n//Read\n$user = $table-\u003efindByPk(123);\n\n//Update\n$user-\u003estatus = Status::BLOCKED;\n$table-\u003esave($user);\n\n//Delete\n$table-\u003edelete($user);\n\n//Other selects out of the box\n$table-\u003efindAll();\n$table-\u003ecountAll();\n```\n\n\u003e You can find full working example [here](doc/example.md) which you can copy and run as is.\n\nYou can also serialize user entity to array or json:\n\n```php\nvar_export($user-\u003etoArray());\n\n//will output\narray (\n  'id' =\u003e 123,\n  'email' =\u003e 'user@example.com',\n  'name' =\u003e 'John',\n  'is_test' =\u003e false,\n  'languages' =\u003e '[\"en\",\"fr\"]',\n  'status' =\u003e 'BLOCKED',\n  'created_at' =\u003e '2022-01-01 11:22:33.000000',\n)\n```\n\nOr deserialize (hydrate) entity from array:\n\n```php\n$user = User::fromArray([\n  'id' =\u003e 123,\n  'email' =\u003e 'user@example.com',\n  'name' =\u003e 'John',\n  'is_test' =\u003e false,\n  'languages' =\u003e '[\"en\",\"fr\"]',\n  'status' =\u003e 'BLOCKED',\n  'created_at' =\u003e '2022-01-01 11:22:33.000000',\n]);\n```\n\nAnd that's it, no special getters or setters, no \"behaviours\" or extra code, smart entity casts everything automatically.\nMore about Entity and supported auto casting types you can find [here](doc/entity.md).\n\n## License:\n\nMIT License (MIT). Please see [`LICENSE`](./LICENSE) for more information. Maintained by Composite PHP.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcompositephp%2Fdb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcompositephp%2Fdb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcompositephp%2Fdb/lists"}