Skip to content

Commit

Permalink
Firestore timestamp serialization for queries support
Browse files Browse the repository at this point in the history
  • Loading branch information
abdolence committed Sep 12, 2022
1 parent 0ceff31 commit d2aca37
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
16 changes: 16 additions & 0 deletions examples/timestamp-serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub fn config_env_var(name: &str) -> Result<String, String> {
#[derive(Debug, Clone, Deserialize, Serialize)]
struct MyTestStructure {
some_id: String,
// Using a special attribute to indicate timestamp serialization for Firestore
// (for serde_json it will be still the same, usually String serialization, so you can reuse the models)
#[serde(with = "firestore::serialize_as_timestamp")]
created_at: DateTime<Utc>,
}
Expand Down Expand Up @@ -46,5 +48,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {

println!("Should be the same: {:?}", find_it_again);

// Query our data
let objects: Vec<MyTestStructure> = db
.query_obj(
FirestoreQueryParams::new(TEST_COLLECTION_NAME.into()).with_filter(
FirestoreQueryFilter::Compare(Some(FirestoreQueryFilterCompare::LessThanOrEqual(
path!(MyTestStructure::created_at),
firestore::FirestoreTimestamp(Utc::now()).into(), // Using the wrapping type to indicate serialization without attribute
))),
),
)
.await?;

println!("Now in the list: {:?}", objects);

Ok(())
}
8 changes: 7 additions & 1 deletion src/firestore_serde/types_serializers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use chrono::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
pub struct FirestoreTimestamp(pub DateTime<Utc>);

pub mod serialize_as_timestamp {

pub(crate) const NEWTYPE_TAG_TYPE: &str = "firestore_timestamp";
pub(crate) const NEWTYPE_TAG_TYPE: &str = "FirestoreTimestamp";

use crate::errors::*;
use crate::{FirestoreError, FirestoreValue};
Expand Down

0 comments on commit d2aca37

Please sign in to comment.