Skip to content

Commit a38ca8f

Browse files
authored
Merge pull request #2 from utopia-php/v0
Initial Commit
2 parents eba15e1 + d54bbae commit a38ca8f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+10885
-1
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/vendor/
2+
/.idea/
3+
.DS_Store
4+
mock.json
5+
data-tests.php
6+
loader.php
7+
.phpunit.result.cache

.travis.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
dist: xenial
2+
3+
arch:
4+
- amd64
5+
6+
os: linux
7+
8+
language: shell
9+
10+
services:
11+
- docker
12+
13+
notifications:
14+
email:
15+
- team@appwrite.io
16+
17+
before_script: docker run --rm --interactive --tty --volume "$(pwd)":/app composer update --ignore-platform-reqs --optimize-autoloader --no-plugins --no-scripts --prefer-dist
18+
19+
before_install:
20+
- >
21+
if [ ! -z "${DOCKERHUB_PULL_USERNAME:-}" ]; then
22+
echo "${DOCKERHUB_PULL_PASSWORD}" | docker login --username "${DOCKERHUB_PULL_USERNAME}" --password-stdin
23+
fi
24+
25+
install:
26+
- docker-compose up -d
27+
- sleep 10
28+
29+
script:
30+
- docker ps
31+
- docker-compose exec tests vendor/bin/phpunit --configuration phpunit.xml tests
32+
- docker-compose exec tests vendor/bin/psalm --show-info=true

Dockerfile

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
FROM composer:2.0 as step0
2+
3+
ARG TESTING=false
4+
ENV TESTING=$TESTING
5+
6+
WORKDIR /usr/local/src/
7+
8+
COPY composer.lock /usr/local/src/
9+
COPY composer.json /usr/local/src/
10+
11+
RUN composer update --ignore-platform-reqs --optimize-autoloader \
12+
--no-plugins --no-scripts --prefer-dist
13+
14+
FROM php:7.4-cli-alpine as final
15+
16+
LABEL maintainer="team@appwrite.io"
17+
18+
ENV PHP_SWOOLE_VERSION=v4.6.6
19+
20+
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
21+
22+
RUN \
23+
apk update \
24+
&& apk add --no-cache postgresql-libs postgresql-dev make automake autoconf gcc g++ git brotli-dev \
25+
&& pecl install mongodb redis \
26+
&& docker-php-ext-enable mongodb redis \
27+
&& docker-php-ext-install opcache pgsql pdo_mysql pdo_pgsql \
28+
## Swoole Extension
29+
&& git clone --depth 1 --branch $PHP_SWOOLE_VERSION https://github.com/swoole/swoole-src.git \
30+
&& cd swoole-src \
31+
&& phpize \
32+
&& ./configure --enable-http2 \
33+
&& make && make install \
34+
&& cd .. \
35+
&& rm -rf /var/cache/apk/*
36+
37+
WORKDIR /usr/src/code
38+
39+
RUN echo extension=swoole.so >> /usr/local/etc/php/conf.d/swoole.ini
40+
41+
COPY --from=step0 /usr/local/src/vendor /usr/src/code/vendor
42+
43+
# Add Source Code
44+
COPY . /usr/src/code
45+
46+
CMD [ "tail", "-f", "/dev/null" ]

README.md

Lines changed: 195 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,195 @@
1-
# database
1+
# Utopia Database
2+
3+
[![Build Status](https://travis-ci.org/utopia-php/abuse.svg?branch=master)](https://travis-ci.com/utopia-php/database)
4+
![Total Downloads](https://img.shields.io/packagist/dt/utopia-php/database.svg)
5+
[![Discord](https://img.shields.io/discord/564160730845151244)](https://appwrite.io/discord)
6+
7+
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](https://appwrite.io).
8+
9+
Although this library is part of the [Utopia Framework](https://github.com/utopia-php/framework) project it is dependency free, and can be used as standalone with any other PHP project or framework.
10+
11+
## Getting Started
12+
13+
Install using composer:
14+
```bash
15+
composer require utopia-php/database
16+
```
17+
18+
Initialization:
19+
20+
```php
21+
<?php
22+
23+
require_once __DIR__ . '/../../vendor/autoload.php';
24+
25+
```
26+
27+
### Concepts
28+
29+
A list of the utopia/php concepts and their relevant equivalent using the different adapters
30+
31+
- **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.
32+
- **Adapter** - An implementation of an underlying database engine that this library can support - below is a list of [supported adapters](#supported-adapters) and supported capabilities for each Adapter.
33+
- **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.
34+
- **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.
35+
- **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.
36+
- **Index** - A simple collection index used to improve the performance of your database queries.
37+
- **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.
38+
39+
### Reserved Attributes
40+
41+
- `$id` - the documnet unique ID, you can set your own custom ID or a random UID will be generated by the library.
42+
- `$collection` - an attribute containing the name of the collection the document is stored in.
43+
- `$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.
44+
- `$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.
45+
46+
### Attribute Types
47+
48+
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.
49+
50+
### Examples
51+
52+
Some examples to help you get started.
53+
54+
**Creating a database:**
55+
56+
```php
57+
use PDO;
58+
use Utopia\Database\Database;
59+
use Utopia\Database\Adapter\MariaDB;
60+
use Utopia\Cache\Cache;
61+
use Utopia\Cache\Adapter\None as NoCache;
62+
63+
$dbHost = 'mariadb';
64+
$dbPort = '3306';
65+
$dbUser = 'root';
66+
$dbPass = 'password';
67+
68+
$pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, [
69+
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4',
70+
PDO::ATTR_TIMEOUT => 3, // Seconds
71+
PDO::ATTR_PERSISTENT => true,
72+
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
73+
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
74+
]);
75+
76+
$cache = new Cache(new NoCache()); // or use any cache adapter you wish
77+
78+
$database = new Database(new MariaDB($pdo), $cache);
79+
$database->setNamespace('mydb');
80+
81+
$database->create(); // Creates a new schema named `mydb`
82+
```
83+
84+
**Creating a collection:**
85+
86+
```php
87+
$database->createCollection('movies');
88+
89+
// Add attributes
90+
$database->createAttribute('movies', 'name', Database::VAR_STRING, 128, true);
91+
$database->createAttribute('movies', 'director', Database::VAR_STRING, 128, true);
92+
$database->createAttribute('movies', 'year', Database::VAR_INTEGER, 0, true);
93+
$database->createAttribute('movies', 'price', Database::VAR_FLOAT, 0, true);
94+
$database->createAttribute('movies', 'active', Database::VAR_BOOLEAN, 0, true);
95+
$database->createAttribute('movies', 'generes', Database::VAR_STRING, 32, true, true, true);
96+
97+
// Create an Index
98+
$database->createIndex('movies', 'index1', Database::INDEX_KEY, ['year'], [128], [Database::ORDER_ASC]);
99+
```
100+
101+
**Create a document:**
102+
103+
```php
104+
static::getDatabase()->createDocument('movies', new Document([
105+
'$read' => ['*', 'user1', 'user2'],
106+
'$write' => ['*', 'user1x', 'user2x'],
107+
'name' => 'Captain Marvel',
108+
'director' => 'Anna Boden & Ryan Fleck',
109+
'year' => 2019,
110+
'price' => 25.99,
111+
'active' => true,
112+
'generes' => ['science fiction', 'action', 'comics'],
113+
]));
114+
```
115+
116+
**Find:**
117+
118+
```php
119+
$documents = static::getDatabase()->find('movies', [
120+
new Query('year', Query::TYPE_EQUAL, [2019]),
121+
]);
122+
```
123+
124+
### Adapters
125+
126+
Below is a list of supported adapters, and thier compatibly tested versions alongside a list of supported features and relevant limits.
127+
128+
| Adapter | Status | Version |
129+
|---------|---------|---|
130+
| MariaDB || 10.5 |
131+
| MySQL || 8.0 |
132+
| Postgres | 🛠 | 13.0 |
133+
| MongoDB | 🛠 | 3.6 |
134+
| SQLlite | 🛠 | 3.35 |
135+
136+
` ✅ - supported, 🛠 - work in progress`
137+
138+
## TODOS
139+
140+
- [ ] CRUD: Updated databases list method
141+
- [ ] CRUD: Validate original document before editing `$id`
142+
- [ ] CRUD: Test no one can overwrite exciting documents/collections without permission
143+
- [ ] FIND: Test for find timeout limits
144+
- [ ] FIND: Add a query validator (Limit queries to indexed attaributes only?)
145+
- [ ] FIND: Add support for more operators (search/match/like)
146+
- [ ] TEST: Missing Collection, DocumentId validators tests
147+
- [ ] TEST: Validate row size is not larger than allowed by adapter (MySQL/MariaDB ~16k)
148+
- [ ] TEST: Add test for creation of a unique index
149+
150+
## Open Issues
151+
152+
- Lazy index creation, maybe add a queue attribute to populate before creating the index?
153+
- In queries for arrays, should we create a dedicated index?
154+
155+
## Limitations (to be completed per adapter)
156+
157+
- ID max size can be 255 bytes
158+
- ID can only contain [^A-Za-z0-9]
159+
- Document max size is x bytes
160+
- Collection can have a max of x attributes
161+
- Collection can have a max of x indexes
162+
- Index value max size is x bytes. Values over x bytes are truncated
163+
164+
## System Requirements
165+
166+
Utopia Framework requires PHP 7.3 or later. We recommend using the latest PHP version whenever possible.
167+
168+
## Tests
169+
170+
To run all unit tests, use the following Docker command:
171+
172+
```bash
173+
docker-compose exec tests vendor/bin/phpunit --configuration phpunit.xml tests
174+
```
175+
176+
To run static code analysis, use the following Psalm command:
177+
178+
```bash
179+
docker-compose exec tests vendor/bin/psalm --show-info=true
180+
```
181+
## Authors
182+
183+
**Eldad Fux**
184+
185+
+ [https://twitter.com/eldadfux](https://twitter.com/eldadfux)
186+
+ [https://github.com/eldadfux](https://github.com/eldadfux)
187+
188+
**Brandon Leckemby**
189+
190+
+ [https://github.com/kodumbeats](https://github.com/kodumbeats)
191+
+ [blog.leckemby.me](blog.leckemby.me)
192+
193+
## Copyright and license
194+
195+
The MIT License (MIT) [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php)

0 commit comments

Comments
 (0)