bulk upsert support

This commit is contained in:
ArnabChatterjee20k
2025-09-03 19:36:24 +05:30
parent 0c139721e4
commit a2e9ee73dc
3 changed files with 71 additions and 1 deletions
@@ -2,6 +2,7 @@
namespace Appwrite\Platform\Modules\Databases\Http\Databases\Collections\Documents\Bulk;
use Appwrite\Event\Event;
use Appwrite\Event\StatsUsage;
use Appwrite\Extend\Exception;
use Appwrite\Platform\Modules\Databases\Http\Databases\Collections\Documents\Action;
@@ -74,11 +75,15 @@ class Upsert extends Action
->inject('response')
->inject('dbForProject')
->inject('queueForStatsUsage')
->inject('queueForEvents')
->inject('queueForRealtime')
->inject('queueForFunctions')
->inject('queueForWebhooks')
->inject('plan')
->callback($this->action(...));
}
public function action(string $databaseId, string $collectionId, array $documents, UtopiaResponse $response, Database $dbForProject, StatsUsage $queueForStatsUsage, array $plan): void
public function action(string $databaseId, string $collectionId, array $documents, UtopiaResponse $response, Database $dbForProject, StatsUsage $queueForStatsUsage, Event $queueForEvents, Event $queueForRealtime, Event $queueForFunctions, Event $queueForWebhooks, array $plan): void
{
$database = $dbForProject->getDocument('databases', $databaseId);
if ($database->isEmpty()) {
@@ -141,5 +146,16 @@ class Upsert extends Action
'total' => $modified,
$this->getSdkGroup() => $upserted
]), $this->getResponseModel());
$this->triggerBulk(
'databases.[databaseId].collections.[collectionId].documents.[documentId].upsert',
$database,
$collection,
$documents,
$queueForEvents,
$queueForRealtime,
$queueForFunctions,
$queueForWebhooks
);
}
}
@@ -61,6 +61,10 @@ class Upsert extends DocumentsUpsert
->inject('response')
->inject('dbForProject')
->inject('queueForStatsUsage')
->inject('queueForEvents')
->inject('queueForRealtime')
->inject('queueForFunctions')
->inject('queueForWebhooks')
->inject('plan')
->callback($this->action(...));
}
@@ -1188,6 +1188,56 @@ class RealtimeCustomClientTest extends Scope
$this->assertArrayHasKey('$permissions', $response['data']['payload']);
$this->assertIsArray($response['data']['payload']['$permissions']);
// bulk upsert
$this->client->call(Client::METHOD_PUT, "/databases/{$databaseId}/collections/{$actorsId}/documents", array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'documents' => [
[
'$id' => ID::unique(),
'name' => 'Robert Downey Jr.',
'$permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
]
],
]);
$response = json_decode($client->receive(), true);
$response = json_decode($client->receive(), true);
$this->assertArrayHasKey('type', $response);
$this->assertArrayHasKey('data', $response);
$this->assertEquals('event', $response['type']);
$this->assertNotEmpty($response['data']);
$this->assertArrayHasKey('timestamp', $response['data']);
$this->assertCount(6, $response['data']['channels']);
$this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.{$response['data']['payload']['$id']}.upsert", $response['data']['events']);
$this->assertContains("databases.*.collections.*.documents.*.upsert", $response['data']['events']);
$this->assertContains("databases.{$databaseId}.collections.*.documents.*.upsert", $response['data']['events']);
$this->assertContains("databases.*.collections.{$actorsId}.documents.*.upsert", $response['data']['events']);
$this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.*", $response['data']['events']);
$this->assertContains("databases.*.collections.*.documents.*", $response['data']['events']);
$this->assertContains("databases.{$databaseId}.collections.*.documents.*", $response['data']['events']);
$this->assertContains("databases.*.collections.{$actorsId}.documents.*", $response['data']['events']);
$this->assertContains("databases.{$databaseId}.collections.{$actorsId}", $response['data']['events']);
$this->assertContains("databases.*.collections.*", $response['data']['events']);
$this->assertContains("databases.{$databaseId}.collections.*", $response['data']['events']);
$this->assertContains("databases.*.collections.{$actorsId}", $response['data']['events']);
$this->assertContains("databases.{$databaseId}.collections.*.documents.*.upsert", $response['data']['events']);
$this->assertContains("databases.*", $response['data']['events']);
$this->assertNotEmpty($response['data']['payload']);
$this->assertIsArray($response['data']['payload']);
$this->assertArrayHasKey('$id', $response['data']['payload']);
$this->assertArrayHasKey('name', $response['data']['payload']);
$this->assertArrayHasKey('$permissions', $response['data']['payload']);
$this->assertIsArray($response['data']['payload']['$permissions']);
$client->close();
}