firestore 0.49.0

Library provides a simple API for Google Firestore and own Serde serializer based on efficient gRPC API
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
//! Builder for constructing Firestore update operations.
//!
//! This module provides a fluent API to specify the document to be updated,
//! the data to update (either a full object, specific fields, or field transformations),
//! and optional preconditions.

use crate::document_transform_builder::FirestoreTransformBuilder;
use crate::{
    FirestoreBatch, FirestoreBatchWriter, FirestoreFieldTransform, FirestoreResult,
    FirestoreTransaction, FirestoreTransactionOps, FirestoreUpdateSupport,
    FirestoreWritePrecondition,
};
use gcloud_sdk::google::firestore::v1::Document;
use serde::{Deserialize, Serialize};

/// The initial builder for a Firestore update operation.
///
/// Created by calling [`FirestoreExprBuilder::update()`](crate::FirestoreExprBuilder::update).
/// This builder allows specifying which fields to update. If no fields are specified,
/// the entire object provided later will be merged with the existing document.
#[derive(Clone, Debug)]
pub struct FirestoreUpdateInitialBuilder<'a, D>
where
    D: FirestoreUpdateSupport,
{
    db: &'a D,
    update_only_fields: Option<Vec<String>>,
}

impl<'a, D> FirestoreUpdateInitialBuilder<'a, D>
where
    D: FirestoreUpdateSupport,
{
    /// Creates a new `FirestoreUpdateInitialBuilder`.
    #[inline]
    pub(crate) fn new(db: &'a D) -> Self {
        Self {
            db,
            update_only_fields: None,
        }
    }

    /// Specifies the exact set of fields to update.
    ///
    /// If this is set, only the fields listed here will be modified. Any other fields
    /// in the provided object/document will be ignored. If not set (the default),
    /// the update acts as a merge: fields in the provided object will overwrite
    /// existing fields, and new fields will be added. Fields not present in the
    /// provided object will remain untouched in the document.
    ///
    /// # Arguments
    /// * `update_only_fields`: An iterator of field paths (dot-separated for nested fields)
    ///   to be included in the update mask.
    ///
    /// # Returns
    /// The builder instance with the field mask set.
    #[inline]
    pub fn fields<I>(self, update_only_fields: I) -> Self
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
    {
        Self {
            update_only_fields: Some(
                update_only_fields
                    .into_iter()
                    .map(|field| field.as_ref().to_string())
                    .collect(),
            ),
            ..self
        }
    }

    /// Specifies the collection ID where the document to update resides.
    ///
    /// # Arguments
    /// * `collection_id`: The ID of the collection.
    ///
    /// # Returns
    /// A [`FirestoreUpdateDocObjBuilder`] to specify the document ID and data.
    #[inline]
    pub fn in_col(self, collection_id: &str) -> FirestoreUpdateDocObjBuilder<'a, D> {
        FirestoreUpdateDocObjBuilder::new(
            self.db,
            collection_id.to_string(),
            self.update_only_fields,
        )
    }
}

/// A builder for specifying the document ID and data for an update operation.
///
/// This stage allows setting the document data (as a raw `Document` or a serializable object),
/// preconditions, field transformations, and which fields to return after the update.
#[derive(Clone, Debug)]
pub struct FirestoreUpdateDocObjBuilder<'a, D>
where
    D: FirestoreUpdateSupport,
{
    db: &'a D,
    collection_id: String,
    update_only_fields: Option<Vec<String>>,
    parent: Option<String>,
    return_only_fields: Option<Vec<String>>,
    precondition: Option<FirestoreWritePrecondition>,
    transforms: Vec<FirestoreFieldTransform>,
}

impl<'a, D> FirestoreUpdateDocObjBuilder<'a, D>
where
    D: FirestoreUpdateSupport,
{
    /// Creates a new `FirestoreUpdateDocObjBuilder`.
    #[inline]
    pub(crate) fn new(
        db: &'a D,
        collection_id: String,
        update_only_fields: Option<Vec<String>>,
    ) -> Self {
        Self {
            db,
            collection_id,
            update_only_fields,
            parent: None,
            return_only_fields: None,
            precondition: None,
            transforms: vec![],
        }
    }

    /// Specifies which fields of the updated document should be returned.
    ///
    /// If not set, the entire document is typically returned after the update.
    ///
    /// # Arguments
    /// * `return_only_fields`: An iterator of field paths to return.
    ///
    /// # Returns
    /// The builder instance with the projection mask for the return value set.
    #[inline]
    pub fn return_only_fields<I>(self, return_only_fields: I) -> Self
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
    {
        Self {
            return_only_fields: Some(
                return_only_fields
                    .into_iter()
                    .map(|field| field.as_ref().to_string())
                    .collect(),
            ),
            ..self
        }
    }

    /// Specifies a precondition for the update operation.
    ///
    /// The update will only be executed if the precondition is met.
    ///
    /// # Arguments
    /// * `precondition`: The [`FirestoreWritePrecondition`] to apply.
    ///
    /// # Returns
    /// The builder instance with the precondition set.
    #[inline]
    pub fn precondition(self, precondition: FirestoreWritePrecondition) -> Self {
        Self {
            precondition: Some(precondition),
            ..self
        }
    }

    /// Specifies server-side field transformations to apply as part of the update.
    ///
    /// The `doc_transform` argument is a closure that receives a [`FirestoreTransformBuilder`]
    /// and should return a `Vec<FirestoreFieldTransform>`.
    ///
    /// # Arguments
    /// * `doc_transform`: A closure to build the list of field transformations.
    ///
    /// # Returns
    /// The builder instance with the field transformations set.
    #[inline]
    pub fn transforms<FN>(self, doc_transform: FN) -> Self
    where
        FN: Fn(FirestoreTransformBuilder) -> Vec<FirestoreFieldTransform>,
    {
        Self {
            transforms: doc_transform(FirestoreTransformBuilder::new()),
            ..self
        }
    }

    /// Specifies the document data to update using a raw [`Document`].
    ///
    /// The `document.name` field should contain the full path to the document.
    ///
    /// # Arguments
    /// * `document`: The Firestore `Document` containing the fields to update.
    ///
    /// # Returns
    /// A [`FirestoreUpdateDocExecuteBuilder`] to execute the operation.
    #[inline]
    pub fn document(self, document: Document) -> FirestoreUpdateDocExecuteBuilder<'a, D> {
        FirestoreUpdateDocExecuteBuilder::new(
            self.db,
            self.collection_id.to_string(),
            self.update_only_fields,
            document,
            self.return_only_fields,
            self.precondition,
        )
    }

    /// Specifies the ID of the document to update.
    ///
    /// This transitions the builder to expect a Rust object for the update data.
    ///
    /// # Arguments
    /// * `document_id`: The ID of the document to update.
    ///
    /// # Returns
    /// A [`FirestoreUpdateObjInitExecuteBuilder`] to specify the object and execute.
    #[inline]
    pub fn document_id<S>(self, document_id: S) -> FirestoreUpdateObjInitExecuteBuilder<'a, D>
    where
        S: AsRef<str> + Send,
    {
        FirestoreUpdateObjInitExecuteBuilder::new(
            self.db,
            self.collection_id,
            self.update_only_fields,
            self.parent,
            document_id.as_ref().to_string(),
            self.return_only_fields,
            self.precondition,
            self.transforms,
        )
    }
}

/// A builder for executing an update operation with raw [`Document`] data.
#[derive(Clone, Debug)]
pub struct FirestoreUpdateDocExecuteBuilder<'a, D>
where
    D: FirestoreUpdateSupport,
{
    db: &'a D,
    collection_id: String,
    update_only_fields: Option<Vec<String>>,
    document: Document,
    return_only_fields: Option<Vec<String>>,
    precondition: Option<FirestoreWritePrecondition>,
}

impl<'a, D> FirestoreUpdateDocExecuteBuilder<'a, D>
where
    D: FirestoreUpdateSupport,
{
    /// Creates a new `FirestoreUpdateDocExecuteBuilder`.
    #[inline]
    pub(crate) fn new(
        db: &'a D,
        collection_id: String,
        update_only_fields: Option<Vec<String>>,
        document: Document,
        return_only_fields: Option<Vec<String>>,
        precondition: Option<FirestoreWritePrecondition>,
    ) -> Self {
        Self {
            db,
            collection_id,
            update_only_fields,
            document,
            return_only_fields,
            precondition,
        }
    }

    /// Executes the configured update operation using a raw `Document`.
    ///
    /// # Returns
    /// A `FirestoreResult` containing the updated [`Document`].
    pub async fn execute(self) -> FirestoreResult<Document> {
        // Note: The `update_doc` method on `FirestoreUpdateSupport` expects the full document path
        // to be in `self.document.name`. The `collection_id` here is somewhat redundant if
        // `document.name` is correctly populated, but kept for consistency with other builders.
        self.db
            .update_doc(
                self.collection_id.as_str(),
                self.document,
                self.update_only_fields,
                self.return_only_fields,
                self.precondition,
            )
            .await
    }
}

/// An intermediate builder stage for update operations using a Rust object.
/// This stage has the document ID and is ready to accept the object to update with.
#[derive(Clone, Debug)]
pub struct FirestoreUpdateObjInitExecuteBuilder<'a, D>
where
    D: FirestoreUpdateSupport,
{
    db: &'a D,
    collection_id: String,
    update_only_fields: Option<Vec<String>>,
    parent: Option<String>,
    document_id: String,
    return_only_fields: Option<Vec<String>>,
    precondition: Option<FirestoreWritePrecondition>,
    transforms: Vec<FirestoreFieldTransform>,
}

impl<'a, D> FirestoreUpdateObjInitExecuteBuilder<'a, D>
where
    D: FirestoreUpdateSupport,
{
    /// Creates a new `FirestoreUpdateObjInitExecuteBuilder`.
    #[inline]
    pub(crate) fn new(
        db: &'a D,
        collection_id: String,
        update_only_fields: Option<Vec<String>>,
        parent: Option<String>,
        document_id: String,
        return_only_fields: Option<Vec<String>>,
        precondition: Option<FirestoreWritePrecondition>,
        transforms: Vec<FirestoreFieldTransform>,
    ) -> Self {
        Self {
            db,
            collection_id,
            update_only_fields,
            parent,
            document_id,
            return_only_fields,
            precondition,
            transforms,
        }
    }

    /// Specifies the parent document path for updating a document in a sub-collection.
    #[inline]
    pub fn parent<S>(self, parent: S) -> Self
    where
        S: AsRef<str>,
    {
        Self {
            parent: Some(parent.as_ref().to_string()),
            ..self
        }
    }

    /// Specifies the Rust object containing the data to update the document with.
    ///
    /// The object `T` must implement `serde::Serialize`.
    ///
    /// # Arguments
    /// * `object`: A reference to the Rust object.
    ///
    /// # Type Parameters
    /// * `T`: The type of the object.
    ///
    /// # Returns
    /// A [`FirestoreUpdateObjExecuteBuilder`] to execute the operation or add it to a batch/transaction.
    #[inline]
    pub fn object<T>(self, object: &'a T) -> FirestoreUpdateObjExecuteBuilder<'a, D, T>
    where
        T: Serialize + Sync + Send,
        for<'de> T: Deserialize<'de>,
    {
        FirestoreUpdateObjExecuteBuilder::new(
            self.db,
            self.collection_id.to_string(),
            self.update_only_fields,
            self.parent,
            self.document_id,
            object,
            self.return_only_fields,
            self.precondition,
            self.transforms,
        )
    }

    /// Specifies server-side field transformations to apply.
    /// This method is used when the update consists *only* of transformations,
    /// without merging an object's fields.
    ///
    /// # Arguments
    /// * `doc_transform`: A closure to build the list of field transformations.
    ///
    /// # Returns
    /// The builder instance with the field transformations set.
    #[inline]
    pub fn transforms<FN>(self, doc_transform: FN) -> Self
    where
        FN: Fn(FirestoreTransformBuilder) -> Vec<FirestoreFieldTransform>,
    {
        Self {
            transforms: doc_transform(FirestoreTransformBuilder::new()),
            ..self
        }
    }

    /// Finalizes the builder for an update operation that *only* applies field transformations.
    ///
    /// This should be called if no `.object()` is provided, and the update relies solely
    /// on the transformations defined via `.transforms()`.
    ///
    /// # Returns
    /// A [`FirestoreUpdateOnlyTransformBuilder`] to add the transform-only operation to a batch or transaction.
    #[inline]
    pub fn only_transform(self) -> FirestoreUpdateOnlyTransformBuilder<'a, D> {
        FirestoreUpdateOnlyTransformBuilder::new(
            self.db,
            self.collection_id.to_string(),
            self.parent,
            self.document_id,
            self.precondition,
            self.transforms,
        )
    }
}

/// A builder for executing an update operation with a serializable Rust object.
#[derive(Clone, Debug)]
pub struct FirestoreUpdateObjExecuteBuilder<'a, D, T>
where
    D: FirestoreUpdateSupport,
    T: Serialize + Sync + Send,
{
    db: &'a D,
    collection_id: String,
    update_only_fields: Option<Vec<String>>,
    parent: Option<String>,
    document_id: String,
    object: &'a T,
    return_only_fields: Option<Vec<String>>,
    precondition: Option<FirestoreWritePrecondition>,
    transforms: Vec<FirestoreFieldTransform>,
}

impl<'a, D, T> FirestoreUpdateObjExecuteBuilder<'a, D, T>
where
    D: FirestoreUpdateSupport,
    T: Serialize + Sync + Send,
{
    /// Creates a new `FirestoreUpdateObjExecuteBuilder`.
    #[inline]
    pub(crate) fn new(
        db: &'a D,
        collection_id: String,
        update_only_fields: Option<Vec<String>>,
        parent: Option<String>,
        document_id: String,
        object: &'a T,
        return_only_fields: Option<Vec<String>>,
        precondition: Option<FirestoreWritePrecondition>,
        transforms: Vec<FirestoreFieldTransform>,
    ) -> Self {
        Self {
            db,
            collection_id,
            update_only_fields,
            parent,
            document_id,
            object,
            return_only_fields,
            precondition,
            transforms,
        }
    }

    /// Executes the configured update operation, serializing the object and
    /// deserializing the result into type `O`.
    ///
    /// # Type Parameters
    /// * `O`: The type to deserialize the result into. Must implement `serde::Deserialize`.
    ///
    /// # Returns
    /// A `FirestoreResult` containing the deserialized object `O` representing the updated document.
    pub async fn execute<O>(self) -> FirestoreResult<O>
    where
        for<'de> O: Deserialize<'de>,
    {
        if let Some(parent) = self.parent {
            self.db
                .update_obj_at(
                    parent.as_str(),
                    self.collection_id.as_str(),
                    self.document_id,
                    self.object,
                    self.update_only_fields,
                    self.return_only_fields,
                    self.precondition,
                    // Note: The current FirestoreUpdateSupport::update_obj_at doesn't take transforms.
                    // This might be an oversight or transforms are handled differently for object updates.
                    // If transforms are intended here, the trait method needs adjustment.
                    // For now, passing an empty vec or ignoring self.transforms if not supported by the trait.
                )
                .await
        } else {
            self.db
                .update_obj(
                    self.collection_id.as_str(),
                    self.document_id,
                    self.object,
                    self.update_only_fields,
                    self.return_only_fields,
                    self.precondition,
                    // Similar note as above for transforms.
                )
                .await
        }
    }

    /// Adds server-side field transformations to the update operation.
    ///
    /// This can be combined with updating fields from an object. The transformations
    /// are applied *after* the object merge/update.
    ///
    /// # Arguments
    /// * `transforms_builder`: A closure to build the list of field transformations.
    ///
    /// # Returns
    /// The builder instance with added transformations.
    #[inline]
    pub fn transforms<FN>(self, transforms_builder: FN) -> Self
    where
        FN: Fn(FirestoreTransformBuilder) -> Vec<FirestoreFieldTransform>,
    {
        Self {
            transforms: transforms_builder(FirestoreTransformBuilder::new()),
            ..self
        }
    }

    /// Adds this update operation (object merge and/or transforms) to a [`FirestoreTransaction`].
    ///
    /// # Arguments
    /// * `transaction`: A mutable reference to the transaction.
    ///
    /// # Returns
    /// A `FirestoreResult` containing the mutable reference to the transaction.
    #[inline]
    pub fn add_to_transaction<'t, TO>(self, transaction: &'t mut TO) -> FirestoreResult<&'t mut TO>
    where
        TO: FirestoreTransactionOps,
    {
        if let Some(parent) = self.parent {
            transaction.update_object_at(
                parent.as_str(),
                self.collection_id.as_str(),
                self.document_id,
                self.object,
                self.update_only_fields,
                self.precondition,
                self.transforms,
            )
        } else {
            transaction.update_object(
                self.collection_id.as_str(),
                self.document_id,
                self.object,
                self.update_only_fields,
                self.precondition,
                self.transforms,
            )
        }
    }

    /// Adds this update operation (object merge and/or transforms) to a [`FirestoreBatch`].
    ///
    /// # Arguments
    /// * `batch`: A mutable reference to the batch writer.
    ///
    /// # Type Parameters
    /// * `W`: The type of the batch writer.
    ///
    /// # Returns
    /// A `FirestoreResult` containing the mutable reference to the batch.
    #[inline]
    pub fn add_to_batch<'t, W>(
        self,
        batch: &'a mut FirestoreBatch<'t, W>,
    ) -> FirestoreResult<&'a mut FirestoreBatch<'t, W>>
    where
        W: FirestoreBatchWriter,
    {
        if let Some(parent) = self.parent {
            batch.update_object_at(
                parent.as_str(),
                self.collection_id.as_str(),
                self.document_id,
                self.object,
                self.update_only_fields,
                self.precondition,
                self.transforms,
            )
        } else {
            batch.update_object(
                self.collection_id.as_str(),
                self.document_id,
                self.object,
                self.update_only_fields,
                self.precondition,
                self.transforms,
            )
        }
    }
}

/// A builder for an update operation that consists *only* of field transformations.
///
/// This is used when no object data is being merged, and the update is solely
/// defined by server-side atomic operations like increment, array manipulation, etc.
#[derive(Clone, Debug)]
pub struct FirestoreUpdateOnlyTransformBuilder<'a, D>
where
    D: FirestoreUpdateSupport,
{
    _db: &'a D,
    collection_id: String,
    parent: Option<String>,
    document_id: String,
    precondition: Option<FirestoreWritePrecondition>,
    transforms: Vec<FirestoreFieldTransform>,
}

impl<'a, D> FirestoreUpdateOnlyTransformBuilder<'a, D>
where
    D: FirestoreUpdateSupport,
{
    /// Creates a new `FirestoreUpdateOnlyTransformBuilder`.
    #[inline]
    pub(crate) fn new(
        db: &'a D,
        collection_id: String,
        parent: Option<String>,
        document_id: String,
        precondition: Option<FirestoreWritePrecondition>,
        transforms: Vec<FirestoreFieldTransform>,
    ) -> Self {
        Self {
            _db: db,
            collection_id,
            parent,
            document_id,
            precondition,
            transforms,
        }
    }

    /// Adds this transform-only update operation to a [`FirestoreTransaction`].
    ///
    /// # Arguments
    /// * `transaction`: A mutable reference to the transaction.
    ///
    /// # Returns
    /// A `FirestoreResult` containing the mutable reference to the transaction.
    #[inline]
    pub fn add_to_transaction<'t>(
        self,
        transaction: &'a mut FirestoreTransaction<'t>,
    ) -> FirestoreResult<&'a mut FirestoreTransaction<'t>> {
        if let Some(parent) = self.parent {
            transaction.transform_at(
                parent.as_str(),
                self.collection_id.as_str(),
                self.document_id,
                self.precondition,
                self.transforms,
            )
        } else {
            transaction.transform(
                self.collection_id.as_str(),
                self.document_id,
                self.precondition,
                self.transforms,
            )
        }
    }

    /// Adds this transform-only update operation to a [`FirestoreBatch`].
    ///
    /// # Arguments
    /// * `batch`: A mutable reference to the batch writer.
    ///
    /// # Type Parameters
    /// * `W`: The type of the batch writer.
    ///
    /// # Returns
    /// A `FirestoreResult` containing the mutable reference to the batch.
    #[inline]
    pub fn add_to_batch<'t, W>(
        self,
        batch: &'a mut FirestoreBatch<'t, W>,
    ) -> FirestoreResult<&'a mut FirestoreBatch<'t, W>>
    where
        W: FirestoreBatchWriter,
    {
        if let Some(parent) = self.parent {
            batch.transform_at(
                parent.as_str(),
                self.collection_id.as_str(),
                self.document_id,
                self.precondition,
                self.transforms,
            )
        } else {
            batch.transform(
                self.collection_id.as_str(),
                self.document_id,
                self.precondition,
                self.transforms,
            )
        }
    }
}