welds - 新的高性能异步ORM


welds's logo

功能介绍

使用介绍

创建rust结构体

#[derive(Debug, sqlx::FromRow, WeldsModel)]
#[welds(db(Postgres))]
//#[welds(db(Postgres, Mssql, Mysql, Sqlite))]
#[welds(schema= "inventory", table = "products")]
#[welds(BelongsTo(seller, super::people::People, "seller_id"))]
pub struct Product {
    #[sqlx(rename = "product_id")]
    #[welds(primary_key)]
    pub id: i32,
    pub name: String,
    pub seller_id: Option,
    pub description: Option,
    pub price: Option,
}

语句执行

  1. 基础查询
let url = "postgres://postgres:password@localhost:5432";
let pool = welds::connection::connect_postgres(url).await.unwrap();

let products = Product::where_col(|p| p.price.equal(3.50)).run(&pool).await?;
  1. 按照条件进行查询
let conn = welds::connection::connect_mssql(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(&conn).await?;
  1. 创建和更新
let conn = welds::connection::connect_sqlite(url).await.unwrap();

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

CRUD生成工具

  1. 执行安装命令cargo install welds-cli --version '0.1.6-alpha'
  2. 生成数据库表定义,首先定义数据库链接export DATABASE_URL=postgres://postgres:password@localhost:5432
  3. 执行命令welds update,生成welds.yaml文件,格式如下:
tables:
- schema: public
  name: orders
  manual_update: false
  model: null
  type: table
  columns:
  - db_name: id
    db_type: int8
    model_name: id
    is_null: false
    primary_key: true
    writeable: true
  - ...
  1. 最后执行命令welds generate,生成rust代码
展开阅读全文

页面更新:2024-02-17

标签:低级   抽象   语句   接口   命令   多种   定义   条件   高级   数据库

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top