diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 274c9e942..0057824ec 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -69,6 +69,80 @@ This is also important for the Utopia-php lead developers to be able to give tec You can follow our [Adding new Database Adapter](docs/add-new-adapter.md) tutorial to add new database support in this library. +## Tests + +To run tests, you first need to bring up the example Docker stack with the following command: + +```bash +docker compose up -d --build +``` + +To run all unit tests, use the following Docker command: + +```bash +docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml tests +``` + +To run tests for a single file, use the following Docker command structure: + +```bash +docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml tests/Database/[FILE_PATH] +``` + +To run static code analysis, use the following phpstan command: + +```bash +composer check +``` + +### Load testing + +Three commands have been added to `bin/` to fill, index, and query the DB to test changes: + +- `bin/load` invokes `bin/tasks/load.php` +- `bin/index` invokes `bin/tasks/index.php` +- `bin/query` invokes `bin/tasks/query.php` + +To test your DB changes under load: + +#### Load the database + +```bash +docker compose exec tests bin/load --adapter=[adapter] --limit=[limit] [--name=[name]] + +# [adapter]: either 'mongodb' or 'mariadb', no quotes +# [limit]: integer of total documents to generate +# [name]: (optional) name for new database +``` + +#### Create indexes + +```bash +docker compose exec tests bin/index --adapter=[adapter] --name=[name] + +# [adapter]: either 'mongodb' or 'mariadb', no quotes +# [name]: name of filled database by bin/load +``` + +#### Run Query Suite + +```bash +docker compose exec tests bin/query --adapter=[adapter] --limit=[limit] --name=[name] + +# [adapter]: either 'mongodb' or 'mariadb', no quotes +# [limit]: integer of query limit (default 25) +# [name]: name of filled database by bin/load +``` + +#### Visualize Query Results + +```bash +docker compose exec tests bin/compare +``` + +Navigate to `localhost:8708` to visualize query results. + + ## Other Ways to Help Pull requests are great, but there are many other areas where you can help Utopia-php. diff --git a/README.md b/README.md index 07d6e6f25..f9792d101 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ Although this library is part of the [Utopia Framework](https://github.com/utopi ## Getting Started Install using composer: + ```bash composer require utopia-php/database ``` @@ -20,7 +21,7 @@ composer require utopia-php/database A list of the utopia/php concepts and their relevant equivalent using the different adapters - **Database** - An instance of the utopia/database library that abstracts one of the supported adapters and provides a unified API for CRUD operation and queries on a specific schema or isolated scope inside the underlining database. -- **Adapter** - An implementation of an underlying database engine that this library can support - below is a list of [supported adapters](#adapters) and supported capabilities for each Adapter. +- **Adapter** - An implementation of an underlying database engine that this library can support - below is a list of [supported databases](#supported-databases) and supported capabilities for each Database. - **Collection** - A set of documents stored on the same adapter scope. For SQL-based adapters, this will be equivalent to a table. For a No-SQL adapter, this will equivalent to a native collection. - **Document** - A simple JSON object that will be stored in one of the utopia/database collections. For SQL-based adapters, this will be equivalent to a row. For a No-SQL adapter, this will equivalent to a native document. - **Attribute** - A simple document attribute. For SQL-based adapters, this will be equivalent to a column. For a No-SQL adapter, this will equivalent to a native document field. @@ -43,205 +44,814 @@ Attribute filters are functions that manipulate attributes before saving them to The database document interface only supports primitives types (`strings`, `integers`, `floats`, and `booleans`) translated to their native database types for each of the relevant database adapters. Complex types like arrays or objects will be encoded to JSON strings when stored and decoded back when fetched from their adapters. -### Examples +### Supported Databases + +Below is a list of supported databases, and their compatibly tested versions alongside a list of supported features and relevant limits. + +| Adapter | Status | Version | +|---------|---------|---| +| MariaDB | ✅ | 10.5 | +| MySQL | ✅ | 8.0 | +| Postgres | ✅ | 13.0 | +| MongoDB | ✅ | 5.0 | +| SQLlite | ✅ | 3.38 | + +` ✅ - supported ` + +` 🛠 - work in progress` + +### Limitations + +#### MariaDB, MySQL, Postgres, SQLite +- ID max size can be 255 bytes +- ID can only contain [^A-Za-z0-9] and symbols `_` `-` +- Document max size is 65535 bytes +- Collection can have a max of 1017 attributes +- Collection can have a max of 64 indexes +- Index value max size is 768 bytes. Values over 768 bytes are truncated +- String max size is 4294967295 characters +- Integer max size is 4294967295 + +#### MongoDB +- ID max size can be 255 bytes +- ID can only contain [^A-Za-z0-9] and symbols `_` `-` +- Document can have unrestricted size +- Collection can have unrestricted amount of attributes +- Collection can have a max of 64 indexes +- Index value can have unrestricted size +- String max size is 2147483647 characters +- Integer max size is 4294967295 + +## Usage -Some examples to help you get started. +### Connecting to a Database -**Creating a database:** +#### MariaDB ```php require_once __DIR__ . '/vendor/autoload.php'; use PDO; use Utopia\Database\Database; -use Utopia\Database\Adapter\MariaDB; use Utopia\Cache\Cache; -use Utopia\Cache\Adapter\None as NoCache; +use Utopia\Cache\Adapter\Memory; +use Utopia\Database\Adapter\MariaDB; $dbHost = 'mariadb'; $dbPort = '3306'; $dbUser = 'root'; $dbPass = 'password'; - -$pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, [ +$pdoConfig = [ PDO::ATTR_TIMEOUT => 3, // Seconds PDO::ATTR_PERSISTENT => true, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => true, PDO::ATTR_STRINGIFY_FETCHES => true, -]); +]; + +$pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, $pdoConfig); -$cache = new Cache(new NoCache()); // or use any cache adapter you wish +$cache = new Cache(new Memory()); // or use any cache adapter you wish $database = new Database(new MariaDB($pdo), $cache); -$database->setNamespace('mydb'); -$database->create(); // Creates a new schema named `mydb` ``` -**Creating a collection:** +#### MySQL ```php -$database->createCollection('movies'); - -// Add attributes -$database->createAttribute('movies', 'name', Database::VAR_STRING, 128, true); -$database->createAttribute('movies', 'director', Database::VAR_STRING, 128, true); -$database->createAttribute('movies', 'year', Database::VAR_INTEGER, 0, true); -$database->createAttribute('movies', 'price', Database::VAR_FLOAT, 0, true); -$database->createAttribute('movies', 'active', Database::VAR_BOOLEAN, 0, true); -$database->createAttribute('movies', 'genres', Database::VAR_STRING, 32, true, true, true); - -// Create an Index -$database->createIndex('movies', 'index1', Database::INDEX_KEY, ['year'], [128], [Database::ORDER_ASC]); -``` +require_once __DIR__ . '/vendor/autoload.php'; -**Create a document:** +use PDO; +use Utopia\Database\Database; +use Utopia\Cache\Cache; +use Utopia\Cache\Adapter\Memory; +use Utopia\Database\Adapter\MySQL; -```php -static::getDatabase()->createDocument('movies', new Document([ - '$permissions' => [ - Permission::read(Role::any()), - Permission::read(Role::user(ID::custom('1'))), - Permission::read(Role::user(ID::custom('2'))), - Permission::create(Role::any()), - Permission::create(Role::user(ID::custom('1x'))), - Permission::create(Role::user(ID::custom('2x'))), - Permission::update(Role::any()), - Permission::update(Role::user(ID::custom('1x'))), - Permission::update(Role::user(ID::custom('2x'))), - Permission::delete(Role::any()), - Permission::delete(Role::user(ID::custom('1x'))), - Permission::delete(Role::user(ID::custom('2x'))), - ], - 'name' => 'Captain Marvel', - 'director' => 'Anna Boden & Ryan Fleck', - 'year' => 2019, - 'price' => 25.99, - 'active' => true, - 'genres' => ['science fiction', 'action', 'comics'], -])); -``` +$dbHost = 'mysql'; +$dbPort = '3306'; +$dbUser = 'root'; +$dbPass = 'password'; +$pdoConfig = [ + PDO::ATTR_TIMEOUT => 3, // Seconds + PDO::ATTR_PERSISTENT => true, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_EMULATE_PREPARES => true, + PDO::ATTR_STRINGIFY_FETCHES => true, +]; -**Get Document**: +$pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, $pdoConfig); -```php -// Get document with all attributes -$document = static::getDatabase()->getDocument('movies', '1'); +$cache = new Cache(new Memory()); // or use any cache adapter you wish -// Get document with a sub-set of attributes -$document = static::getDatabase()->getDocument('movies', '1', [ - Query::select(['name', 'director', 'year']), -]); +$database = new Database(new MySql($pdo), $cache); ``` -**Find:** +#### Postgres ```php -$documents = static::getDatabase()->find('movies', [ - Query::equal('year', [2019]), -]); -``` +require_once __DIR__ . '/vendor/autoload.php'; -### Adapters +use PDO; +use Utopia\Database\Database; +use Utopia\Cache\Cache; +use Utopia\Cache\Adapter\Memory; +use Utopia\Database\Adapter\Postgres; -Below is a list of supported adapters, and their compatibly tested versions alongside a list of supported features and relevant limits. +$dbHost = 'postgres'; +$dbPort = '5432'; +$dbUser = 'root'; +$dbPass = 'password'; +$pdoConfig = [ + PDO::ATTR_TIMEOUT => 3, // Seconds + PDO::ATTR_PERSISTENT => true, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_EMULATE_PREPARES => true, + PDO::ATTR_STRINGIFY_FETCHES => true, +]; -| Adapter | Status | Version | -|---------|---------|---| -| MariaDB | ✅ | 10.5 | -| MySQL | ✅ | 8.0 | -| Postgres | 🛠 | 13.0 | -| MongoDB | ✅ | 5.0 | -| SQLlite | ✅ | 3.38 | +$pdo = new PDO("pgsql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, $pdoConfig); -` ✅ - supported, 🛠 - work in progress` +$cache = new Cache(new Memory()); // or use any cache adapter you wish -## Limitations (to be completed per adapter) +$database = new Database(new Postgres($pdo), $cache); +``` -- ID max size can be 255 bytes -- ID can only contain [^A-Za-z0-9] and symbols `_` `-` -- Document max size is x bytes -- Collection can have a max of x attributes -- Collection can have a max of x indexes -- Index value max size is x bytes. Values over x bytes are truncated +#### SQLite -## System Requirements +```php +require_once __DIR__ . '/vendor/autoload.php'; -Utopia Framework requires PHP 8.0 or later. We recommend using the latest PHP version whenever possible. +use PDO; +use Utopia\Database\Database; +use Utopia\Cache\Cache; +use Utopia\Cache\Adapter\Memory; +use Utopia\Database\Adapter\SQLite; + +$dbPath = '/path/to/database.sqlite'; +$pdoConfig = [ + PDO::ATTR_TIMEOUT => 3, // Seconds + PDO::ATTR_PERSISTENT => true, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_EMULATE_PREPARES => true, + PDO::ATTR_STRINGIFY_FETCHES => true, +]; -## Tests +$pdo = new PDO("{$dbPath}", $pdoConfig); -To run tests, you first need to bring up the example Docker stack with the following command: +$cache = new Cache(new Memory()); // or use any cache adapter you wish -```bash -docker compose up -d --build +$database = new Database(new SQLite($pdo), $cache); ``` -To run all unit tests, use the following Docker command: +#### MongoDB -```bash -docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml tests +```php +require_once __DIR__ . '/vendor/autoload.php'; + +use Utopia\Database\Database; +use Utopia\Cache\Cache; +use Utopia\Cache\Adapter\Memory; +use Utopia\Database\Adapter\Mongo; +use Utopia\Mongo\Client; // from utopia-php/mongo + +$dbHost = 'mongo'; +$dbPort = 27017; +$dbUser = 'root'; +$dbPass = 'password'; +$dbName = 'dbName'; + +$mongoClient = new Client($dbName, $dbHost, $dbPort, $dbUser, $dbPass, true); + +$cache = new Cache(new Memory()); // or use any cache adapter you wish + +$database = new Database(new Mongo($client), $cache); ``` -To run tests for a single file, use the following Docker command structure: +### Database Methods -```bash -docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml tests/Database/[FILE_PATH] +```php + +// Get namespace +$database->getNamespace(); + +// Sets namespace that prefixes all collection names +$database->setNamespace( + namespace: 'namespace' +); + +// Get default database +$database->getDefaultDatabase(); + +// Sets default database +$database->setDefaultDatabase( + name: 'dbName' +); + +// Creates a new database. +// Uses default database as the name. +$database->create(); + +// Returns an array of all databases +$database->list(); + +// Delete database +$database->delete( + name: 'mydb' +); + +// Ping database it returns true if the database is alive +$database->ping(); + +// Check if database exists +$database->exists( + database: 'mydb' +); + +// Check if collection exists +$database->exists( + database: 'mydb', + collection: 'users' +); + +// Listen to events + +// Event Types +Database::EVENT_ALL +Database::EVENT_DATABASE_CREATE, +Database::EVENT_DATABASE_LIST, +Database::EVENT_COLLECTION_CREATE, +Database::EVENT_COLLECTION_LIST, +Database::EVENT_COLLECTION_READ, +Database::EVENT_ATTRIBUTE_CREATE, +Database::EVENT_ATTRIBUTE_UPDATE, +Database::EVENT_INDEX_CREATE, +Database::EVENT_DOCUMENT_CREATE, +Database::EVENT_DOCUMENT_UPDATE, +Database::EVENT_DOCUMENT_READ, +Database::EVENT_DOCUMENT_FIND, +Database::EVENT_DOCUMENT_FIND, +Database::EVENT_DOCUMENT_COUNT, +Database::EVENT_DOCUMENT_SUM, +Database::EVENT_DOCUMENT_INCREASE, +Database::EVENT_DOCUMENT_DECREASE, +Database::EVENT_INDEX_DELETE, +Database::EVENT_DOCUMENT_DELETE, +Database::EVENT_ATTRIBUTE_DELETE, +Database::EVENT_COLLECTION_DELETE, +Database::EVENT_DATABASE_DELETE, + +$database->on( + Database::EVENT_ALL, + function($event, $data) { + // Do something + } +); + +// Get Database Adapter +$database->getAdapter(); + +// Get List of keywords that cannot be used +$database->getKeywords(); ``` -To run static code analysis, use the following Psalm command: +### Collection Methods -```bash -docker compose exec tests vendor/bin/psalm --show-info=true +```php +// Creates two new collection named '$namespace_$collectionName' with attribute names '_id', '_uid', '_createdAt', '_updatedAt', '_permissions' +// The second collection is named '$namespace_$collectionName_perms' with attribute names '_id', '_type', '_permission', '_document' +$database->createCollection( + name: 'users' +); + +// Create collection with attributes and indexes +$attributes = [ + new Document([ + '$id' => ID::unique(), + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()) + ], + 'name' => 'Jhon', + 'age' => 20 + ]), + new Document([ + '$id' => ID::unique(), + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()) + ], + 'name' => 'Doe', + 'age' => 34 + ]), +] + +$indexes = [ + new Document([ + '$id' => ID::unique(), + 'type' => Database::INDEX_KEY, + 'attributes' => ['name'], + 'lengths' => [256], + 'orders' => ['ASC'], + ]), + new Document([ + '$id' => ID::unique(), + 'type' => Database::INDEX_KEY, + 'attributes' => ['name', 'age'], + 'lengths' => [128, 128], + 'orders' => ['ASC'], + ]) +]; + +$database->createCollection( + name: 'users', + attributes: $attributes, + indexes: $indexes +); + +// Update Collection Permissions +$database->updateCollection( + id: 'users', + permissions: [ + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()) + ], + documentSecurity: true +); + +// Get Collection +$database->getCollection( + id: 'users' +); + +// List Collections +$database->listCollections( + limit: 25, + offset: 0 +); + +// Deletes the two collections named 'namespace_$collectionName' and 'namespace_$collectionName_perms' +$database->deleteCollection( + id: 'users' +); + +// Delete cached documents of a collection +$database->deleteCachedCollection( + collection: 'users' +); ``` -### Load testing -Three commands have been added to `bin/` to fill, index, and query the DB to test changes: +### Attribute Methods -- `bin/load` invokes `bin/tasks/load.php` -- `bin/index` invokes `bin/tasks/index.php` -- `bin/query` invokes `bin/tasks/query.php` +```php +// Data types +Database::VAR_STRING +Database::VAR_INTEGER +Database::VAR_FLOAT +Database::VAR_BOOLEAN +Database::VAR_DATETIME + + +// Creates a new attribute named '$attributeName' in the '$namespace_$collectionName' collection. +$database->createAttribute( + collection: 'movies', + id: 'name', + type: Database::VAR_STRING, + size: 128, + required: true +); + +// New attribute with optional parameters +$database->createAttribute( + collection: 'movies', + id: 'genres', + type: Database::VAR_STRING, + size: 128, + required: true, + default: null, + signed: true, + array: false, + format: null, + formatOptions: [], + filters: [] +); + +// Updates the attribute named '$attributeName' in the '$namespace_$collectionName' collection. +$database-> updateAttribute( + collection: 'movies', + id: 'genres', + type: Database::VAR_STRING, + size: 128, + required: true, + default: null, + signed: true, + array: false, + format: null, + formatOptions: [], + filters: [] +); + +// Update the required status of an attribute +$database->updateAttributeRequired( + collection: 'movies', + id: 'genres', + required: true +); + +// Update the attribute format +$database->updateAttributeFormat( + collection: 'movies', + id: 'genres', + format: null, +); + +// Update the attribute format options +$database->updateAttributeFormatOptions( + collection: 'movies', + id: 'genres', + formatOptions: [] +); + +// Update the attribute filters +$database->updateAttributeFilters( + collection: 'movies', + id: 'genres', + filters: [] +); + +// Update the default value of an attribute +$database->updateAttributeDefault( + collection: 'movies', + id: 'genres', + default: 'sci-fi' +); + +// Check if attribute can be added to a collection +$collection = $database->getCollection('movies'); + +$attribute = new Document([ + '$id' => ID::unique(), + 'type' => Database::VAR_INTEGER, + 'size' => 256, + 'required' => true, + 'default' => null, + 'signed' => true, + 'array' => false, + 'filters' => [], +]); -To test your DB changes under load: +$database->checkAttribute( + collection: $collection, + attribute: $attribute +); + +// Get Adapter attribute limit +$database->getLimitForAttributes(); // if 0 then no limit + +// Get Adapter index limit +$database->getLimitForIndexes(); + +// Renames the attribute from old to new in the '$namespace_$collectionName' collection. +$database->renameAttribute( + collection: 'movies', + old: 'genres', + new: 'genres2' +); + +// Deletes the attribute in the '$namespace_$collectionName' collection. +$database->deleteAttribute( + collection: 'movies', + id: 'genres' +); +``` -#### Load the database +### Index Methods -```bash -docker compose exec tests bin/load --adapter=[adapter] --limit=[limit] [--name=[name]] +```php +// Index types +Database::INDEX_KEY, +Database::INDEX_FULLTEXT +Database::INDEX_UNIQUE +Database::INDEX_SPATIAL +Database::INDEX_ARRAY + +// Insertion Order +Database::ORDER_ASC +Database::ORDER_DESC + + +// Creates a new index named '$indexName' in the '$namespace_$collectionName' collection. +// Note: The size for the index will be taken from the size of the attribute +$database->createIndex( + collection: 'movies', + id: 'index1', Database::INDEX_KEY, + attributes: ['name', 'genres'], + lengths: [128,128], + orders: [Database::ORDER_ASC, Database::ORDER_DESC] +); + +// Rename index from old to new in the '$namespace_$collectionName' collection. +$database->renameIndex( + collection: 'movies', + old: 'index1', + new: 'index2' +); + +// Deletes the index in the '$namespace_$collectionName' collection. +$database->deleteIndex( + collection: 'movies', + id: 'index2' +); +``` + +### Relationship Methods -# [adapter]: either 'mongodb' or 'mariadb', no quotes -# [limit]: integer of total documents to generate -# [name]: (optional) name for new database +```php +// Relationship types +Database::RELATION_ONE_TO_ONE +Database::RELATION_ONE_TO_MANY +Database::RELATION_MANY_TO_ONE +Database::RELATION_MANY_TO_MANY + +// Creates a relationship between the two collections with the default reference attributes +$database->createRelationship( + collection: 'movies', + relatedCollection: 'users', + type: Database::RELATION_ONE_TO_ONE, + twoWay: true +); + + +// Create a relationship with custom reference attributes +$database->createRelationship( + collection: 'movies', + relatedCollection: 'users', + type: Database::RELATION_ONE_TO_ONE, + twoWay: true, + id: 'movies_id', + twoWayKey: 'users_id' +); + +// Relationship onDelete types +Database::RELATION_MUTATE_CASCADE, +Database::RELATION_MUTATE_SET_NULL, +Database::RELATION_MUTATE_RESTRICT, + +// Update the relationship with the default reference attributes +$database->updateRelationship( + collection: 'movies', + id: 'users', + onDelete: Database::RELATION_MUTATE_CASCADE +); + +// Update the relationship with custom reference attributes +$database->updateRelationship( + collection: 'movies', + id: 'users', + onDelete: Database::RELATION_MUTATE_CASCADE, + newKey: 'movies_id', + newTwoWayKey: 'users_id', + twoWay: true +); + +// Delete the relationship with the default or custom reference attributes +$database->deleteRelationship( + collection: 'movies', + id: 'users' +); ``` -#### Create indexes +### Document Methods -```bash -docker compose exec tests bin/index --adapter=[adapter] --name=[name] +```php +use Utopia\Database\Document; +use Utopia\Database\Helpers\ID; +use Utopia\Database\Helpers\Permission; +use Utopia\Database\Helpers\Role; + +// Id helpers +ID::unique(padding: 12) // Creates an id of length 7 + padding +ID::custom(id: 'my_user_3235') + +// Role helpers +Role::any() +Role::guests() +Role::user( + identifier: ID::unique() + status: 'active' //optional +) +Role::users() +Role::team( + identifier: ID::unique() +) +Role::team( + identifier: ID::unique() + dimension: '123' //team:id/dimension +) +Role::label( + identifier: 'admin' +) +Role::members( + identifier: ID::unique() +) + + + +// Permission helpers +Permission::read(Role::any()), +Permission::create(Role::user(ID::unique())), +Permission::update(Role::user(ID::unique(padding: 23))), +Permission::delete(Role::user(ID::custom(id: 'my_user_3235')) + +// To create a document +$document = new Document([ + '$permissions' => [ + Permission::read(Role::any()), + Permission::create(Role::user(ID::custom('1x'))), + Permission::update(Role::user(ID::unique(12))), + Permission::delete(Role::user($customId)), + ], + '$id' => ID::unique(), + 'name' => 'Captain Marvel', + 'director' => 'Anna Boden & Ryan Fleck', + 'year' => 2019, + 'price' => 25.99, + 'active' => true, + 'genres' => ['science fiction', 'action', 'comics'], +]); -# [adapter]: either 'mongodb' or 'mariadb', no quotes -# [name]: name of filled database by bin/load -``` +$document = $database->createDocument( + collection: 'movies', + document: $document +); + +// Get which collection a document belongs to +$document->getCollection(); + +// Get document id +$document->getId(); + +// Check whether document in empty +$document->isEmpty(); + +// Increase an attribute in a document +$database->increaseDocumentAttribute( + collection: 'movies', + id: $document->getId(), + attribute: 'name', + value: 24, + max: 100 +); + +// Decrease an attribute in a document +$database->decreaseDocumentAttribute( + collection: 'movies', + id: $document->getId(), + attribute: 'name', + value: 24, + min: 100 +); + +// Update the value of an attribute in a document + +// Set types +Document::SET_TYPE_ASSIGN, // Assign the new value directly +Document::SET_TYPE_APPEND, // Append the new value to end of the array +Document::SET_TYPE_PREPEND // Prepend the new value to start of the array +Note: Using append/prepend with an attribute which is not an array, it will be set to an array containing the new value. + +$document->setAttribute(key: 'name', 'Chris Smoove') + ->setAttribute(key: 'age', 33, Document::SET_TYPE_ASSIGN); + +$database->updateDocument( + collection: 'users', + id: $document->getId(), + document: $document +); + +// Update the permissions of a document +$document->setAttribute('$permissions', Permission::read(Role::any()), Document::SET_TYPE_APPEND) + ->setAttribute('$permissions', Permission::create(Role::any()), Document::SET_TYPE_APPEND) + ->setAttribute('$permissions', Permission::update(Role::any()), Document::SET_TYPE_APPEND) + ->setAttribute('$permissions', Permission::delete(Role::any()), Document::SET_TYPE_APPEND) + +$database->updateDocument( + collection: 'users', + id: $document->getId(), + document: $document +); + +// Info regarding who has permission to read, create, update and delete a document +$document->getRead(); // returns an array of roles that have permission to read the document +$document->getCreate(); // returns an array of roles that have permission to create the document +$document->getUpdate(); // returns an array of roles that have permission to update the document +$document->getDelete(); // returns an array of roles that have permission to delete the document -#### Run Query Suite +// Get document with all attributes +$database->getDocument( + collection: 'movies', + id: $document->getId() +); -```bash -docker compose exec tests bin/query --adapter=[adapter] --limit=[limit] --name=[name] +// Get document with a sub-set of attributes +$database->getDocument( + collection: 'movies', + id: $document->getId(), + queries: [ + Query::select(['name', 'director', 'year']) + ] +); + +// Find documents + +// Query Types +Query::equal(attribute: "...", values: ["...", "..."]), +Query::notEqual(attribute: "...", value: "..."), +Query::lessThan(attribute: "...", value: 100), +Query::lessThanEqual(attribute: "...", value: 1000), +Query::greaterThan(attribute: "...", value: 1000), +Query::greaterThanEqual(attribute: "...", value: ...), +Query::contains(attribute: "...", values: ["...", "..."]), +Query::between(attribute: "...", start: 100, end: 1000), +Query::search(attribute: "...", value: "..."), +Query::select(attributes: ["...", "..."]), +Query::orderDesc(attribute: "..."), +Query::orderAsc(attribute: "..."), +Query::isNull(attribute: "..."), +Query::isNotNull(attribute: "..."), +Query::startsWith(attribute: "...", value: "..."), +Query::endsWith(attribute: "...", value: "..."), +Query::limit(value: 35), +Query::offset(value: 0), + +$database->find( + collection: 'movies', + queries: [ + Query::equal(attribute: 'name', values: ['Captain Marvel']), + Query::notEqual(attribute: 'year', values: [2019]) + ], + timeout: 1 //timeout is optional +); + +// Find a document +$database->findOne( + collection: 'movies', + queries: [ + Query::equal(attribute: 'name', values: ['Captain Marvel']), + Query::lessThan(attribute: 'year', value: 2019) + ] +); + +// Get count of documents +$database->count( + collection: 'movies', + queries: [ + Query::equal(attribute: 'name', values: ['Captain Marvel']), + Query::greaterThan(attribute: 'year', value: 2019) + ], + max: 1000 // Max is optional +); + +// Get the sum of an attribute from all the documents +$database->sum( + collection: 'movies', + attribute: 'price', + queries: [ + Query::greaterThan(attribute: 'year', value: 2019) + ], + max: null // max = null means no limit +); + +// Delete a document +$database->deleteDocument( + collection: 'movies', + id: $document->getId() +); + +// Delete a cached document +Note: Cached Documents or Collections are automatically deleted when a document or collection is updated or deleted +$database->deleteCachedDocument( + collection: 'movies', + id: $document->getId() +); -# [adapter]: either 'mongodb' or 'mariadb', no quotes -# [limit]: integer of query limit (default 25) -# [name]: name of filled database by bin/load ``` -#### Visualize Query Results +## System Requirements + +Utopia Framework requires PHP 8.0 or later. We recommend using the latest PHP version whenever possible. -```bash -docker compose exec tests bin/compare -``` +## Contributing -Navigate to `localhost:8708` to visualize query results. +Thank you for considering contributing to the Utopia Framework! +Checkout the [CONTRIBUTING.md](https://github.com/utopia-php/database/blob/main/CONTRIBUTING.md) file for more information. -## Copyright and license +## Copyright and License -The MIT License (MIT) [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php) +The MIT License (MIT) [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php) \ No newline at end of file