{"id":46645078,"url":"https://github.com/phpnomad/db","last_synced_at":"2026-06-12T12:00:15.930Z","repository":{"id":244274146,"uuid":"701559828","full_name":"phpnomad/db","owner":"phpnomad","description":"Database abstraction layer with table schemas, query building, caching, and event broadcasting","archived":false,"fork":false,"pushed_at":"2026-05-27T20:49:05.000Z","size":330,"stargazers_count":0,"open_issues_count":3,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2026-05-27T22:20:43.937Z","etag":null,"topics":["database","datastore","framework","php","phpnomad","platform-agnostic","query-builder","sql"],"latest_commit_sha":null,"homepage":"https://phpnomad.com","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/phpnomad.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-10-06T23:11:43.000Z","updated_at":"2026-05-27T20:44:56.000Z","dependencies_parsed_at":"2024-06-13T19:56:11.942Z","dependency_job_id":"dea17cac-4429-4e28-ad19-2a856e05f121","html_url":"https://github.com/phpnomad/db","commit_stats":null,"previous_names":["phpnomad/db"],"tags_count":12,"template":false,"template_full_name":"phpnomad/repository-template","purl":"pkg:github/phpnomad/db","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpnomad%2Fdb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpnomad%2Fdb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpnomad%2Fdb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpnomad%2Fdb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phpnomad","download_url":"https://codeload.github.com/phpnomad/db/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpnomad%2Fdb/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34243053,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-12T02:00:06.859Z","response_time":109,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","datastore","framework","php","phpnomad","platform-agnostic","query-builder","sql"],"created_at":"2026-03-08T04:31:39.768Z","updated_at":"2026-06-12T12:00:15.824Z","avatar_url":"https://github.com/phpnomad.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# phpnomad/db\n\n[![Latest Version](https://img.shields.io/packagist/v/phpnomad/db.svg)](https://packagist.org/packages/phpnomad/db)\n[![Total Downloads](https://img.shields.io/packagist/dt/phpnomad/db.svg)](https://packagist.org/packages/phpnomad/db)\n[![PHP Version](https://img.shields.io/packagist/php-v/phpnomad/db.svg)](https://packagist.org/packages/phpnomad/db)\n[![License](https://img.shields.io/packagist/l/phpnomad/db.svg)](https://packagist.org/packages/phpnomad/db)\n\n`phpnomad/db` implements the datastore pattern for SQL databases. It gives you table schemas, query building, automatic caching, and event broadcasting on top of the storage-agnostic abstractions in `phpnomad/datastore`, so your handlers stay free of raw SQL and your domain logic stays portable.\n\nYou get full CRUD operations, condition-array querying, cache lookups on reads, cache invalidation on writes, and `RecordCreated`, `RecordUpdated`, and `RecordDeleted` events dispatched from every mutation. The package powers the data layer in [Siren](https://sirenaffiliates.com) and has been in production for years.\n\n## Installation\n\n```bash\ncomposer require phpnomad/db\n```\n\nThis package is the abstraction layer. To actually execute queries you also need a concrete integration like `phpnomad/mysql-db-integration`, which provides the `QueryStrategy` and table-management strategies that `phpnomad/db` delegates to.\n\n## Quick Start\n\nDefine a table schema by extending `Table`. Columns and indices come from value objects and factories, and a table version string lets migrations detect schema changes.\n\n```php\n\u003c?php\n\nuse PHPNomad\\Database\\Abstracts\\Table;\nuse PHPNomad\\Database\\Factories\\Column;\nuse PHPNomad\\Database\\Factories\\Columns\\DateCreatedFactory;\nuse PHPNomad\\Database\\Factories\\Columns\\DateModifiedFactory;\nuse PHPNomad\\Database\\Factories\\Columns\\PrimaryKeyFactory;\nuse PHPNomad\\Database\\Factories\\Index;\n\nclass PostsTable extends Table\n{\n    public function getUnprefixedName(): string\n    {\n        return 'posts';\n    }\n\n    public function getSingularUnprefixedName(): string\n    {\n        return 'post';\n    }\n\n    public function getAlias(): string\n    {\n        return 'p';\n    }\n\n    public function getTableVersion(): string\n    {\n        return '1';\n    }\n\n    public function getColumns(): array\n    {\n        return [\n            (new PrimaryKeyFactory())-\u003etoColumn(),\n            new Column('title', 'VARCHAR', [255], 'NOT NULL'),\n            new Column('content', 'TEXT', null, 'NOT NULL'),\n            new Column('status', 'VARCHAR', [20], \"NOT NULL DEFAULT 'draft'\"),\n            (new DateCreatedFactory())-\u003etoColumn(),\n            (new DateModifiedFactory())-\u003etoColumn(),\n        ];\n    }\n\n    public function getIndices(): array\n    {\n        return [\n            new Index(['status'], 'idx_posts_status'),\n        ];\n    }\n}\n```\n\nCreate a handler by extending `IdentifiableDatabaseDatastoreHandler` and pulling in `WithDatastoreHandlerMethods`. The base class implements `find`, `findMultiple`, `update`, and `delete` against the `id` column, and the trait supplies the rest of the CRUD surface along with cache and event wiring.\n\n```php\n\u003c?php\n\nuse PHPNomad\\Database\\Abstracts\\IdentifiableDatabaseDatastoreHandler;\nuse PHPNomad\\Database\\Providers\\DatabaseServiceProvider;\nuse PHPNomad\\Database\\Services\\TableSchemaService;\nuse PHPNomad\\Database\\Traits\\WithDatastoreHandlerMethods;\n\nclass PostDatabaseDatastoreHandler extends IdentifiableDatabaseDatastoreHandler\n{\n    use WithDatastoreHandlerMethods;\n\n    public function __construct(\n        DatabaseServiceProvider $serviceProvider,\n        PostsTable              $table,\n        PostAdapter             $adapter,\n        TableSchemaService      $tableSchemaService\n    ) {\n        $this-\u003eserviceProvider    = $serviceProvider;\n        $this-\u003etable              = $table;\n        $this-\u003emodelAdapter       = $adapter;\n        $this-\u003etableSchemaService = $tableSchemaService;\n        $this-\u003emodel              = Post::class;\n    }\n}\n```\n\nOnce the handler is wired into your container, reads hit the cache first and writes invalidate it automatically. Complex reads use condition arrays instead of raw SQL.\n\n```php\n$published = $postHandler-\u003ewhere([\n    [\n        'type' =\u003e 'AND',\n        'clauses' =\u003e [\n            ['column' =\u003e 'status', 'operator' =\u003e '=', 'value' =\u003e 'published'],\n            ['column' =\u003e 'views', 'operator' =\u003e '\u003e', 'value' =\u003e 1000],\n        ],\n    ],\n], limit: 10);\n```\n\nThe `QueryBuilder` turns that array into parameterized SQL and runs it through whichever `QueryStrategy` your integration package provides.\n\n## Key Concepts\n\n- Extend `Table` to define a schema with columns, indices, and a version string for migrations\n- Extend `IdentifiableDatabaseDatastoreHandler` as the base for any handler keyed by a single `id` column\n- Use the `WithDatastoreHandlerMethods` trait for CRUD, cache reads, cache invalidation, and event dispatch\n- Build reads and writes through `QueryBuilder` and `ClauseBuilder` with condition arrays instead of raw SQL\n- Inject `DatabaseServiceProvider` into every handler to access `QueryBuilder`, `QueryStrategy`, `ClauseBuilder`, `CacheableService`, `EventStrategy`, and `LoggerStrategy`\n- Use column factories like `PrimaryKeyFactory`, `DateCreatedFactory`, `DateModifiedFactory`, and `ForeignKeyFactory` for common column patterns\n- Model many-to-many relationships with `JunctionTable`, which handles compound primary keys and foreign key constraints\n\n## Documentation\n\nFull documentation lives at [phpnomad.com](https://phpnomad.com), including detailed guides on table schema definition, database handlers, query building, caching and event broadcasting, junction tables, and the column and index factories.\n\n## License\n\nMIT License. See [LICENSE.txt](LICENSE.txt).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpnomad%2Fdb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphpnomad%2Fdb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpnomad%2Fdb/lists"}