{"id":36992000,"url":"https://github.com/peroks/model-store","last_synced_at":"2026-01-13T23:44:08.870Z","repository":{"id":128342683,"uuid":"603386843","full_name":"peroks/model-store","owner":"peroks","description":"Model Store: Permanent data store for models.","archived":false,"fork":false,"pushed_at":"2025-06-08T11:46:01.000Z","size":144,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-08T12:31:02.449Z","etag":null,"topics":["database","json","mysql","mysqli","pdo","pdo-mysql","persistent-storage"],"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/peroks.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,"zenodo":null}},"created_at":"2023-02-18T11:00:12.000Z","updated_at":"2025-06-08T11:44:35.000Z","dependencies_parsed_at":"2023-12-20T09:38:08.674Z","dependency_job_id":"83af3a60-67c1-4074-b921-fbd71ab35d1c","html_url":"https://github.com/peroks/model-store","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/peroks/model-store","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peroks%2Fmodel-store","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peroks%2Fmodel-store/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peroks%2Fmodel-store/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peroks%2Fmodel-store/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/peroks","download_url":"https://codeload.github.com/peroks/model-store/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peroks%2Fmodel-store/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28405175,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T21:51:37.118Z","status":"ssl_error","status_checked_at":"2026-01-13T21:45:14.585Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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","json","mysql","mysqli","pdo","pdo-mysql","persistent-storage"],"created_at":"2026-01-13T23:44:08.794Z","updated_at":"2026-01-13T23:44:08.850Z","avatar_url":"https://github.com/peroks.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Model Store: Permanent data store for models.\n\n## Reason why\n\nThe purpose of this package is to store models **permanently**. Currently,\n**JSON files** and **MySql databases** (mysqli and pdo-mysql) are supported.\n\nThe Model Store is an abstraction layer on top of the permanent store.\nIt automatically creates **JSON files** or **database schemas** for you based\non your models.\n\nThe Model Store provides a simple [interface](src/StoreInterface.php) for\nreading models from and writing models to the permanents store.\n\n## How to use\n\n### The Store interface\n\nYou can of course access a database directly, but the\nrecommended way it to create a `Store instance` and use the\n[StoreInterface](src/StoreInterface.php).\n\n### Connecting to a model store\n\nIn order to connect to a model store, you must create a new **model store instance**.\nCurrently, these model stores are supported:\n\n- `FileStore`: JSON file store\n- `MysqlStore`: Native MySql store (mysqli)\n- `MysqlJsonStore`: Native MySql (mysqli) store with JSON support\n- `PdoStore`: PDO MySql store (pdo-mysqli)\n- `PdoJsonStore`: PDO MySql (pdo-mysqli) store with JSON support\n\nThe JsonStore classes stores models in MySQL `json` columns with\nadditional columns for indices and constraints.\n\nYou can also create your own implementation of the\n[StoreInterface](src/StoreInterface.php).\n\n#### File store\n\nStoring your models in a JSON file is only recommended for **very small** data\nstores, no more than a few MBs. It's intended for use in **development**\nand **rapid prototyping**, but not in **production**. For each PHP request\nthe complete JSON file is loaded into memory, and it will consume more\nand more **ram** and **cpu** as the file grows.\n\nTo connect to a JSON file store, just provide the full path and file name\nto the JSON file which contains your models. If the file does not\nexist, it will be created.\n\n```php\nuse Peroks\\Model\\Store\\FileStore;\n$store = new FileStore( '/\u003cpath\u003e/\u003cfilename\u003e.json' );\n```\n\n#### PDO MySql store\n\nTo connect to a PDO MySql store, just provide the\n[connection info](https://www.php.net/manual/en/mysqli.quickstart.connections.php)\nfor the MySQL database.\n\nAll connection properties below are required, except for `port` and `socket`,\nwhich are mutually exclusive. If the host is `localhost`, a `socket` is expected.\nThe connection info can be an `array` or an `object`.\n\n```php\nuse Peroks\\Model\\Store\\PdoStore;\n$store = new PdoStore( [\n    'host'   =\u003e 'localhost|\u003chost name\u003e|\u003cip address\u003e',\n    'name'   =\u003e '\u003cdb name\u003e',\n    'user'   =\u003e '\u003cdb username\u003e',\n    'pass'   =\u003e '\u003cdb password\u003e',\n    'port'   =\u003e '\u003cport\u003e',\n    'socket' =\u003e '\u003csocket\u003e',\n] );\n```\n\nAlternatively, you can use the `PdoJsonStore` class, which stores the\nmodels in MySql `json` columns. Additional columns are only created for\nprimary, index and constraint properties.\n\n#### Native MySql (mysqli) store\n\nYou can also connect to a MySql database using the native `mysqli` driver\nif you prefer. Just replace the store class `PdoStore` with `MysqlStore`.\n\n```php\nuse Peroks\\Model\\Store\\MysqlStore;\n$store = new MysqlStore( [\n    'host'   =\u003e 'localhost|\u003chost name\u003e|\u003cip address\u003e',\n    'name'   =\u003e '\u003cdb name\u003e',\n    'user'   =\u003e '\u003cdb username\u003e',\n    'pass'   =\u003e '\u003cdb password\u003e',\n    'port'   =\u003e '\u003cport\u003e',\n    'socket' =\u003e '\u003csocket\u003e',\n] );\n```\n\nAlternatively, you can use the `MysqlJsonStore` class, which stores the\nmodels in MySql `json` columns. Additional columns are only created for\nprimary, index and constraint properties.\n\n### Creating and Updating database schemas\n\nBefore you can start using a database store, you need to build the\n**database schema** based on your models. Fortunately, you don't need to do this\nmanually. To create (and update) your database schema, call the `build()`\nmethod with an array of the model **class names** that you want to store.\nThis will also create a new database if it doesn't already exist.\n\nYou should only call the `build()` method when you create a new model store or\nwhen your models have changed. Do **not** call `build()` every time you connect to\nthe store. You can use `info( 'ready' )` to check if the db is ready for use or not.\n\n```php\nuse Peroks\\Model\\Store\\MysqlStore;\n$store = new MysqlStore( $connection );\n\nif ( ! $store-\u003einfo( 'ready' ) ) {\n    $store-\u003ebuild( [\n        MyModelOne::class,\n        MyModelTwo::class,\n        MyModelThree::class,\n    ] );\n}\n\n```\n\nIf a model contains [sub-models](https://github.com/peroks/model#nested-models),\ndatabase tables are automatically created for the sub-models.\nYou do not need to include sub-models in the `build()` method.\nSo, if you have a hierarchy of models, you only need to provide\nyour **top-level** models.\n\n## Caching\n\nYou can cache query results in memory with the special `Cache` store.\nThe constructor takes another store instance as the only argument.\nCalls to `has()`, `get()`, `list()` and `filter()` return cached results when available.\nThe cache is cleared every time `set()`, `delete()` or `build()` are called.\n\n```php\nuse Peroks\\Model\\Store\\Cache;\nuse Peroks\\Model\\Store\\PdoJsonStore;\n\n$store = new Cache( new PdoJsonStore( $connection ) );\n$model = $store-\u003eget( SomeClass::class, 'someId' );\n$model = $store-\u003eget( SomeClass::class, 'someId' ); // Cached result.\n```\n\n## Examples\n\nThe below examples assume that a model store instance has already been created,\ni.e. like this\n\n```php\nuse Peroks\\Model\\Store\\MysqlStore;\n$store = new MysqlStore( [\n    'host'   =\u003e 'localhost|\u003chost name\u003e|\u003cip address\u003e',\n    'name'   =\u003e '\u003cdb name\u003e',\n    'user'   =\u003e '\u003cdb username\u003e',\n    'pass'   =\u003e '\u003cdb password\u003e',\n    'port'   =\u003e '\u003cport\u003e',\n    'socket' =\u003e '\u003csocket\u003e',\n] );\n```\n\nAll methods accept the **model class name** as the first argument.\nThe only exception is `set`, since the class name can be derived from the\nmodel instance.\n\n#### Check if a model exists in the store\n\n```php\n$exists = $store-\u003ehas( MyModelOne::class, 123 );\n$exists = $store-\u003ehas( MyModelOne::class, 'abc' );\n```\n\n#### Get a single model by id\n\n```php\n$stored_model = $store-\u003eget( MyModelOne::class, 123 );\n$stored_model = $store-\u003eget( MyModelOne::class, 'abc' );\n```\n\n#### Get an array of models by their ids\n\n```php\n$some_models = $store-\u003elist( MyModelOne::class, [123, 'abc', 'xyz'] );\n$all_models  = $store-\u003elist( MyModelOne::class );\n```\n\nIf no ids are provided, all models of the given class are returned.\n\n#### Get models by their property values\n\nThe `filter` method returns all models of the given **class name** matching\npairs of property ids and their values, i.e.\n\n```php\n$some_artists = $store-\u003efilter( Artist::class, [\n    'first_name' =\u003e 'Tom',\n    'last_name'  =\u003e 'Waits',\n] );\n\n$all_artists = $store-\u003efilter( Artist::class );\n```\n\nIf no property filter is provided, all models of the given class are returned\n(same as `list()`).\n\n#### Add or update a model in the store\n\n```php\n$model = new Artist( [ 'first_name' =\u003e 'Tom', 'last_name' =\u003e 'Waits' ] );\n$store-\u003eset( $model );\n```\n\n#### Delete a model from the store\n\n```php\n$store-\u003edelete( MyModelOne::class, 123 );\n$store-\u003edelete( MyModelOne::class, 'abc' );\n```\n\n## Installing\n\nYou need **composer** to download and install this\n[package](https://packagist.org/packages/peroks/model-store).\nJust run `composer require peroks/model-store` in your project.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperoks%2Fmodel-store","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fperoks%2Fmodel-store","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperoks%2Fmodel-store/lists"}