appwrite 0.14.0

Appwrite SDK for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
//! VectorsDB service for Appwrite SDK

use crate::client::Client;

use reqwest::Method;
use serde_json::json;
use std::collections::HashMap;

#[derive(Debug, Clone)]
pub struct VectorsDB {
    client: Client,
}

impl VectorsDB {
    pub fn new(client: &Client) -> Self {
        Self {
            client: client.clone(),
        }
    }

    pub fn client(&self) -> &Client {
        &self.client
    }

    /// Get a list of all databases from the current Appwrite project. You can use
    /// the search parameter to filter your results.
    pub async fn list(
        &self,
        queries: Option<Vec<String>>,
        total: Option<bool>,
    ) -> crate::error::Result<crate::models::DatabaseList> {
        let mut params = HashMap::new();
        if let Some(value) = queries {
            params.insert(
                "queries".to_string(),
                json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
            );
        }
        if let Some(value) = total {
            params.insert("total".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb".to_string();

        self.client
            .call(Method::GET, &path, Some(api_headers), Some(params))
            .await
    }

    /// Create a new Database.
    pub async fn create(
        &self,
        database_id: impl Into<String>,
        name: impl Into<String>,
        enabled: Option<bool>,
        specification: Option<&str>,
        replicas: Option<i64>,
        sync_mode: Option<&str>,
    ) -> crate::error::Result<crate::models::Database> {
        let mut params = HashMap::new();
        params.insert("databaseId".to_string(), json!(database_id.into()));
        params.insert("name".to_string(), json!(name.into()));
        if let Some(value) = enabled {
            params.insert("enabled".to_string(), json!(value));
        }
        if let Some(value) = specification {
            params.insert("specification".to_string(), json!(value));
        }
        if let Some(value) = replicas {
            params.insert("replicas".to_string(), json!(value));
        }
        if let Some(value) = sync_mode {
            params.insert("syncMode".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb".to_string();

        self.client
            .call(Method::POST, &path, Some(api_headers), Some(params))
            .await
    }

    /// List the dedicated database specifications available on the current plan.
    /// Each specification reports its resource limits, pricing, and whether it is
    /// enabled for the organization.
    pub async fn list_specifications(
        &self,
    ) -> crate::error::Result<crate::models::DedicatedDatabaseSpecificationList> {
        let params = HashMap::new();
        let mut api_headers = HashMap::new();
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/specifications".to_string();

        self.client
            .call(Method::GET, &path, Some(api_headers), Some(params))
            .await
    }

    /// List transactions across all databases.
    pub async fn list_transactions(
        &self,
        queries: Option<Vec<String>>,
    ) -> crate::error::Result<crate::models::TransactionList> {
        let mut params = HashMap::new();
        if let Some(value) = queries {
            params.insert(
                "queries".to_string(),
                json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
            );
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/transactions".to_string();

        self.client
            .call(Method::GET, &path, Some(api_headers), Some(params))
            .await
    }

    /// Create a new transaction.
    pub async fn create_transaction(
        &self,
        ttl: Option<i64>,
    ) -> crate::error::Result<crate::models::Transaction> {
        let mut params = HashMap::new();
        if let Some(value) = ttl {
            params.insert("ttl".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/transactions".to_string();

        self.client
            .call(Method::POST, &path, Some(api_headers), Some(params))
            .await
    }

    /// Get a transaction by its unique ID.
    pub async fn get_transaction(
        &self,
        transaction_id: impl Into<String>,
    ) -> crate::error::Result<crate::models::Transaction> {
        let params = HashMap::new();
        let mut api_headers = HashMap::new();
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/transactions/{transactionId}"
            .to_string()
            .replace("{transactionId}", &transaction_id.into().to_string());

        self.client
            .call(Method::GET, &path, Some(api_headers), Some(params))
            .await
    }

    /// Update a transaction, to either commit or roll back its operations.
    pub async fn update_transaction(
        &self,
        transaction_id: impl Into<String>,
        commit: Option<bool>,
        rollback: Option<bool>,
    ) -> crate::error::Result<crate::models::Transaction> {
        let mut params = HashMap::new();
        if let Some(value) = commit {
            params.insert("commit".to_string(), json!(value));
        }
        if let Some(value) = rollback {
            params.insert("rollback".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/transactions/{transactionId}"
            .to_string()
            .replace("{transactionId}", &transaction_id.into().to_string());

        self.client
            .call(Method::PATCH, &path, Some(api_headers), Some(params))
            .await
    }

    /// Delete a transaction by its unique ID.
    pub async fn delete_transaction(
        &self,
        transaction_id: impl Into<String>,
    ) -> crate::error::Result<()> {
        let params = HashMap::new();
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());

        let path = "/vectorsdb/transactions/{transactionId}"
            .to_string()
            .replace("{transactionId}", &transaction_id.into().to_string());

        self.client
            .call(Method::DELETE, &path, Some(api_headers), Some(params))
            .await
    }

    /// Create multiple operations in a single transaction.
    pub async fn create_operations(
        &self,
        transaction_id: impl Into<String>,
        operations: Option<Vec<serde_json::Value>>,
    ) -> crate::error::Result<crate::models::Transaction> {
        let mut params = HashMap::new();
        if let Some(value) = operations {
            params.insert("operations".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/transactions/{transactionId}/operations"
            .to_string()
            .replace("{transactionId}", &transaction_id.into().to_string());

        self.client
            .call(Method::POST, &path, Some(api_headers), Some(params))
            .await
    }

    /// Get a database by its unique ID. This endpoint response returns a JSON
    /// object with the database metadata.
    pub async fn get(
        &self,
        database_id: impl Into<String>,
    ) -> crate::error::Result<crate::models::Database> {
        let params = HashMap::new();
        let mut api_headers = HashMap::new();
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string());

        self.client
            .call(Method::GET, &path, Some(api_headers), Some(params))
            .await
    }

    /// Update a database by its unique ID.
    pub async fn update(
        &self,
        database_id: impl Into<String>,
        name: impl Into<String>,
        enabled: Option<bool>,
        specification: Option<&str>,
        replicas: Option<i64>,
        sync_mode: Option<&str>,
    ) -> crate::error::Result<crate::models::Database> {
        let mut params = HashMap::new();
        params.insert("name".to_string(), json!(name.into()));
        if let Some(value) = enabled {
            params.insert("enabled".to_string(), json!(value));
        }
        if let Some(value) = specification {
            params.insert("specification".to_string(), json!(value));
        }
        if let Some(value) = replicas {
            params.insert("replicas".to_string(), json!(value));
        }
        if let Some(value) = sync_mode {
            params.insert("syncMode".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string());

        self.client
            .call(Method::PUT, &path, Some(api_headers), Some(params))
            .await
    }

    /// Delete a database by its unique ID. Only API keys with with databases.write
    /// scope can delete a database.
    pub async fn delete(&self, database_id: impl Into<String>) -> crate::error::Result<()> {
        let params = HashMap::new();
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string());

        self.client
            .call(Method::DELETE, &path, Some(api_headers), Some(params))
            .await
    }

    /// Get a list of all collections that belong to the provided databaseId. You
    /// can use the search parameter to filter your results.
    pub async fn list_collections(
        &self,
        database_id: impl Into<String>,
        queries: Option<Vec<String>>,
        search: Option<&str>,
        total: Option<bool>,
    ) -> crate::error::Result<crate::models::VectorsdbCollectionList> {
        let mut params = HashMap::new();
        if let Some(value) = queries {
            params.insert(
                "queries".to_string(),
                json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
            );
        }
        if let Some(value) = search {
            params.insert("search".to_string(), json!(value));
        }
        if let Some(value) = total {
            params.insert("total".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/collections"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string());

        self.client
            .call(Method::GET, &path, Some(api_headers), Some(params))
            .await
    }

    /// Create a new Collection. Before using this route, you should create a new
    /// database resource using either a [server
    /// integration](https://appwrite.io/docs/server/databases#documentsDBCreateCollection)
    /// API or directly from your database console.
    #[allow(clippy::too_many_arguments)]
    pub async fn create_collection(
        &self,
        database_id: impl Into<String>,
        collection_id: impl Into<String>,
        name: impl Into<String>,
        dimension: i64,
        permissions: Option<Vec<String>>,
        document_security: Option<bool>,
        enabled: Option<bool>,
    ) -> crate::error::Result<crate::models::VectorsdbCollection> {
        let mut params = HashMap::new();
        params.insert("collectionId".to_string(), json!(collection_id.into()));
        params.insert("name".to_string(), json!(name.into()));
        params.insert("dimension".to_string(), json!(dimension));
        if let Some(value) = permissions {
            params.insert(
                "permissions".to_string(),
                json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
            );
        }
        if let Some(value) = document_security {
            params.insert("documentSecurity".to_string(), json!(value));
        }
        if let Some(value) = enabled {
            params.insert("enabled".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/collections"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string());

        self.client
            .call(Method::POST, &path, Some(api_headers), Some(params))
            .await
    }

    /// Get a collection by its unique ID. This endpoint response returns a JSON
    /// object with the collection metadata.
    pub async fn get_collection(
        &self,
        database_id: impl Into<String>,
        collection_id: impl Into<String>,
    ) -> crate::error::Result<crate::models::VectorsdbCollection> {
        let params = HashMap::new();
        let mut api_headers = HashMap::new();
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/collections/{collectionId}"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string())
            .replace("{collectionId}", &collection_id.into().to_string());

        self.client
            .call(Method::GET, &path, Some(api_headers), Some(params))
            .await
    }

    /// Update a collection by its unique ID.
    #[allow(clippy::too_many_arguments)]
    pub async fn update_collection(
        &self,
        database_id: impl Into<String>,
        collection_id: impl Into<String>,
        name: impl Into<String>,
        dimension: Option<i64>,
        permissions: Option<Vec<String>>,
        document_security: Option<bool>,
        enabled: Option<bool>,
    ) -> crate::error::Result<crate::models::VectorsdbCollection> {
        let mut params = HashMap::new();
        params.insert("name".to_string(), json!(name.into()));
        if let Some(value) = dimension {
            params.insert("dimension".to_string(), json!(value));
        }
        if let Some(value) = permissions {
            params.insert(
                "permissions".to_string(),
                json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
            );
        }
        if let Some(value) = document_security {
            params.insert("documentSecurity".to_string(), json!(value));
        }
        if let Some(value) = enabled {
            params.insert("enabled".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/collections/{collectionId}"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string())
            .replace("{collectionId}", &collection_id.into().to_string());

        self.client
            .call(Method::PUT, &path, Some(api_headers), Some(params))
            .await
    }

    /// Delete a collection by its unique ID. Only users with write permissions
    /// have access to delete this resource.
    pub async fn delete_collection(
        &self,
        database_id: impl Into<String>,
        collection_id: impl Into<String>,
    ) -> crate::error::Result<()> {
        let params = HashMap::new();
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/collections/{collectionId}"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string())
            .replace("{collectionId}", &collection_id.into().to_string());

        self.client
            .call(Method::DELETE, &path, Some(api_headers), Some(params))
            .await
    }

    /// Get a list of all the user's documents in a given collection. You can use
    /// the query params to filter your results.
    pub async fn list_documents(
        &self,
        database_id: impl Into<String>,
        collection_id: impl Into<String>,
        queries: Option<Vec<String>>,
        transaction_id: Option<&str>,
        total: Option<bool>,
        ttl: Option<i64>,
    ) -> crate::error::Result<crate::models::DocumentList> {
        let mut params = HashMap::new();
        if let Some(value) = queries {
            params.insert(
                "queries".to_string(),
                json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
            );
        }
        if let Some(value) = transaction_id {
            params.insert("transactionId".to_string(), json!(value));
        }
        if let Some(value) = total {
            params.insert("total".to_string(), json!(value));
        }
        if let Some(value) = ttl {
            params.insert("ttl".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/collections/{collectionId}/documents"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string())
            .replace("{collectionId}", &collection_id.into().to_string());

        self.client
            .call(Method::GET, &path, Some(api_headers), Some(params))
            .await
    }

    /// Create a new Document. Before using this route, you should create a new
    /// collection resource using either a [server
    /// integration](https://appwrite.io/docs/server/databases#documentsDBCreateCollection)
    /// API or directly from your database console.
    pub async fn create_document(
        &self,
        database_id: impl Into<String>,
        collection_id: impl Into<String>,
        document_id: impl Into<String>,
        data: serde_json::Value,
        permissions: Option<Vec<String>>,
        transaction_id: Option<&str>,
    ) -> crate::error::Result<crate::models::Document> {
        let mut params = HashMap::new();
        params.insert("documentId".to_string(), json!(document_id.into()));
        params.insert("data".to_string(), json!(data));
        if let Some(value) = permissions {
            params.insert(
                "permissions".to_string(),
                json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
            );
        }
        if let Some(value) = transaction_id {
            params.insert("transactionId".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/collections/{collectionId}/documents"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string())
            .replace("{collectionId}", &collection_id.into().to_string());

        self.client
            .call(Method::POST, &path, Some(api_headers), Some(params))
            .await
    }

    /// Create new Documents. Before using this route, you should create a new
    /// collection resource using either a [server
    /// integration](https://appwrite.io/docs/server/databases#documentsDBCreateCollection)
    /// API or directly from your database console.
    pub async fn create_documents(
        &self,
        database_id: impl Into<String>,
        collection_id: impl Into<String>,
        documents: Vec<serde_json::Value>,
        transaction_id: Option<&str>,
    ) -> crate::error::Result<crate::models::DocumentList> {
        let mut params = HashMap::new();
        params.insert("documents".to_string(), json!(documents));
        if let Some(value) = transaction_id {
            params.insert("transactionId".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/collections/{collectionId}/documents"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string())
            .replace("{collectionId}", &collection_id.into().to_string());

        self.client
            .call(Method::POST, &path, Some(api_headers), Some(params))
            .await
    }

    /// Create or update Documents. Before using this route, you should create a
    /// new collection resource using either a [server
    /// integration](https://appwrite.io/docs/server/databases#documentsDBCreateCollection)
    /// API or directly from your database console.
    pub async fn upsert_documents(
        &self,
        database_id: impl Into<String>,
        collection_id: impl Into<String>,
        documents: Vec<serde_json::Value>,
        transaction_id: Option<&str>,
    ) -> crate::error::Result<crate::models::DocumentList> {
        let mut params = HashMap::new();
        params.insert("documents".to_string(), json!(documents));
        if let Some(value) = transaction_id {
            params.insert("transactionId".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/collections/{collectionId}/documents"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string())
            .replace("{collectionId}", &collection_id.into().to_string());

        self.client
            .call(Method::PUT, &path, Some(api_headers), Some(params))
            .await
    }

    /// Update all documents that match your queries, if no queries are submitted
    /// then all documents are updated. You can pass only specific fields to be
    /// updated.
    pub async fn update_documents(
        &self,
        database_id: impl Into<String>,
        collection_id: impl Into<String>,
        data: Option<serde_json::Value>,
        queries: Option<Vec<String>>,
        transaction_id: Option<&str>,
    ) -> crate::error::Result<crate::models::DocumentList> {
        let mut params = HashMap::new();
        if let Some(value) = data {
            params.insert("data".to_string(), json!(value));
        }
        if let Some(value) = queries {
            params.insert(
                "queries".to_string(),
                json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
            );
        }
        if let Some(value) = transaction_id {
            params.insert("transactionId".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/collections/{collectionId}/documents"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string())
            .replace("{collectionId}", &collection_id.into().to_string());

        self.client
            .call(Method::PATCH, &path, Some(api_headers), Some(params))
            .await
    }

    /// Bulk delete documents using queries, if no queries are passed then all
    /// documents are deleted.
    pub async fn delete_documents(
        &self,
        database_id: impl Into<String>,
        collection_id: impl Into<String>,
        queries: Option<Vec<String>>,
        transaction_id: Option<&str>,
    ) -> crate::error::Result<crate::models::DocumentList> {
        let mut params = HashMap::new();
        if let Some(value) = queries {
            params.insert(
                "queries".to_string(),
                json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
            );
        }
        if let Some(value) = transaction_id {
            params.insert("transactionId".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/collections/{collectionId}/documents"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string())
            .replace("{collectionId}", &collection_id.into().to_string());

        self.client
            .call(Method::DELETE, &path, Some(api_headers), Some(params))
            .await
    }

    /// Get a list of all the user's documents in a given collection using a POST
    /// request. This behaves identically to the list documents endpoint but
    /// accepts the queries in the request body, allowing much larger `queries`
    /// arrays than can fit in a URL query string.
    pub async fn create_query(
        &self,
        database_id: impl Into<String>,
        collection_id: impl Into<String>,
        queries: Option<Vec<String>>,
        transaction_id: Option<&str>,
        total: Option<bool>,
        ttl: Option<i64>,
    ) -> crate::error::Result<crate::models::DocumentList> {
        let mut params = HashMap::new();
        if let Some(value) = queries {
            params.insert(
                "queries".to_string(),
                json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
            );
        }
        if let Some(value) = transaction_id {
            params.insert("transactionId".to_string(), json!(value));
        }
        if let Some(value) = total {
            params.insert("total".to_string(), json!(value));
        }
        if let Some(value) = ttl {
            params.insert("ttl".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/collections/{collectionId}/documents/query"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string())
            .replace("{collectionId}", &collection_id.into().to_string());

        self.client
            .call(Method::POST, &path, Some(api_headers), Some(params))
            .await
    }

    /// Get a document by its unique ID. This endpoint response returns a JSON
    /// object with the document data.
    pub async fn get_document(
        &self,
        database_id: impl Into<String>,
        collection_id: impl Into<String>,
        document_id: impl Into<String>,
        queries: Option<Vec<String>>,
        transaction_id: Option<&str>,
    ) -> crate::error::Result<crate::models::Document> {
        let mut params = HashMap::new();
        if let Some(value) = queries {
            params.insert(
                "queries".to_string(),
                json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
            );
        }
        if let Some(value) = transaction_id {
            params.insert("transactionId".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/collections/{collectionId}/documents/{documentId}"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string())
            .replace("{collectionId}", &collection_id.into().to_string())
            .replace("{documentId}", &document_id.into().to_string());

        self.client
            .call(Method::GET, &path, Some(api_headers), Some(params))
            .await
    }

    /// Create or update a Document. Before using this route, you should create a
    /// new collection resource using either a [server
    /// integration](https://appwrite.io/docs/server/databases#documentsDBCreateCollection)
    /// API or directly from your database console.
    pub async fn upsert_document(
        &self,
        database_id: impl Into<String>,
        collection_id: impl Into<String>,
        document_id: impl Into<String>,
        data: Option<serde_json::Value>,
        permissions: Option<Vec<String>>,
        transaction_id: Option<&str>,
    ) -> crate::error::Result<crate::models::Document> {
        let mut params = HashMap::new();
        if let Some(value) = data {
            params.insert("data".to_string(), json!(value));
        }
        if let Some(value) = permissions {
            params.insert(
                "permissions".to_string(),
                json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
            );
        }
        if let Some(value) = transaction_id {
            params.insert("transactionId".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/collections/{collectionId}/documents/{documentId}"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string())
            .replace("{collectionId}", &collection_id.into().to_string())
            .replace("{documentId}", &document_id.into().to_string());

        self.client
            .call(Method::PUT, &path, Some(api_headers), Some(params))
            .await
    }

    /// Update a document by its unique ID. Using the patch method you can pass
    /// only specific fields that will get updated.
    pub async fn update_document(
        &self,
        database_id: impl Into<String>,
        collection_id: impl Into<String>,
        document_id: impl Into<String>,
        data: Option<serde_json::Value>,
        permissions: Option<Vec<String>>,
        transaction_id: Option<&str>,
    ) -> crate::error::Result<crate::models::Document> {
        let mut params = HashMap::new();
        if let Some(value) = data {
            params.insert("data".to_string(), json!(value));
        }
        if let Some(value) = permissions {
            params.insert(
                "permissions".to_string(),
                json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
            );
        }
        if let Some(value) = transaction_id {
            params.insert("transactionId".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/collections/{collectionId}/documents/{documentId}"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string())
            .replace("{collectionId}", &collection_id.into().to_string())
            .replace("{documentId}", &document_id.into().to_string());

        self.client
            .call(Method::PATCH, &path, Some(api_headers), Some(params))
            .await
    }

    /// Delete a document by its unique ID.
    pub async fn delete_document(
        &self,
        database_id: impl Into<String>,
        collection_id: impl Into<String>,
        document_id: impl Into<String>,
        transaction_id: Option<&str>,
    ) -> crate::error::Result<()> {
        let mut params = HashMap::new();
        if let Some(value) = transaction_id {
            params.insert("transactionId".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/collections/{collectionId}/documents/{documentId}"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string())
            .replace("{collectionId}", &collection_id.into().to_string())
            .replace("{documentId}", &document_id.into().to_string());

        self.client
            .call(Method::DELETE, &path, Some(api_headers), Some(params))
            .await
    }

    /// List indexes in the collection.
    pub async fn list_indexes(
        &self,
        database_id: impl Into<String>,
        collection_id: impl Into<String>,
        queries: Option<Vec<String>>,
        total: Option<bool>,
    ) -> crate::error::Result<crate::models::IndexList> {
        let mut params = HashMap::new();
        if let Some(value) = queries {
            params.insert(
                "queries".to_string(),
                json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()),
            );
        }
        if let Some(value) = total {
            params.insert("total".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/collections/{collectionId}/indexes"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string())
            .replace("{collectionId}", &collection_id.into().to_string());

        self.client
            .call(Method::GET, &path, Some(api_headers), Some(params))
            .await
    }

    /// Creates an index on the attributes listed. Your index should include all
    /// the attributes you will query in a single request.
    /// Attributes can be `key`, `fulltext`, and `unique`.
    #[allow(clippy::too_many_arguments)]
    pub async fn create_index(
        &self,
        database_id: impl Into<String>,
        collection_id: impl Into<String>,
        key: impl Into<String>,
        r#type: crate::enums::VectorsDBIndexType,
        attributes: impl IntoIterator<Item = impl Into<String>>,
        orders: Option<Vec<crate::enums::OrderBy>>,
        lengths: Option<Vec<i64>>,
    ) -> crate::error::Result<crate::models::Index> {
        let mut params = HashMap::new();
        params.insert("key".to_string(), json!(key.into()));
        params.insert("type".to_string(), json!(r#type));
        params.insert(
            "attributes".to_string(),
            json!(attributes
                .into_iter()
                .map(|s| s.into())
                .collect::<Vec<String>>()),
        );
        if let Some(value) = orders {
            params.insert("orders".to_string(), json!(value));
        }
        if let Some(value) = lengths {
            params.insert("lengths".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/collections/{collectionId}/indexes"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string())
            .replace("{collectionId}", &collection_id.into().to_string());

        self.client
            .call(Method::POST, &path, Some(api_headers), Some(params))
            .await
    }

    /// Get index by ID.
    pub async fn get_index(
        &self,
        database_id: impl Into<String>,
        collection_id: impl Into<String>,
        key: impl Into<String>,
    ) -> crate::error::Result<crate::models::Index> {
        let params = HashMap::new();
        let mut api_headers = HashMap::new();
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/collections/{collectionId}/indexes/{key}"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string())
            .replace("{collectionId}", &collection_id.into().to_string())
            .replace("{key}", &key.into().to_string());

        self.client
            .call(Method::GET, &path, Some(api_headers), Some(params))
            .await
    }

    /// Delete an index.
    pub async fn delete_index(
        &self,
        database_id: impl Into<String>,
        collection_id: impl Into<String>,
        key: impl Into<String>,
    ) -> crate::error::Result<()> {
        let params = HashMap::new();
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/collections/{collectionId}/indexes/{key}"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string())
            .replace("{collectionId}", &collection_id.into().to_string())
            .replace("{key}", &key.into().to_string());

        self.client
            .call(Method::DELETE, &path, Some(api_headers), Some(params))
            .await
    }

    /// Trigger a manual failover for a dedicated database with high availability
    /// enabled. Promotes a replica to primary. The failover runs asynchronously;
    /// poll the database document for status updates. A database left
    /// mid-operation also accepts this call as a repair once nothing is driving
    /// the operation it is stuck in. Repairing a failover that did not finish, a
    /// `failed` database, a stranded upgrade or migrate, or a stranded compute
    /// resize additionally requires `targetReplicaId` to name the member to
    /// promote, because the default target may be the member that operation
    /// already promoted.
    pub async fn create_failover(
        &self,
        database_id: impl Into<String>,
        target_replica_id: Option<&str>,
    ) -> crate::error::Result<crate::models::DedicatedDatabase> {
        let mut params = HashMap::new();
        if let Some(value) = target_replica_id {
            params.insert("targetReplicaId".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("content-type".to_string(), "application/json".to_string());
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/failovers"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string());

        self.client
            .call(Method::POST, &path, Some(api_headers), Some(params))
            .await
    }

    /// List the lifecycle operations recorded for a dedicated database, newest
    /// first. Every provision, update, restore, backup and replication action is
    /// recorded here with its outcome, including an attempt that was abandoned
    /// because another worker took over the database.
    pub async fn list_operations(
        &self,
        database_id: impl Into<String>,
        status: Option<&str>,
        limit: Option<i64>,
        offset: Option<i64>,
    ) -> crate::error::Result<crate::models::DedicatedDatabaseOperationList> {
        let mut params = HashMap::new();
        if let Some(value) = status {
            params.insert("status".to_string(), json!(value));
        }
        if let Some(value) = limit {
            params.insert("limit".to_string(), json!(value));
        }
        if let Some(value) = offset {
            params.insert("offset".to_string(), json!(value));
        }
        let mut api_headers = HashMap::new();
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/operations"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string());

        self.client
            .call(Method::GET, &path, Some(api_headers), Some(params))
            .await
    }

    /// Get high availability status for a dedicated database. Returns replica
    /// statuses, replication lag, and sync mode.
    pub async fn get_replicas(
        &self,
        database_id: impl Into<String>,
    ) -> crate::error::Result<crate::models::DedicatedDatabaseReplicas> {
        let params = HashMap::new();
        let mut api_headers = HashMap::new();
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/replicas"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string());

        self.client
            .call(Method::GET, &path, Some(api_headers), Some(params))
            .await
    }

    /// Get real-time health and status information for a dedicated database.
    /// Returns health status, readiness, uptime, connection info, replica status,
    /// and volume information.
    pub async fn get_status(
        &self,
        database_id: impl Into<String>,
    ) -> crate::error::Result<crate::models::DatabaseStatus> {
        let params = HashMap::new();
        let mut api_headers = HashMap::new();
        api_headers.insert("accept".to_string(), "application/json".to_string());

        let path = "/vectorsdb/{databaseId}/status"
            .to_string()
            .replace("{databaseId}", &database_id.into().to_string());

        self.client
            .call(Method::GET, &path, Some(api_headers), Some(params))
            .await
    }
}

impl crate::services::Service for VectorsDB {
    fn client(&self) -> &Client {
        &self.client
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_vectors_db_creation() {
        let client = Client::new();
        let service = VectorsDB::new(&client);
        assert!(service.client().endpoint().contains("cloud.appwrite.io/v1"));
    }
}