#protobuf #database-schema #schema #macro

awto

Awtomate your 🦀 microservices with awto

3 releases

0.1.2 Oct 21, 2021
0.1.1 Oct 18, 2021
0.1.0 Oct 17, 2021

#85 in #database-schema


Used in awto-compile

MIT/Apache

23KB
592 lines

awto

Awtomate your 🦀 microservices with awto

crate

What is awto?

Awto treats your rust project as the source of truth for microservices, and generates database tables and protobufs based on your schema and service.

Database Generation

  • Database tables are generated from your Rust structs
  • Changes to your structs are detected and will modify the table schema accordingly

Protobuf Generation

  • Generate protobuf schemas from your models
  • Compile a protobuf server from your app's service

Concepts

With awto, you create a Cargo workspace for each microservice. Under your workspace, you create two libs: schema and service

Schema

The schema lib's purpose is to define your microservice models which will be used to generate database tables.

// schema/src/lib.rs
use awto::prelude::*;

schema! {
    #[database_table]
    #[protobuf_message]
    pub struct Product {
        pub id: Uuid,
        pub created_at: DateTime<FixedOffset>,
        pub updated_at: DateTime<FixedOffset>,
        pub name: String,
        #[awto(max_len = 120)]
        pub description: Option<String>,
        #[awto(default = 0)]
        pub price: i64,
    }
}

See example schema in examples/ecom.

Service

The service lib is where you write your business logic. This business logic can later be used to create a protobuf API (and in the future a graphql API).

// service/src/lib.rs

register_services!(ProductService);

pub struct ProductService {
    pub conn: DatabaseConnection,
}

#[protobuf_service]
impl ProductService {
    pub async fn find_by_id(&self, request: ProductId) -> Result<Product, Status> {
        let product = product::Entity::find_by_id(request.id)
            .one(&self.conn)
            .await
            .map_err(|err| Status::internal(err.to_string()))?
            .ok_or_else(|| Status::not_found("product not found"))?;

        Ok(product.into())
    }
}

See example service in examples/ecom.

Cargo workspace

Your schema and service libs should be under a cargo workspace.

# root Cargo.toml
[workspace]
members = ["schema", "service"]

See example project in examples/ecom.

Awto CLI

Awto provides a cli for generating additional libraries with the awto compile <lib> command. Currently the available libraries are:

  • database - based on SeaORM, provides a database library for use in your service
  • protobuf - based on tonic, provides a protobuf server library

Install

The cli can be installed with:

cargo install awto-cli

This will provide a binary called awto.

Check installation with awto --help.

Compile library

To compile a library, you can run:

awto compile <output>

The available outputs currently are:

  • database - syncs your database with your schema and generates a lib for performing operations with the database via SeaORM.
  • protobuf - generates a protobuf file and lib which can be used as a protobuf server & client via tonic.

Roadmap

Awto is still in alpha stages and is made mostly as an experiment at this point. If it gets some attention, serious effort will be put into it to make it a meaningful tool for the Rust community.

Contributing

Whether you want to share ideas, bugs, suggestions, or other, your contributions to this project are welcomed 🤌

License

By contributing, you agree that your contributions will be licensed under this repository's MIT or Apache-2.0 License.

Dependencies

~8–15MB
~164K SLoC