Merge pull request #2 from utopia-php/shmuel.1

Mongo
This commit is contained in:
Torsten Dittmann authored and GitHub committed 2022-10-13 17:31:32 +02:00
commit d26afa40b5
11 files changed
+2572 -182

No files matched your search

+1 -13
View File
@@ -13,8 +13,7 @@ RUN composer update --ignore-platform-reqs --optimize-autoloader \
FROM php:8.0-cli-alpine as compile
ENV PHP_REDIS_VERSION=5.3.4 \
PHP_SWOOLE_VERSION=v4.8.0 \
ENV PHP_SWOOLE_VERSION=v4.8.0 \
PHP_MONGO_VERSION=1.11.1
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
@@ -25,15 +24,6 @@ RUN \
&& docker-php-ext-install opcache pgsql pdo_mysql pdo_pgsql \
&& rm -rf /var/cache/apk/*
# Redis Extension
FROM compile AS redis
RUN \
git clone --depth 1 --branch $PHP_REDIS_VERSION https://github.com/phpredis/phpredis.git \
&& cd phpredis \
&& phpize \
&& ./configure \
&& make && make install
## Swoole Extension
FROM compile AS swoole
RUN \
@@ -59,7 +49,6 @@ LABEL maintainer="team@appwrite.io"
WORKDIR /usr/src/code
RUN echo extension=redis.so >> /usr/local/etc/php/conf.d/redis.ini
RUN echo extension=swoole.so >> /usr/local/etc/php/conf.d/swoole.ini
RUN echo extension=mongodb.so >> /usr/local/etc/php/conf.d/mongodb.ini
@@ -71,7 +60,6 @@ RUN echo "memory_limit=1024M" >> $PHP_INI_DIR/php.ini
COPY --from=composer /usr/local/src/vendor /usr/src/code/vendor
COPY --from=swoole /usr/local/lib/php/extensions/no-debug-non-zts-20200930/swoole.so /usr/local/lib/php/extensions/no-debug-non-zts-20200930/
COPY --from=redis /usr/local/lib/php/extensions/no-debug-non-zts-20200930/redis.so /usr/local/lib/php/extensions/no-debug-non-zts-20200930/
COPY --from=mongodb /usr/local/lib/php/extensions/no-debug-non-zts-20200930/mongodb.so /usr/local/lib/php/extensions/no-debug-non-zts-20200930/
# Add Source Code
+3 -5
View File
@@ -16,10 +16,10 @@
}
],
"autoload": {
"psr-4": {"Utopia\\Mongo\\": "src/Mongo"}
"psr-4": {"Utopia\\Mongo\\": "src"}
},
"autoload-dev": {
"psr-4": {"Utopia\\Tests\\": "tests/Mongo"}
"psr-4": {"Utopia\\Tests\\": "tests"}
},
"require": {
"php": ">=8.0",
@@ -29,8 +29,6 @@
"require-dev": {
"fakerphp/faker": "^1.14",
"phpunit/phpunit": "^9.4",
"swoole/ide-helper": "4.8.0",
"utopia-php/cli": "^0.11.0",
"vimeo/psalm": "4.0.1"
"swoole/ide-helper": "4.8.0"
}
}
Generated
+2261
View File
File diff suppressed because it is too large. Load diff
+4 -66
View File
@@ -3,7 +3,7 @@ version: '3.1'
services:
tests:
container_name: tests
container_name: utopia-mongo-test-1
build:
context: .
networks:
@@ -11,80 +11,18 @@ services:
volumes:
- ./:/usr/src/code
ports:
- "8708:8708"
- "8882:8882"
adminer:
image: adminer
container_name: utopia-adminer
restart: always
ports:
- "8760:8080"
networks:
- database
postgres:
image: postgres:13
container_name: utopia-postgres
networks:
- database
ports:
- "8700:5432"
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: password
cockroach:
image: cockroachdb/cockroach:v20.2.0
container_name: utopia-cockroach
command: start-single-node --insecure --logtostderr
networks:
- database
ports:
- "8704:26257"
- "8705:8080"
mariadb:
image: mariadb:10.5
container_name: utopia-mariadb
networks:
- database
ports:
- "8701:3306"
environment:
- MYSQL_ROOT_PASSWORD=password
mongo:
image: mongo:4.4
container_name: utopia-mongo
container_name: utopia-mongo-1
networks:
- database
ports:
- "8702:27017"
- "8881:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mysql:
image: mysql:8.0.28-oracle
container_name: utopia-mysql
networks:
- database
ports:
- "8703:3307"
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: default
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_TCP_PORT: 3307
cap_add:
- SYS_NICE
redis:
image: redis:6.0-alpine
container_name: utopia-redis
networks:
- database
networks:
database:
+16
View File
@@ -0,0 +1,16 @@
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
+2 -2
View File
@@ -1,6 +1,6 @@
<?php
namespace Utopia\Database\Adapter\Mongo;
namespace Utopia\Mongo;
use MongoDB\BSON;
@@ -289,4 +289,4 @@ class Auth
{
return \md5(\utf8_encode($username . ':mongo:' . $password));
}
}
}
+6
View File
@@ -0,0 +1,6 @@
<?php
namespace Utopia\Mongo\Exception;
class Duplicate extends \Exception
{
}
+160 -93
View File
@@ -1,14 +1,13 @@
<?php
namespace Utopia\Database\Adapter\Mongo;
namespace Utopia\Mongo;
use Exception;
use MongoDB\BSON;
use Swoole\Client;
use Swoole\Coroutine\Client as CoroutineClient;
use Utopia\Database\Adapter\Mongo\Auth;
use Utopia\Database\Adapter\Mongo\MongoClientOptions;
use Utopia\Database\Document;
use Utopia\Database\Exception\Duplicate as DuplicateException;
use stdClass;
use Utopia\Mongo\Exception\Duplicate;
class MongoClient
{
@@ -53,11 +52,13 @@ class MongoClient
}
/**
* Connect to Mongo using TCP/IP
* Connect to Mongo using TCP/IP
* and Wire Protocol.
*/
public function connect(): MongoClient
{
if($this->client->isConnected()) return $this;
$this->client->connect($this->options->host, $this->options->port);
[$payload, $db] = $this->auth->start();
@@ -73,6 +74,7 @@ class MongoClient
/**
* Send a raw string query to connection.
* @param string $qry
* @return mixed
*/
public function raw_query(string $qry): mixed
{
@@ -81,11 +83,12 @@ class MongoClient
/**
* Send a BSON packed query to connection.
*
*
* @param array $command
* @param string $db
* @param string|null $db
* @return stdClass|array|int
*/
public function query(array $command, ?string $db = null): \stdClass|array|int
public function query(array $command, ?string $db = null): stdClass|array|int
{
$params = array_merge($command, [
'$db' => $db ?? $this->options->name,
@@ -93,14 +96,13 @@ class MongoClient
$sections = BSON\fromPHP($params);
$message = pack('V*', 21 + strlen($sections), $this->id, 0, 2013, 0) . "\0" . $sections;
return $this->send($message);
}
/**
* Send a syncronous command to connection.
*/
public function blocking(string $cmd): \stdClass|array|int
public function blocking(string $cmd): stdClass|array|int
{
$this->client->send($cmd . PHP_EOL);
@@ -117,18 +119,21 @@ class MongoClient
* Send a message to connection.
*
* @param mixed $data
* @return stdClass|array|int
* @throws Exception
*/
public function send(mixed $data): \stdClass|array|int
public function send(mixed $data): stdClass|array|int
{
$this->client->send($data);
return $this->receive();
}
/**
* Receive a message from connection.
* @throws Duplicate
* @throws Exception
*/
private function receive(): \stdClass|array|int
private function receive(): stdClass|array|int
{
$receivedLength = 0;
$responseLength = null;
@@ -153,24 +158,23 @@ class MongoClient
$result = BSON\toPHP(substr($res, 21, $responseLength - 21));
if (property_exists($result, "writeErrors")) {
throw new DuplicateException($result->writeErrors[0]->errmsg);
throw new Duplicate($result->writeErrors[0]->errmsg);
}
if (property_exists($result, "n") && $result->ok == 1) {
if (property_exists($result, "n") && $result->ok === 1.0) {
return $result->n;
}
if (property_exists($result, "nonce") && $result->ok == 1) {
if (property_exists($result, "nonce") && $result->ok === 1.0) {
return $result;
}
if (property_exists($result, 'errmsg')) {
throw new \Exception($result->errmsg);
throw new Exception($result->errmsg);
}
if ($result->ok == 1) {
return $result;
if ($result->ok === 1.0) {
return $result;
}
return $result->cursor->firstBatch;
@@ -178,22 +182,22 @@ class MongoClient
/**
* Selects a collection.
*
*
* Note: Since Mongo creates on the fly, this just returns
* a instances of self.
* an instances of self.
*/
public function selectDatabase(string $name): MongoClient
public function selectDatabase(): MongoClient
{
return $this;
}
/**
* Creates a collection.
*
*
* Note: Since Mongo creates on the fly, this just returns
* a instances of self.
* an instances of self.
*/
public function createDatabase(string $name): MongoClient
public function createDatabase(): MongoClient
{
return $this;
}
@@ -201,7 +205,7 @@ class MongoClient
/**
* Get a list of databases.
*/
public function listDatabaseNames(): \stdClass
public function listDatabaseNames(): stdClass
{
return $this->query([
'listDatabases' => 1,
@@ -212,17 +216,16 @@ class MongoClient
/**
* Drop (remove) a database.
* https://docs.mongodb.com/manual/reference/command/dropDatabase/#mongodb-dbcommand-dbcmd.dropDatabase
*
*
* @param array $options
* @param string $db
* @param string|null $db
* @return bool
*/
public function dropDatabase(array $options = [], ?string $db = null): MongoClient
public function dropDatabase(array $options = [], ?string $db = null): bool
{
$db ??= $this->options->name;
$this->query(array_merge(["dropDatabase" => 1], $options), $db);
return $this;
$res = $this->query(array_merge(["dropDatabase" => 1], $options), $db);
return $res->ok === 1.0;
}
public function selectCollection($name): MongoClient
@@ -233,52 +236,56 @@ class MongoClient
/**
* Create a collection.
* https://docs.mongodb.com/manual/reference/command/create/#mongodb-dbcommand-dbcmd.create
*
*
* @param string $name
* @param array $options
* @return bool
* @throws Duplicate
*/
public function createCollection(string $name, array $options = []): MongoClient
public function createCollection(string $name, array $options = []): bool
{
$list = $this->listCollectionNames(["name" => $name]);
if (\count($list->cursor->firstBatch) > 0) {
return $this;
throw new Duplicate('Collection Exists');
}
$this->query(array_merge([
$res = $this->query(array_merge([
'create' => $name,
], $options));
return $this;
return $res->ok === 1.0;
}
/**
* Drop a collection.
* https://docs.mongodb.com/manual/reference/command/drop/#mongodb-dbcommand-dbcmd.drop
*
*
* @param string $name
* @param array $options
* @return bool
*/
public function dropCollection(string $name, array $options = []): MongoClient
public function dropCollection(string $name, array $options = []): bool
{
$this->query(array_merge([
$res = $this->query(array_merge([
'drop' => $name,
], $options));
return $this;
return $res->ok === 1.0;
}
/**
* List collections (name only).
* https://docs.mongodb.com/manual/reference/command/listCollections/#listcollections
*
*
* @param array $filter
* @param array $options
*
* @return \stdClass
*
* @return stdClass
*/
public function listCollectionNames(array $filter = [], array $options = []): \stdClass
public function listCollectionNames(array $filter = [], array $options = []): stdClass
{
$qry = array_merge(
[
"listCollections" => 1.0,
@@ -295,15 +302,23 @@ class MongoClient
/**
* Create indexes.
* https://docs.mongodb.com/manual/reference/command/createIndexes/#createindexes
*
*
* @param string $collection
* @param array $indexes
* @param array $options
*
*
* @return boolean
*/
public function createIndexes(string $collection, array $indexes, array $options = []): bool
{
foreach ($indexes as $key => $index) {
if (\array_key_exists('unique', $index) && $index['unique'] == true) {
/**
* TODO: Unique Indexes are now sparse indexes, which results into incomplete indexes.
*/
$indexes[$key] = \array_merge($index, ['sparse' => true]);
}
}
$qry = array_merge(
[
@@ -345,16 +360,19 @@ class MongoClient
* https://docs.mongodb.com/manual/reference/command/insert/#mongodb-dbcommand-dbcmd.insert
*
* @param string $collection
* @param array $documents
* @param array $document
* @param array $options
*
* @return Document
* @return array
* @throws Exception
*/
public function insert(string $collection, array $documents, array $options = []): Document
public function insert(string $collection, array $document, array $options = []): array
{
$docObj = new \stdClass();
$docObj = new stdClass();
foreach ($document as $key => $value) {
if(\is_null($value)) continue;
foreach ($documents as $key => $value) {
$docObj->{$key} = $value;
}
@@ -363,7 +381,25 @@ class MongoClient
'documents' => [$docObj],
], $options));
return new Document($this->toArray($docObj));
return $this->lastInsertedDocument($collection);
}
/**
* Retreive the last inserted document.
*
* @param string $collection
*
* @return array
*/
public function lastInsertedDocument(string $collection): array
{
$result = $this->find($collection, [], [
'sort' => ['_id' => -1],
'limit' => 1
])->cursor->firstBatch[0];
return $this->toArray($result);
}
/**
@@ -380,13 +416,20 @@ class MongoClient
public function update(string $collection, array $where = [], array $updates = [], array $options = []): MongoClient
{
$cleanUpdates = [];
foreach($updates as $k => $v) {
if(\is_null($v)) continue;
$cleanUpdates[$k] = $v;
}
$this->query(
array_merge([
MongoCommand::UPDATE => $collection,
'updates' => [
[
'q' => $this->toObject($where),
'u' => $this->toObject($updates),
'u' => $this->toObject($cleanUpdates),
'multi' => false,
'upsert' => false
]
@@ -400,17 +443,25 @@ class MongoClient
/**
* Insert, or update, a document/s.
* https://docs.mongodb.com/manual/reference/command/update/#syntax
*
*
* @param string $collection
* @param array $where
* @param array $updates
* @param array $options
*
*
* @return MongoClient
*/
public function upsert(string $collection, array $where = [], array $updates = [], array $options = []): MongoClient
{
$cleanUpdates = [];
foreach($updates as $k => $v) {
if(\is_null($v)) continue;
$cleanUpdates[$k] = $v;
}
$this->query(
array_merge(
[
@@ -418,7 +469,7 @@ class MongoClient
'updates' => [
[
'q' => ['_uid' => $where['_uid']],
'u' => ['$set' => $updates],
'u' => ['$set' => $cleanUpdates],
]
],
],
@@ -432,16 +483,15 @@ class MongoClient
/**
* Find a document/s.
* https://docs.mongodb.com/manual/reference/command/find/#mongodb-dbcommand-dbcmd.find
*
*
* @param string $collection
* @param array $filters
* @param array $options
*
* @return \stdClass
*
* @return stdClass
*/
public function find(string $collection, array $filters = [], array $options = []): \stdClass
public function find(string $collection, array $filters = [], array $options = []): stdClass
{
$result = $this->query(
array_merge([
MongoCommand::FIND => $collection,
@@ -450,23 +500,24 @@ class MongoClient
);
return $result;
}
/**
* Find and modify document/s.
* https://docs.mongodb.com/manual/reference/command/findAndModify/#mongodb-dbcommand-dbcmd.findAndModify
*
*
* @param string $collection
* @param array $update
* @param boolean $remove
* @param array $filters
* @param array $options
*
*
* @return array
*/
public function findAndModify(string $collection, array $update, bool $remove = false, array $filters = [], array $options = []): \stdClass
public function findAndModify(string $collection, array $update, bool $remove = false, array $filters = [], array $options = []): stdClass
{
return $this->query(
$result = $this->query(
array_merge([
MongoCommand::FIND_AND_MODIFY => $collection,
'filter' => $this->toObject($filters),
@@ -474,18 +525,39 @@ class MongoClient
'update' => $update,
], $options)
);
return $result;
}
/**
* Use in conjunction with commands that return a cursor.
* https://www.mongodb.com/docs/v5.0/reference/command/getMore/#getmore
*
* @param int $cursorId
* @param string $collection
* @param int batchSize
*
* @return stdClass
*/
public function getMore(int $cursorId, string $collection, int $batchSize = 25): stdClass
{
return $this->query([
MongoCommand::GET_MORE => $cursorId,
'collection' => $collection,
'batchSize' => $batchSize
]);
}
/**
* Delete a document/s.
* https://docs.mongodb.com/manual/reference/command/delete/#mongodb-dbcommand-dbcmd.delete
*
*
* @param string $collection
* @param array $filters
* @param int $limit
* @param array $deleteOptions
* @param array $options
*
*
* @return int
*/
public function delete(string $collection, array $filters = [], int $limit = 1, array $deleteOptions = [], array $options = []): int
@@ -513,11 +585,11 @@ class MongoClient
/**
* Count documents.
*
*
* @param string $collection
* @param array $filters
* @param array $options
*
*
* @return int
*/
public function count(string $collection, array $filters, array $options): int
@@ -530,13 +602,13 @@ class MongoClient
/**
* Aggregate a collection pipeline.
*
*
* @param string $collection
* @param array $pipeline
*
* @return \stdClass
*
* @return stdClass
*/
public function aggregate(string $collection, array $pipeline): \stdClass
public function aggregate(string $collection, array $pipeline): stdClass
{
$result = $this->query([
MongoCommand::AGGREGATE => $collection,
@@ -549,24 +621,17 @@ class MongoClient
/**
* Convert an assoc array to an object (stdClass).
*
*
* @param array $dict
*
*
* @return stdClass
*/
public function toObject(array $dict): \stdClass
public function toObject(array $dict): stdClass
{
$obj = new \stdClass();
$obj = new stdClass();
foreach ($dict as $k => $v) {
$key = $k == 'id' ? '_id' : $k;
$val = $v;
if ($k == '_id') {
$val = new \MongoDB\BSON\ObjectId($val);
}
$obj->{$key} = $val;
$obj->{$k} = $v;
}
return $obj;
@@ -575,12 +640,14 @@ class MongoClient
/**
* Convert an object (stdClass) to an assoc array.
*
* @param string $obj
* @param stdClass|array|string|null $obj
*
* @return array
* @return array|null
*/
public function toArray(\stdClass|array|string $obj): array
public function toArray(stdClass|array|string|null $obj): array|null
{
if(\is_null($obj)) return null;
if (is_object($obj) || is_array($obj)) {
$ret = (array) $obj;
foreach ($ret as $item) {
@@ -592,4 +659,4 @@ class MongoClient
return [$obj];
}
}
}
+2 -2
View File
@@ -1,6 +1,6 @@
<?php
namespace Utopia\Database\Adapter\Mongo;
namespace Utopia\Mongo;
class MongoClientOptions
{
@@ -16,4 +16,4 @@ class MongoClientOptions
public string $password
) {
}
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
<?php
namespace Utopia\Database\Adapter\Mongo;
namespace Utopia\Mongo;
class MongoCommand
{
+116
View File
@@ -0,0 +1,116 @@
<?php
namespace Utopia\Tests;
use MongoDB\BSON\ObjectId;
use PHPUnit\Framework\TestCase;
use Utopia\Mongo\Exception\Duplicate;
use Utopia\Mongo\MongoClient;
use Utopia\Mongo\MongoClientOptions;
class MongoTest extends TestCase
{
static ?MongoClient $db = null;
static function getDatabase(): MongoClient
{
if (!is_null(self::$db)) {
return self::$db;
}
$options = new MongoClientOptions(
'utopia_testing',
'mongo',
27017,
'root',
'example'
);
$client = new MongoClient($options, false);
$client->connect();
self::$db = $client;
return self::$db;
}
public function testDeleteDatabase()
{
self::assertTrue($this->getDatabase()->dropDatabase([]));
}
/**
* @throws Duplicate
*/
public function testCreateCollection()
{
self::assertTrue($this->getDatabase()->createCollection('movies'));
self::assertTrue($this->getDatabase()->dropCollection('movies'));
self::assertTrue($this->getDatabase()->createCollection('movies'));
self::expectException(Duplicate::class);
self::assertTrue($this->getDatabase()->createCollection('movies'));
}
public function testListDatabases()
{
self::assertCount(4, $this->getDatabase()->listDatabaseNames()->databases);
}
public function testCreateDocument()
{
$doc = $this->getDatabase()->insert('movies', [
'name' => 'Armageddon',
'country' => 'USA',
'language' => 'English'
]
);
$id = (string)$doc['_id'];
self::assertEquals(24, strlen($id));
$doc = $this->getDatabase()->find('movies', ['name' => 'Armageddon'])->cursor->firstBatch ?? [];
self::assertCount(1, $doc);
$doc = $this->getDatabase()->find('movies', ['_id' => new ObjectId($id)])->cursor->firstBatch ?? [];
self::assertCount(1, $doc);
$doc = $this->getDatabase()->insert('movies', ['9 Monkeys']);
$id = (string)$doc['_id'];
self::assertEquals(24, strlen($id));
$doc = $this->getDatabase()->insert('movies', [
'name' => 300,
'country' => 'USA',
'language' => 'English'
]
);
$id = 999;
$doc = $this->getDatabase()->insert('movies', [
'_id' => $id,
'array' => ['USA', 'UK', 'India'],
'language' => 'English',
'float' => 9.9,
'integer' => 9,
'is_open' => true,
'date_string' => (new \DateTime())->format('Y-m-d\TH:i:s.vP'),
'date_object' => new \DateTime()
]
);
$doc = $this->getDatabase()->find('movies', ['_id' => $id])->cursor->firstBatch ?? [];
$doc = $doc[0];
self::assertTrue($doc->is_open);
self::assertIsFloat($doc->float);
self::assertIsInt($doc->integer);
self::assertIsArray($doc->array);
self::assertIsString($doc->date_string);
self::assertIsObject($doc->date_object); // Todo: This is not working can't retrieve the object back
}
}