@@ -92,14 +92,16 @@ class Appwrite extends Destination
9292 * @param UtopiaDatabase $dbForProject
9393 * @param callable(UtopiaDocument $database):UtopiaDatabase $getDatabasesDB
9494 * @param array<array<string, mixed>> $collectionStructure
95+ * @param OnDuplicate $onDuplicate Behavior when a row with an existing $id is encountered.
9596 */
9697 public function __construct (
9798 string $ project ,
9899 string $ endpoint ,
99100 string $ key ,
100101 protected UtopiaDatabase $ dbForProject ,
101102 callable $ getDatabasesDB ,
102- protected array $ collectionStructure
103+ protected array $ collectionStructure ,
104+ protected OnDuplicate $ onDuplicate = OnDuplicate::Fail,
103105 ) {
104106 $ this ->project = $ project ;
105107 $ this ->endpoint = $ endpoint ;
@@ -1067,10 +1069,21 @@ protected function createRecord(Row $resource, bool $isLast): bool
10671069 }
10681070 }
10691071 }
1070- $ dbForDatabases ->skipRelationshipsExistCheck (fn () => $ dbForDatabases ->createDocuments (
1071- 'database_ ' . $ databaseInternalId . '_collection_ ' . $ tableInternalId ,
1072- $ this ->rowBuffer
1073- ));
1072+ $ collectionId = 'database_ ' . $ databaseInternalId . '_collection_ ' . $ tableInternalId ;
1073+
1074+ match ($ this ->onDuplicate ) {
1075+ OnDuplicate::Upsert => $ dbForDatabases ->skipRelationshipsExistCheck (
1076+ fn () => $ dbForDatabases ->upsertDocuments ($ collectionId , $ this ->rowBuffer )
1077+ ),
1078+ OnDuplicate::Skip => $ dbForDatabases ->skipDuplicates (
1079+ fn () => $ dbForDatabases ->skipRelationshipsExistCheck (
1080+ fn () => $ dbForDatabases ->createDocuments ($ collectionId , $ this ->rowBuffer )
1081+ )
1082+ ),
1083+ OnDuplicate::Fail => $ dbForDatabases ->skipRelationshipsExistCheck (
1084+ fn () => $ dbForDatabases ->createDocuments ($ collectionId , $ this ->rowBuffer )
1085+ ),
1086+ };
10741087
10751088 } finally {
10761089 $ this ->rowBuffer = [];
0 commit comments