Giter VIP home page Giter VIP logo

firestore-serde's Introduction

firestore-serde

wokflow state crates.io

firestore-serde is a serializer/deserializer implementation for Firestore Values and Documents.

This allows you to store arbitrary Rust values in a Firestore database, but this crate does not handle any of the communication with the Firestore service. Instead, it's meant to fit into a stack of other crates:

Preliminaries

Firestore is a cloud-based, proprietary document database from Google (not to be confused with Firebase or Cloud Datastore, which are also proprietary document databases from Google).

Google does not provide official Rust bindings for Google Cloud Platform, but they do provide API specifications for REST and gRPC APIs. As a result, a number of Rust projects have appeared to produce Rust bindings from these specifications:

There is a 1:1 mapping between REST API calls and gRPC API calls. Unfortunately, there is a known issue in the REST API spec for Firestore which breaks querying, so I recommend not using the REST API. As a consequence, this crate only supports the gRPC API. If you do want to use the REST API, see the firestore-db-and-auth-rs crate instead of this one.

The unit of data retrieval in Firestore is a Document, which is a map of string fields to Values. Values themselves are a rich type which can represent arrays and maps composed of other values. As such, we can represent many Rust types in an intuitive way as both Values and Documents. This crate provides a serde serializer and deserializer to do just that.

Usage

This crate provides four primary functions:

// Conversions between Rust types and Value gRPC type.

pub fn to_grpc_value<T>(value: &T)
    -> Result<Value, SerializationError>
    where T: Serialize;

pub fn from_grpc_value<T>(value: &Value)
    -> Result<T, DeserializationError>
    where T: DeserializeOwned;

// Conversions between Rust types and Document gRPC type.

pub fn to_document<T>(value: &T)
    -> Result<Document, SerializationError>
    where T: Serialize;

pub fn from_document<T>(document: Document)
    -> Result<T, DeserializationError>
    where T: DeserializeOwned;

Note that the from_document takes ownership of its argument, so if you need the original Document after conversion you will have to clone it.

Timestamps

The chrono crate supports serializable timestamps, by converting the timestamp to a string or number. Without intervention, firestore-serde doesn't differentiate between these and other numbers or strings, so they are turned into ValueType::IntegerValue and ValueType::StringValue respectively.

This is fine if you always deserialize the values to Rust, but if you want to access the data in other ways (for example, the web-based data console), it's often useful to store time data in Firestore's ValueType::TimestampValue. To do this, add firestore-serde-timestamp as a dependency and tell Serde to use it as the encoding:

use serde::{Serialize, Deserialize};
use chrono::{DateTime, Utc};

#[derive(Serialize, Deserialize)]
struct MyStruct {
    #[serde(with="firestore_serde_timestamp::timestamp")]
    some_timestamp: DateTime<Utc>,
}

API versions

There are currently two versions of the gRPC API, google.firestore.v1.* and google.firestore.v1beta1.*. Each version is represented by a different namespace which is isolated from the other, so even types which are used in common by both (e.g. Document and Value) are represented by different protocol buffers.

firestore-serde uses the v1 namespace by default, but it is possible to change that with a feature flag. In your Cargo.toml, specify the dependency as follows:

[dependencies]
firestore-serde = {version = "0.1.0", default-features=false, features=["google-firestore-v1beta1"]}

It is important that you disable default-features, because firestore-serde will refuse to compile if both google-firestore-v1 and google-firestore-v1beta1 are enabled.

firestore-serde's People

Contributors

paulgb avatar thrykol avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

firestore-serde's Issues

impl From<DeserializationError>

Hello ๐Ÿ‘‹

I'd like to be able to do this:

impl From<firestore_serde::deserialize::error::DeserializationError> for MyErrorType {
    fn from(value: firestore_serde::deserialize::error::DeserializationError) -> Self {
        todo!()
    }
}

But it seems like I can't ๐Ÿค”

error[E0603]: module `deserialize` is private
   --> src/error.rs
    |
125 |     fn from(value: firestore_serde::deserialize::error::DeserializationError) -> Self {
    |                                     ^^^^^^^^^^^ private module
    |

Any pointers would be appreciated, thanks for this crate!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.