#postgresql #sql #sqlx #mysql #sqlite #proc-macro

macro supermodel-macros

Procedural macros for Supermodel, an abstract data-modeling library

1 unstable release

0.1.0 Feb 9, 2024

#68 in #postgres


Used in supermodel

Custom license

16KB
374 lines

supermodel

https://img.shields.io/crates/v/supermodel https://img.shields.io/crates/v/supermodel__macros https://img.shields.io/crates/v/supermodel__sqlx

Supermodel is an abstract data-modeling library. It provides an interface for creating, editing, and modifying data in various types of databases and APIs.

Goals

  • Be generic. The library tries be as abstract as possible. This allows the same code to be used for multiple purposes. Moreover, this means prioritizing the Rust type system over the type system of a database implementation.
  • Be intuitive. Supermodel is centered around the Model and Dialect trait. With an implementation of a Dialect (typically through a crate like supermodel-sqlx) and an implementation of a Model (typically through the model proc-macro), all of Supermodel's features can be accessed.
  • Be fast. While I would not consider myself skilled at high-performance Rust, this library aims to be only a thin layer over its implementations when it comes to runtime performance.

Examples

#[model(Postgres)]
#[name = "users"]
pub struct User {
    username: String,
    password: String
}

#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
    dotenv().expect("initializing .env file");

    let url = std::env::var("DATABASE_URL").expect("missing DATABASE_URL in env");
    let mut connection = Postgres::create_connection(&url).await?;

    User::register().execute(&mut connection).await?;

    Ok(())
}

Generated SQL:

create table if not exists users
(
    id       bigserial
             primary key,
    username varchar(30),
    password varchar(30)
);

Check out some more example programs written with Supermodel here.

Usability

Supermodel currently compiles, but is largely unusable. It only supports a handful of data types and many operations are unimplemented. Moreover, the API is very unstable as I haven't yet decided the best way to go about certain things.

Roadmap

Check out the plans for the future of this library here.

Dependencies

~305–750KB
~18K SLoC