mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
sync against feat-mongo-2
This commit is contained in:
1 parent
ecb7ec8082
commit
3c44d99baf
3 files changed
+40
-54
No files matched your search
+1
-1
@@ -30,7 +30,7 @@
|
||||
"require": {
|
||||
"php": ">=8.0",
|
||||
"ext-mongodb": "*",
|
||||
"mongodb/mongodb": "^1.21"
|
||||
"mongodb/mongodb": "2.1.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"fakerphp/faker": "^1.14",
|
||||
|
||||
+35
-50
@@ -6,7 +6,6 @@ use MongoDB\BSON;
|
||||
use Swoole\Client as SwooleClient;
|
||||
use Swoole\Coroutine\Client as CoroutineClient;
|
||||
use stdClass;
|
||||
use MongoDB\BSON\Int64;
|
||||
|
||||
class Client
|
||||
{
|
||||
@@ -144,7 +143,6 @@ class Client
|
||||
|
||||
$sections = BSON\fromPHP($params);
|
||||
$message = pack('V*', 21 + strlen($sections), $this->id, 0, 2013, 0) . "\0" . $sections;
|
||||
|
||||
return $this->send($message);
|
||||
}
|
||||
|
||||
@@ -231,6 +229,7 @@ class Client
|
||||
if (property_exists($result, "nonce") && $result->ok === 1.0) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
if ($result->ok === 1.0) {
|
||||
return $result;
|
||||
}
|
||||
@@ -520,7 +519,6 @@ class Client
|
||||
}
|
||||
$cleanUpdates[$k] = $v;
|
||||
}
|
||||
|
||||
|
||||
$this->query(
|
||||
array_merge([
|
||||
@@ -540,18 +538,20 @@ class Client
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform multiple upserts in a single update command (wire protocol batch).
|
||||
* Each operation should have 'filter' and 'update' keys.
|
||||
* Insert, or update, document(s) with support for bulk operations.
|
||||
* https://docs.mongodb.com/manual/reference/command/update/#syntax
|
||||
*
|
||||
* @param string $collection
|
||||
* @param array $operations
|
||||
* @param array $operations Array of operations, each with 'filter' and 'update' keys
|
||||
* @param array $options
|
||||
*
|
||||
* @return self
|
||||
* @throws Exception
|
||||
*/
|
||||
public function bulkUpsert(string $collection, array $operations, array $options = []): self
|
||||
public function upsert(string $collection, array $operations, array $options = []): self
|
||||
{
|
||||
$updates = [];
|
||||
|
||||
foreach ($operations as $op) {
|
||||
$cleanUpdate = [];
|
||||
foreach ($op['update'] as $k => $v) {
|
||||
@@ -560,12 +560,16 @@ class Client
|
||||
}
|
||||
}
|
||||
|
||||
$updates[] = [
|
||||
$updateOperation = [
|
||||
'q' => $op['filter'],
|
||||
'u' => $cleanUpdate,
|
||||
'upsert' => true,
|
||||
'multi' => isset($op['multi']) ? $op['multi'] : false,
|
||||
];
|
||||
|
||||
$updates[] = $updateOperation;
|
||||
}
|
||||
|
||||
$this->query(
|
||||
array_merge(
|
||||
[
|
||||
@@ -578,48 +582,7 @@ class Client
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 Client
|
||||
* @throws Exception
|
||||
*/
|
||||
|
||||
public function upsert(string $collection, array $where = [], array $updates = [], array $options = []): self
|
||||
{
|
||||
$cleanUpdates = [];
|
||||
|
||||
foreach ($updates as $k => $v) {
|
||||
if (\is_null($v)) {
|
||||
continue;
|
||||
}
|
||||
$cleanUpdates[$k] = $v;
|
||||
}
|
||||
|
||||
|
||||
$this->query(
|
||||
array_merge(
|
||||
[
|
||||
'update' => $collection,
|
||||
'updates' => [
|
||||
[
|
||||
'q' => ['_uid' => $where['_uid']],
|
||||
'u' => ['$set' => $cleanUpdates],
|
||||
]
|
||||
],
|
||||
],
|
||||
$options
|
||||
)
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a document/s.
|
||||
@@ -924,4 +887,26 @@ class Client
|
||||
return $cleanedFilters;
|
||||
}
|
||||
|
||||
}
|
||||
private ?bool $replicaSet = null;
|
||||
|
||||
/**
|
||||
* Check if MongoDB is running as a replica set.
|
||||
*
|
||||
* @return bool True if this is a replica set, false if standalone
|
||||
* @throws Exception
|
||||
*/
|
||||
public function isReplicaSet(): bool
|
||||
{
|
||||
if ($this->replicaSet !== null) {
|
||||
return $this->replicaSet;
|
||||
}
|
||||
|
||||
$result = $this->query([
|
||||
'isMaster' => 1,
|
||||
], 'admin');
|
||||
|
||||
$this->replicaSet = property_exists($result, 'setName');
|
||||
return $this->replicaSet;
|
||||
}
|
||||
|
||||
}
|
||||
+4
-3
@@ -110,6 +110,7 @@ class MongoTest extends TestCase
|
||||
|
||||
public function testCreateDocuments(): array
|
||||
{
|
||||
|
||||
$docs = $this->getDatabase()->insertMany(
|
||||
'movies',
|
||||
[
|
||||
@@ -226,7 +227,7 @@ class MongoTest extends TestCase
|
||||
}
|
||||
|
||||
|
||||
public function testBulkUpsert()
|
||||
public function testUpsert()
|
||||
{
|
||||
$this->getDatabase()->insert(
|
||||
'movies_upsert',
|
||||
@@ -238,7 +239,7 @@ class MongoTest extends TestCase
|
||||
]
|
||||
);
|
||||
|
||||
$this->getDatabase()->bulkUpsert('movies_upsert', [
|
||||
$this->getDatabase()->upsert('movies_upsert', [
|
||||
[
|
||||
'filter' => ['name' => 'Gone with the wind'],
|
||||
'update' => [
|
||||
@@ -254,7 +255,6 @@ class MongoTest extends TestCase
|
||||
],
|
||||
]);
|
||||
|
||||
|
||||
$documents = $this->getDatabase()->find('movies_upsert')->cursor->firstBatch ?? [];
|
||||
self::assertCount(2, $documents);
|
||||
self::assertEquals(4, $documents[0]->counter);
|
||||
@@ -262,4 +262,5 @@ class MongoTest extends TestCase
|
||||
self::assertEquals('USA', $documents[1]->country);
|
||||
self::assertEquals('English', $documents[1]->language);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user