Go to file
2024-04-07 11:51:28 +02:00
.github Cleanup GHA 2024-03-31 19:45:37 +02:00
docs Bump 2024-03-31 19:12:01 +02:00
examples Improve documentation 2024-04-06 13:13:50 +02:00
src Avoid issues when serializing during migration 2024-04-07 11:51:28 +02:00
tests Improve tests 2024-04-06 20:00:44 +02:00
.gitattributes Add basic README 2024-03-28 23:07:09 +01:00
.gitignore Cleanup README.md, legacy MadelineProto table migration coverage 2024-04-06 19:52:36 +02:00
.php-cs-fixer.dist.php First commit 2024-03-17 20:20:00 +01:00
composer.json Add badges 2024-03-31 19:49:11 +02:00
infection.json5 Rename a few classes 2024-03-31 17:13:03 +02:00
LICENSE Change license to Apache 2 2024-03-31 18:28:23 +02:00
NOTICE Change license to Apache 2 2024-03-31 18:28:23 +02:00
phpunit.xml Finalize 2024-03-31 17:05:51 +02:00
psalm-baseline.xml Cleanup 2024-04-06 20:02:22 +02:00
psalm.xml Psalm fixes 2024-03-31 20:12:14 +02:00
README.md Cleanup README.md, legacy MadelineProto table migration coverage 2024-04-06 19:52:36 +02:00

Async ORM

codecov Psalm coverage Psalm level 1 License

Async ORM based on AMPHP v3 and fibers, created by Daniil Gentili (https://daniil.it) and Alexander Pankratov (alexander@i-c-a.su).

Supports MySQL, Redis, Postgres.

Features read and write-back caching, type-specific optimizations, and much more!

This ORM library was initially created for MadelineProto, an async PHP client API for the telegram MTProto protocol.

Installation

composer require danog/async-orm

Usage

There 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.

The DbArray obtained through one of the methods above is an abstract array object that automatically stores and fetches elements of the specified type », from the specified database.

DbArrays of type ValueType::OBJECT can contain objects extending DbObject.

Classes 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.

Settings

As specified in the examples above, there are multiple settings classes that can be used to connect to a specific database type:

All these classes have multiple fields, described in their respective documentation (click on each class name to view it).

Caching

One of the most important settings is the cacheTtl field, which specifies the duration of the read and write cache.

If 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.

All 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.

If the array has an object value type (ValueType::OBJECT), write caching is disabled.

If cacheTtl is 0, read and write caching is disabled.

A special setting class is used to create DbArrays backed by no database, which can also be useful in certain circumstances:

Key and value types

Each DbArray must have a specific key and value type.

For optimal performance, the specified types must be as strict as possible, here's a list of allowed types:

Key types

  • KeyType::STRING - String keys only
  • KeyType::INT - Integer keys only
  • KeyType::STRING_OR_INT - String or integer keys (not recommended, for performance reasons please always specify either STRING or STRING_OR_INT).

Value types

  • ValueType::STRING: Direct storage of UTF-8 string values.
  • ValueType::INT: Direct storage of integer values.
  • ValueType::BOOL: Direct storage of boolean values.
  • ValueType::FLOAT: Direct storage of floating point (double precision) values.
  • ValueType::SCALAR: Values of any scalar type (including arrays, excluding objects), serialized as specified in the settings. Using SCALAR worsens performances, please use any of the other types if possible.
  • ValueType::OBJECT: Objects extending DbObject, serialized as specified in the settings.

One of the most important value types is ValueType::OBJECT, it is used to store entire objects extending the DbObject class to the database.

Objects 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.

API Documentation

Click here » to view the API documentation.