#sqlite #orm #query-builder #drizzle

rizzle

A rust查询构建器和模式生成器。不要称其为ORM

5个版本

0.1.4 2023年9月20日
0.1.3 2023年9月20日
0.1.2 2023年9月2日
0.1.1 2023年9月2日
0.1.0 2023年8月31日

#709数据库接口

每月29次下载

MIT 许可证

60KB
1.5K SLoC

rizzle [wip]

rizzle是一个sqlite(*postgres将在某个时候到来)用于rust的自动迁移生成器和查询构建器!

可能或可能不是受到 drizzle 的启发

安装

cargo add rizzle

声明您的模式并连接到数据库

use rizzle::prelude::*;

#[derive(Table, Clone, Copy)]
#[rizzle(table = "posts")]
struct Posts {
  #[rizzle(primary_key)]
  id: Integer,

  #[rizzle(not_null)]
  body: Text
}

#[derive(Table, Clone, Copy)]
#[rizzle(table = "comments")]
struct Comments {
    #[rizzle(primary_key)]
    id: Integer,

    #[rizzle(not_null)]
    body: Text,

    #[rizzle(references = "posts(id)")]
    post_id: Integer,
}

#[derive(RizzleSchema, Clone, Copy)]
struct Schema {
    posts: Posts,
    comments: Comments
}

#[tokio::main]
async fn main() -> Result<(), RizzleError> {
    let options = DatabaseOptions::new("sqlite://:memory:");
    let schema = Schema::new();
    let db = rizzle(options, schema).await?;
    Ok(())
}

插入、更新和删除行

use rizzle::prelude::*;

#[derive(Row)]
struct Post {
  id: i64,
  body: String
}

#[tokio::main]
async fn main() -> Result<(), RizzleError> {
    let options = DatabaseOptions::new("sqlite://:memory:");
    let schema = Schema::new();
    let db = rizzle(options, schema).await?;
    let Schema { posts, .. } = schema;

    // insert into posts (id, body) values (?, ?) returning *
    let inserted_post: Post = db
        .insert(posts)
        .values(Post {
            id: 1,
            body: "".to_owned(),
        })
        .returning()
        .await?;

    // update posts set body = ?, id = ? where id = ?
    let rows_affected = db
        .update(posts)
        .set(Post {
            body: "post".to_owned(),
            ..inserted_post
        })
        .where(eq(posts.id, 1))
        .rows_affected()
        .await?;

    // delete from posts where id = ? returning *
    let deleted_post = db.delete(posts).where(eq(posts.id, 1)).returning().await?;

    Ok(())
}

使用 * 选择行

use rizzle::prelude::*;

#[derive(Row)]
struct Comment {
    id: i64,
    body: String,
    post_id: i64
}

#[tokio::main]
async fn main() -> Result<(), RizzleError> {
    let options = DatabaseOptions::new("sqlite://:memory:");
    let schema = Schema::new();
    let db = rizzle(options, schema).await?;
    let Schema { comments, .. } = schema;

    // select * from comments
    let rows: Vec<Comment> = db.select().from(comments).all().await;

    Ok(())
}

选择特定列

use rizzle::prelude::*;

#[derive(Select, Default)]
struct PartialComment {
  body: String
}

#[tokio::main]
async fn main() -> Result<(), RizzleError> {
    let options = DatabaseOptions::new("sqlite://:memory:");
    let schema = Schema::new();
    let db = rizzle(options, schema).await?;
    let Schema { comments, .. } = schema;
    let partial_comment = PartialComment::default();

    // select body from comments
    let partial_rows: Vec<PartialComment> = db.select_with(partial_comment).from(comments).all().await;

    Ok(())
}

连接

use rizzle::prelude::*;

#[tokio::main]
async fn main() -> Result<(), RizzleError> {
    let options = DatabaseOptions::new("sqlite://:memory:");
    let schema = Schema::new();
    let db = rizzle(options, schema).await?;
    let Schema { comments, posts } = schema;

    // select * from comments inner join posts on posts.id = comments.post_id
    let rows: Vec<Comment> = db
        .select()
        .from(comments)
        .inner_join(posts, on(posts.id, comments.post_id))
        .all()
        .await;

    Ok(())
}

预处理语句

use rizzle::prelude::*;

#[tokio::main]
async fn main() -> Result<(), RizzleError> {
    let options = DatabaseOptions::new("sqlite://:memory:");
    let schema = Schema::new();
    let db = rizzle(options, schema).await?;
    let Schema { comments, .. } = schema;

    // select * from comments
    let query = db.select().from(comments);

    // prepare the query store it in a once lock or something
    let prepared = query.prepare_as::<Comment>();

    // execute the prepared query later
    let rows = prepared.all().await?;

    Ok(())
}

依赖项

~33–45MB
~781K SLoC