{"id":16511148,"url":"https://github.com/danog/asyncorm","last_synced_at":"2025-03-16T19:30:24.442Z","repository":{"id":230757388,"uuid":"773490014","full_name":"danog/AsyncOrm","owner":"danog","description":"Async ORM based on AMPHP v3 and fibers.","archived":false,"fork":false,"pushed_at":"2024-09-24T18:36:55.000Z","size":203,"stargazers_count":12,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-12T15:58:49.339Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danog.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"github":"danog"}},"created_at":"2024-03-17T19:52:54.000Z","updated_at":"2024-09-24T18:36:59.000Z","dependencies_parsed_at":"2024-04-06T12:25:23.951Z","dependency_job_id":"9e6fadf9-e29b-41b7-988c-d04089bcfa42","html_url":"https://github.com/danog/AsyncOrm","commit_stats":null,"previous_names":["danog/asyncorm"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danog%2FAsyncOrm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danog%2FAsyncOrm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danog%2FAsyncOrm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danog%2FAsyncOrm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danog","download_url":"https://codeload.github.com/danog/AsyncOrm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243826780,"owners_count":20354220,"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":[],"created_at":"2024-10-11T15:59:07.966Z","updated_at":"2025-03-16T19:30:24.434Z","avatar_url":"https://github.com/danog.png","language":"PHP","funding_links":["https://github.com/sponsors/danog"],"categories":[],"sub_categories":[],"readme":"# Async ORM\n\n[![codecov](https://codecov.io/gh/danog/AsyncOrm/branch/master/graph/badge.svg)](https://codecov.io/gh/danog/AsyncOrm)\n[![Psalm coverage](https://shepherd.dev/github/danog/AsyncOrm/coverage.svg)](https://shepherd.dev/github/danog/AsyncOrm)\n[![Psalm level 1](https://shepherd.dev/github/danog/AsyncOrm/level.svg)](https://shepherd.dev/github/danog/AsyncOrm)\n![License](https://img.shields.io/github/license/danog/AsyncOrm)\n\nAsync PHP ORM based on AMPHP v3 and fibers, created by Daniil Gentili (https://daniil.it) and Alexander Pankratov (https://github.com/xtrime-ru).  \n\nSupports MySQL, Redis, Postgres.  \n\nFeatures read and write-back caching, type-specific optimizations, and much more!  \n\nThis ORM library was initially created for [MadelineProto](https://docs.madelineproto.xyz), an async PHP client API for the telegram MTProto protocol.  \n\n## Installation\n\n```bash\ncomposer require danog/async-orm\n```\n\n## Usage\n\nThere are two main ways to use the ORM: through automatic ORM properties, which automatically connects appropriately marked `DbArray` properties to the specified database, or by manually instantiating a `DbArray` with a `DbArrayBuilder`.\n\nThe `DbArray` obtained through one of the methods below is an abstract array object that automatically stores and fetches elements of the specified [type \u0026raquo;](#value-types), from the specified database.  \n\n`DbArray`s of type `ValueType::OBJECT` can contain objects extending `DbObject`.  \n\nClasses extending `DbObject` have a special `save` method that can be used to persist object changes to the database, as can be seen in the [example](https://github.com/danog/AsyncOrm/blob/master/examples/2-manual.php).  \n\n\n### Automatic ORM properties example\n\n```php\n\u003c?php declare(strict_types=1);\n\nuse Amp\\Mysql\\MysqlConfig;\nuse Amp\\Postgres\\PostgresConfig;\nuse Amp\\Redis\\RedisConfig;\nuse danog\\AsyncOrm\\Annotations\\OrmMappedArray;\nuse danog\\AsyncOrm\\DbArray;\nuse danog\\AsyncOrm\\DbAutoProperties;\nuse danog\\AsyncOrm\\DbObject;\nuse danog\\AsyncOrm\\KeyType;\nuse danog\\AsyncOrm\\Settings;\nuse danog\\AsyncOrm\\Settings\\MysqlSettings;\nuse danog\\AsyncOrm\\Settings\\PostgresSettings;\nuse danog\\AsyncOrm\\Settings\\RedisSettings;\nuse danog\\AsyncOrm\\ValueType;\n\nrequire __DIR__ . '/../vendor/autoload.php';\n\n// Any of the following database backends can be used,\n// remove the ones you don't need.\n$settings = new MysqlSettings(\n    new MysqlConfig(\n        host: \"/var/run/mysqld/mysqld.sock\",\n        user: 'user',\n        password: 'password',\n        database: 'database'\n    ),\n    cacheTtl: 100\n);\n$settings = new PostgresSettings(\n    new PostgresConfig(\n        host: \"127.0.0.1\",\n        user: \"user\",\n        password: \"password\",\n        database: \"database\"\n    ),\n    cacheTtl: 100\n);\n$settings = new RedisSettings(\n    RedisConfig::fromUri(\"redis://127.0.0.1\"),\n    cacheTtl: 100\n);\n\n/**\n * An object stored in a database.\n */\nclass MyObject extends DbObject\n{\n    public function __construct(\n        private string $value\n    ) {\n    }\n    public function setValue(string $value): void\n    {\n        $this-\u003evalue = $value;\n    }\n    public function getValue(): string\n    {\n        return $this-\u003evalue;\n    }\n}\n\n/**\n * Main class of your application.\n */\nfinal class Application\n{\n    use DbAutoProperties;\n\n    /**\n     * This field is automatically connected to the database using the specified Settings.\n     *\n     * @var DbArray\u003cstring, MyObject\u003e\n     */\n    #[OrmMappedArray(KeyType::STRING, ValueType::OBJECT)]\n    private DbArray $dbProperty1;\n\n    /**\n     * This field is automatically connected to the database using the specified Settings.\n     *\n     * @var DbArray\u003cstring, int\u003e\n     */\n    #[OrmMappedArray(KeyType::STRING, ValueType::INT)]\n    private DbArray $dbProperty2;\n\n    public function __construct(\n        Settings $settings,\n        string $tablePrefix\n    ) {\n        $this-\u003einitDbProperties($settings, $tablePrefix);\n    }\n\n    public function businessLogic(): void\n    {\n        $this-\u003edbProperty1['someOtherKey'] = new MyObject(\"initialValue\");\n\n        // Can store integers, strings, arrays or objects depending on the specified ValueType\n        $this-\u003edbProperty2['someKey'] = 123;\n        var_dump($this-\u003edbProperty2['someKey']);\n    }\n\n    public function businessLogic2(string $value): void\n    {\n        $obj = $this-\u003edbProperty1['someOtherKey'];\n        $obj-\u003esetValue($value);\n        $obj-\u003esave();\n    }\n\n    public function businessLogic3(): string\n    {\n        return $this-\u003edbProperty1['someOtherKey']-\u003egetValue();\n    }\n\n    public function shutdown(): void\n    {\n        // Flush all database caches, saving all changes.\n        $this-\u003esaveDbProperties();\n    }\n}\n\n$app = new Application($settings, 'tablePrefix');\n$app-\u003ebusinessLogic();\n$app-\u003ebusinessLogic2(\"newValue\");\nvar_dump($app-\u003ebusinessLogic3());\n$app-\u003eshutdown();\n```\n\n### Manual ORM properties example\n\nSee [here \u0026raquo;](https://github.com/danog/AsyncOrm/blob/master/examples/2-manual.php)\n\n### Settings\n\nAs specified in the examples above, there are multiple settings classes that can be used to connect to a specific database type:  \n\n* [MysqlSettings: MySQL backend settings.](https://github.com/danog/AsyncOrm/blob/master/docs/docs/danog/AsyncOrm/Settings/MysqlSettings.md)\n* [PostgresSettings: Postgres backend settings.](https://github.com/danog/AsyncOrm/blob/master/docs/docs/danog/AsyncOrm/Settings/PostgresSettings.md)\n* [RedisSettings: Redis backend settings.](https://github.com/danog/AsyncOrm/blob/master/docs/docs/danog/AsyncOrm/Settings/RedisSettings.md)\n\nAll these classes have multiple fields, described in their respective documentation (click on each class name to view it).  \n\n#### Caching\n\nOne of the most important settings is the `cacheTtl` field, which specifies the duration of the read and write cache.  \n\nIf non-zero, all array elements fetched from the database will be stored in an in-memory *read cache* for the specified number of seconds; multiple accesses to the same field will each postpone flushing of that field by `cacheTtl` seconds.  \n\nAll elements written to the array by the application will also be stored in an in-memory *write cache*, and flushed to the database every `cacheTtl` seconds.  \n\nThe cache is also flushed on shutdown by deferring an event loop callback, so make sure `EventLoop::run();` is being used to run the application, to make sure all data is flushed correctly (alternatively, `saveDbProperties` can be used to manually flush the cache on shutdown when using automatic properties, and `flushCache` in manual mode).  \n\nIf the array has an [object value type (ValueType::OBJECT)](#key-and-value-types), write caching is disabled.  \n\nIf `cacheTtl` is 0, read and write caching is disabled.  \n\nA special setting class is used to create `DbArray`s backed by no database, which can also be useful in certain circumstances:  \n\n* [MemorySettings: MemorySettings backend settings.](https://github.com/danog/AsyncOrm/blob/master/docs/docs/danog/AsyncOrm/Settings/MemorySettings.md)\n\n\n### Key and value types\n\nEach DbArray must have a specific key and value type.  \n\nFor optimal performance, the specified types must be as strict as possible, here's a list of allowed types:  \n\n#### Key types\n\n* `KeyType::STRING` - String keys only\n* `KeyType::INT` - Integer keys only\n* `KeyType::STRING_OR_INT` - String or integer keys (not recommended, for performance reasons please always specify either `STRING` or `STRING_OR_INT`).  \n\n#### Value types\n\n* `ValueType::STRING`: Direct storage of UTF-8 string values.\n* `ValueType::INT`: Direct storage of integer values.\n* `ValueType::BOOL`: Direct storage of boolean values.\n* `ValueType::FLOAT`: Direct storage of floating point (double precision) values.\n* `ValueType::SCALAR`: Values of any scalar type (including blobs and arrays, excluding objects), serialized as specified in the settings.\n   Using SCALAR worsens performances, please use any of the other types if possible.\n* `ValueType::OBJECT`: Objects extending DbObject, serialized as specified in the settings.\n\nOne of the most important value types is `ValueType::OBJECT`, it is used to store entire objects extending the `DbObject` class to the database.  \n\nObjects extending `DbObject` have a special `save` method that can be used to persist object changes to the database, as can be seen in the [example](https://github.com/danog/AsyncOrm/blob/master/examples/2-manual.php).  \n\n## API Documentation\n\nClick [here \u0026raquo;](https://github.com/danog/AsyncOrm/blob/master/docs/docs/index.md) to view the API documentation.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanog%2Fasyncorm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanog%2Fasyncorm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanog%2Fasyncorm/lists"}