firestore-structured-query

Crates.iofirestore-structured-query
lib.rsfirestore-structured-query
version
sourcesrc
created_at2024-01-29 22:48:46.944676
updated_at2025-02-13 12:19:24.146214
descriptionA Firestore StructuredQuery builder
homepage
repositoryhttps://github.com/bouzuya/firestore-structured-query
max_upload_size
id1119469
Cargo.toml error:TOML parse error at line 22, column 1 | 22 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
bouzuya (bouzuya)

documentation

README

firestore-structured-query

A Firestore StructuredQuery builder.

ci crates.io docs.rs license

Examples

use firestore_structured_query::{FieldPath, Filter, Query};
use googleapis_tonic_google_firestore_v1::google::firestore::v1::{
    structured_query, value::ValueType, ArrayValue, Cursor, StructuredQuery, Value,
};

let _ = StructuredQuery::from(
    // or Query::collection_group(...)
    Query::collection("collection_id1")
        .select([FieldPath::raw("field1"), FieldPath::raw("field2")])
        .r#where(Filter::and([
            // field filters
            FieldPath::raw("field1").less_than(Value { value_type: Some(ValueType::IntegerValue(1)) })?,
            FieldPath::raw("field2").less_than_or_equal(Value { value_type: Some(ValueType::IntegerValue(2)) })?,
            FieldPath::raw("field3").greater_than(Value { value_type: Some(ValueType::IntegerValue(3)) })?,
            FieldPath::raw("field4").greater_than_or_equal(Value { value_type: Some(ValueType::IntegerValue(4)) })?,
            FieldPath::raw("field5").equal(Value { value_type: Some(ValueType::IntegerValue(5)) })?,
            FieldPath::raw("field6").not_equal(Value { value_type: Some(ValueType::IntegerValue(6)) })?,
            FieldPath::raw("field7").array_contains(Value { value_type: Some(ValueType::IntegerValue(7)) })?,
            FieldPath::raw("field8").r#in(Value { value_type: Some(ValueType::ArrayValue(ArrayValue { values: vec![Value { value_type: Some(ValueType::IntegerValue(8)) }] })) })?,
            FieldPath::raw("field9").array_contains_any(Value { value_type: Some(ValueType::ArrayValue(ArrayValue { values: vec![Value { value_type: Some(ValueType::IntegerValue(9)) }] })) })?,
            FieldPath::raw("field10").not_in(Value { value_type: Some(ValueType::ArrayValue(ArrayValue { values: vec![Value { value_type: Some(ValueType::IntegerValue(10)) }] })) })?,
            // unary filters
            FieldPath::raw("field11").is_nan()?,
            FieldPath::raw("field12").is_not_nan()?,
            FieldPath::raw("field13").is_not_null()?,
            FieldPath::raw("field14").is_null()?,
            // composite filters
            Filter::and([
                FieldPath::raw("f").equal(Value { value_type: Some(ValueType::StringValue("a".to_string())) })?,
                FieldPath::raw("f").equal(Value { value_type: Some(ValueType::StringValue("b".to_string())) })?,
            ]),
            Filter::or([
                FieldPath::raw("f").equal(Value { value_type: Some(ValueType::StringValue("a".to_string())) })?,
                FieldPath::raw("f").equal(Value { value_type: Some(ValueType::StringValue("b".to_string())) })?,
            ]),
        ]))
        .order_by([
            FieldPath::raw("field1").ascending(),
            FieldPath::raw("field2").descending(),
        ])
        // .start_after(...)
        .start_at([
            Value { value_type: Some(ValueType::IntegerValue(1))},
            Value { value_type: Some(ValueType::IntegerValue(2))},
        ])
        // .end_before(...)
        .end_at([
            Value { value_type: Some(ValueType::IntegerValue(1))},
            Value { value_type: Some(ValueType::IntegerValue(2))},
        ])
        .offset(1_i32)
        .limit(2_i32),
);

// If "serde" feature is enabled.
use firestore_structured_query::to_value;
let _ = StructuredQuery::from(
    // or Query::collection_group(...)
    Query::collection("collection_id1")
        .select([FieldPath::raw("field1"), FieldPath::raw("field2")])
        .r#where(Filter::and([
            // field filters
            FieldPath::raw("field1").less_than(&1)?,
            FieldPath::raw("field2").less_than_or_equal(&2)?,
            FieldPath::raw("field3").greater_than(&3)?,
            FieldPath::raw("field4").greater_than_or_equal(&4)?,
            FieldPath::raw("field5").equal(&5)?,
            FieldPath::raw("field6").not_equal(&6)?,
            FieldPath::raw("field7").array_contains(&7)?,
            FieldPath::raw("field8").r#in(&[8])?,
            FieldPath::raw("field9").array_contains_any(&[9])?,
            FieldPath::raw("field10").not_in(&[10])?,
            // unary filters
            FieldPath::raw("field11").is_nan()?,
            FieldPath::raw("field12").is_not_nan()?,
            FieldPath::raw("field13").is_not_null()?,
            FieldPath::raw("field14").is_null()?,
            // composite filters
            Filter::and([
                FieldPath::raw("f").equal(&"a")?,
                FieldPath::raw("f").equal(&"b")?,
            ]),
            Filter::or([
                FieldPath::raw("f").equal(&"a")?,
                FieldPath::raw("f").equal(&"b")?,
            ]),
        ]))
        .order_by([
            FieldPath::raw("field1").ascending(),
            FieldPath::raw("field2").descending(),
        ])
        // .start_after(...)
        .start_at([
            to_value(&1)?,
            to_value(&2)?
        ])
        // .end_before(...)
        .end_at([
            to_value(&1)?,
            to_value(&2)?,
        ])
        .offset(1_i32)
        .limit(2_i32),
);

TODOs

  • StructuredQuery::find_nearest support
Commit count: 192

cargo fmt