welds

Crates.iowelds
lib.rswelds
version0.4.8
sourcesrc
created_at2023-04-13 01:03:53.698865
updated_at2024-11-22 12:14:55.539347
descriptionAn async ORM for (postgres, mssql, mysql, sqlite)
homepage
repositoryhttps://github.com/weldsorm/welds
max_upload_size
id837757
size347,265
(lex148)

documentation

README

An async ORM written in rust using sqlx and/or Tiberius.

Welds

Welds is an async ORM written in rust using sqlx and/or Tiberius.

Features

  • Async for all.
  • Support for multiple SQL databases (Mssql, MySql, Postgres, Sqlite)
  • Written for ease of development. Features aren't hidden behind traits. Code should be simple to write, and simple to read.
  • Low level connection always available when you need to drop down to raw SQL.

Under the hood welds uses:

  • sqlx for Postgres, MySql, and Sqlite.
  • Tiberius for MSSQL

Compatibility:

  • the 0.4.* line of welds is compiled with sqlx 0.8
  • the 0.3.* line of welds is compiled with sqlx 0.7

Example Setup

#[derive(Debug, WeldsModel)]
#[welds(schema= "inventory", table = "products")]
#[welds(BelongsTo(seller, super::people::People, "seller_id"))]
pub struct Product {
    #[welds(rename = "product_id")]
    #[welds(primary_key)]
    pub id: i32,
    pub name: String,
    pub seller_id: Option<i32>,
    pub description: Option<String>,
    pub price: Option<f32>,
}

Example Usage

Basic Select

  let url = "postgres://postgres:password@localhost:5432";
  let client = welds::connections::postgres::connect(url).await.unwrap();

  let products = Product::where_col(|p| p.price.equal(3.50)).run(&client).await?;

Basic Filter Across tables

  let client = welds::connections::mssql::connect(url).await.unwrap();

  let sellers = Product::where_col(|product| product.price.equal(3.50))
        .map_query(|product| product.seller )
        .where_col(|seller| seller.name.ilike("%Nessie%") )
        .run(&client).await?;

Create And Update

  let client = welds::connections::sqlite::connect(url).await.unwrap();

  let mut cookies = Product::new();
  cookies.name = "cookies".to_owned();
  // Creates the product cookie
  cookies.save.await(&client)?;
  cookies.description = "Yum".to_owned();
  // Updates the Cookies
  cookies.save.await(&client)?;

Types from external crates

Both Tiberius and sqlx support types from external create such at chrono and serde_json. These types need to be enabled in the underlying crate to use. In order to use types that are external the appropriate feature needs to be enabled in these underlying frameworks. We have chosen to leave this up to you as the developer so you have full control over your underlying SQLX/Tiberius setup.

In order to get these types to work you will need to:

  1. Add the external create cargo add chrono
  2. Enable the feature in the underlying SQL framework. cargo add sqlx --features=chrono
  3. (Tiberius only) enable the corresponding feature for welds-connections feature cargo add welds-connections --features=mssql,mssql-chrono

welds-connections features needed for mssql (tiberius):

  • mssql-chrono

  • mssql-time

  • mssql-rust_decimal

  • mssql-bigdecimal

Other Examples

For more good examples check out the examples repo.

Commit count: 225

cargo fmt