Git Product home page Git Product logo

database's Introduction

Utopia Database

Build Status Total Downloads Discord

Utopia framework database library is simple and lite library for managing application persistency using multiple database adapters. This library is aiming to be as simple and easy to learn and use. This library is maintained by the Appwrite team.

Although this library is part of the Utopia Framework project it is dependency free, and can be used as standalone with any other PHP project or framework.

Getting Started

Install using composer:

composer require utopia-php/database

Initialization:

<?php

require_once __DIR__ . '/../../vendor/autoload.php';

Concepts

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 and supported capabilities for each Adapter.
  • 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.
  • Index - A simple collection index used to improve the performance of your database queries.
  • Permissions - Using permissions, you can decide which roles will grant read or write access for a specific document. The special attributes $read and $write are used to store permissions metadata for each document in the collection. A permission role can be any string you want. You can use Authorization::setRole() to delegate new roles to your users, once obtained a new role a user would gain read or write access to a relevant document.

Reserved Attributes

  • $id - the documnet unique ID, you can set your own custom ID or a random UID will be generated by the library.
  • $collection - an attribute containing the name of the collection the document is stored in.
  • $read - an attribute containing an array of strings. Each string represent a specific role. If your user obtains that role he will have read access for this document.
  • $write - an attribute containing an array of strings. Each string represent a specific role. If your user obtains that role he will have write access for this document.

Attribute Types

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

Some examples to help you get started.

Creating a database:

use PDO;
use Utopia\Database\Database;
use Utopia\Database\Adapter\MariaDB;
use Utopia\Cache\Cache;
use Utopia\Cache\Adapter\None as NoCache;

$dbHost = 'mariadb';
$dbPort = '3306';
$dbUser = 'root';
$dbPass = 'password';

$pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, [
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4',
    PDO::ATTR_TIMEOUT => 3, // Seconds
    PDO::ATTR_PERSISTENT => true,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$cache = new Cache(new NoCache()); // 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:

$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', 'generes', Database::VAR_STRING, 32, true, true, true);

// Create an Index
$database->createIndex('movies', 'index1', Database::INDEX_KEY, ['year'], [128], [Database::ORDER_ASC]);

Create a document:

static::getDatabase()->createDocument('movies', new Document([
    '$read' => ['role:all', 'user1', 'user2'],
    '$write' => ['role:all', 'user1x', 'user2x'],
    'name' => 'Captain Marvel',
    'director' => 'Anna Boden & Ryan Fleck',
    'year' => 2019,
    'price' => 25.99,
    'active' => true,
    'generes' => ['science fiction', 'action', 'comics'],
]));

Find:

$documents = static::getDatabase()->find('movies', [
    new Query('year', Query::TYPE_EQUAL, [2019]),
]);

Adapters

Below is a list of supported adapters, and thier 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 โœ… 3.6
SQLlite ๐Ÿ›  3.35

โœ… - supported, ๐Ÿ›  - work in progress

TODOS

  • CRUD: Updated databases list method
  • CRUD: Validate original document before editing $id
  • CRUD: Test no one can overwrite exciting documents/collections without permission
  • FIND: Test for find timeout limits
  • FIND: Add a query validator (Limit queries to indexed attaributes only?)
  • FIND: Add support for more operators (search/match/like)
  • TEST: Missing Collection, DocumentId validators tests
  • TEST: Validate row size is not larger than allowed by adapter (MySQL/MariaDB ~16k)
  • TEST: Add test for creation of a unique index

Open Issues

  • In queries for arrays, should we create a dedicated index?

Limitations (to be completed per adapter)

  • ID max size can be 255 bytes
  • ID can only contain [^A-Za-z0-9]
  • 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

System Requirements

Utopia Framework requires PHP 7.3 or later. We recommend using the latest PHP version whenever possible.

Tests

To run all unit tests, use the following Docker command:

docker-compose exec tests vendor/bin/phpunit --configuration phpunit.xml tests

To run static code analysis, use the following Psalm command:

docker-compose exec tests vendor/bin/psalm --show-info=true

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

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

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

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

docker-compose exec tests bin/compare

Navigate to localhost:8708 to visualize query results.

Authors

Eldad Fux

Brandon Leckemby

Copyright and license

The MIT License (MIT) http://www.opensource.org/licenses/mit-license.php

database's People

Contributors

algyjr avatar eldadfux avatar kodumbeats avatar ln11211 avatar lohanidamodar avatar sin-sumit avatar torstendittmann avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.