firestore-sdk

Examples

create

```rust use firestoresdk::{db, FireStore, State, storefield, store_field::Value};

let projectid = "YOURPROJECTID"; let token = "YOURTOKEN";

let conn = db().init(project_id, Some(token));

let fields = storefield::tovalues() .add("name", Value::string("Alice") .add("age", Value::integer(20)) .get_fields();

let res = conn .collection("users") .add_document(fields) .await?; ```

create or update

```rust use firestoresdk::{db, FireStore, State, storefield, store_field::Value};

let projectid = "YOURPROJECTID"; let token = "YOURTOKEN";

let conn = db().init(project_id, Some(token));

let documentid = "exampleid";

let fields = storefield::tovalues() .add("name", Value::string("Bob") .add("age", Value::integer(23)) .get_fields();

let res = conn .collection("users") .document(documentid) .setdocument(fields) .await?; ```

delete

```rust use firestore_sdk::{db, FireStore, State};

let projectid = "YOURPROJECTID"; let token = "YOURTOKEN";

let conn = db().init(project_id, Some(token));

let documentid = "exampleid"

let res = conn .collection("users") .document(document_id) .delete() .await?; ```

get one

```rust use firestore_sdk::{db, FireStore, State};

let projectid = "YOURPROJECTID"; let token = "YOURTOKEN";

let conn = db().init(project_id, Some(token));

let documentid = "exampleid"

let res = conn .collection("users") .document(document_id) .await?; ```

find

```rust use firestoresdk::{ db, FireStore, State, Direction, Operator, firestoregrpc::v1::value::ValueType };

let projectid = "YOURPROJECTID"; let token = "YOURTOKEN";

let conn = db().init(project_id, Some(token));

let res = conn .collection("users") .orderby("age", Direction::ASCENDING) .wherefield( "age", Operator::LESSTHAN, ValueType::IntegerValue(20), ) .getdocuments() .await?; ```

find (start couser)

```rust use firestoresdk::{ db, FireStore, State, Direction, Operator, firestoregrpc::v1::value::ValueType };

let projectid = "YOURPROJECTID"; let token = "YOURTOKEN";

let conn = db().init(project_id, Some(token));

let documentid = "exampleid"

let beforelast = db() .collection("users") .document(documentid) .get_document() .await?;

let res = db() .collection("foo") .startafter(beforelast) .limit(10) .get_documents() .await?; ```

get document to struct

```rust use chrono::NaiveDateTime; use serde::{Deserialize, Serialize};

[derive(Debug, Serialize, Deserialize)]

struct User { id: String, name: String, age: i32, createdat: NaiveDateTime, updatedat: NaiveDateTime, }

fn documenttouser(document: &Document) -> Result { let v: Vec<&str> = document.name.rsplit('/').collect(); let uid = (*v.first().unwrap()).to_string();

let created_time = &document.create_time.as_ref().unwrap();
let created_at =
    NaiveDateTime::from_timestamp(created_time.seconds, created_time.nanos.try_into().unwrap());

let updated_time = &document.update_time.as_ref().unwrap();
let updated_at =
    NaiveDateTime::from_timestamp(updated_time.seconds, updated_time.nanos.try_into().unwrap());

let values = store_field::from_values(document.fields.clone());

let user = User {
    uid,
    name: values.clone().get_string("name"),
    age: values.clone().get_string("age"),
    created_at,
    updated_at,
};

Ok(user)

} ```