spring-sea-orm

Crates.iospring-sea-orm
lib.rsspring-sea-orm
version0.3.0
sourcesrc
created_at2024-08-07 04:50:36.796105
updated_at2024-12-01 03:19:05.089248
descriptionIntegration of spring-rs framework and sea-orm
homepage
repositoryhttps://github.com/spring-rs/spring-rs
max_upload_size
id1328075
size21,279
holmofy (holmofy)

documentation

README

crates.io Documentation

Dependencies

spring-sea-orm = { version = "<version>", features = ["postgres"] }
sea-orm = { version = "1.0" }          # Mainly to adapt to the entity code generated by sea-orm-cli

You can replace postgres, mysql, sqlitefeature to select the appropriate database driver.

optional features: with-web.

Configuration items

[sea-orm]
uri = "postgres://root:123456@localhost:5432/pg_db" # Database address
min_connections = 1        # Minimum number of connections in the connection pool, the default value is 1
max_connections = 10       # Maximum number of connections in the connection pool, the default value is 10
acquire_timeout = 30000    # Connection timeout, in milliseconds, default 30s
idle_timeout = 600000      # Connection idle time, in milliseconds, default 10min
connect_timeout = 1800000  # Maximum connection survival time, in milliseconds, default 30min
enable_logging = true      # Print sql log

Components

After configuring the above configuration items, the plugin will automatically register a DbConn connection pool object. This object is an alias of sea_orm::DbConn.

pub type DbConn = sea_orm::DbConn;

Model generation

sea-orm-cli provides a great model code generation function. You only need to define the table structure in the database, and after a simple configuration, the model code corresponding to the database structure can be generated, which can save a lot of code writing work.

Extract the Component registered by the plugin

The SeaOrmPlugin plugin automatically registers a connection pool component for us. We can use Component to extract this connection pool from AppState. Component is an axum extractor.

use spring_sqlx::{sqlx::{self, Row}, ConnectPool};
use spring_web::get;
use spring_web::extractor::Component;
use spring_web::error::Result;
use anyhow::Context;

#[get("/:id")]
async fn get_todo_list(
    Component(db): Component<DbConn>,
    Path(id): Path<i32>
) -> Result<String> {
    let rows = TodoItem::find()
        .filter(todo_item::Column::ListId.eq(id))
        .all(&db)
        .await
        .context("query todo list failed")?;
    Ok(Json(rows))
}

Pagination support

spring-sea-orm extends SeaOrm's Select with the PaginationExt feature.

In addition, web pagination parameter parsing is also provided. Just add the with-web function to the dependency.

spring-sea-orm = { version = "<version>", features = ["postgres", "with-web"] }

The configuration is as follows:

# sea-orm-web configuration
[sea-orm-web]
one_indexed = false     # 1-based index, closed by default
max_page_size = 2000    # Maximum supported page size, to avoid OOM caused by server attacks, default value 2000
default_page_size = 20  # Default page size, 20

Use as follows:

#[get("/")]
async fn get_todo_list(
    Component(db): Component<DbConn>,
    Query(query): Query<TodoListQuery>,
    pagination: Pagination,
) -> Result<impl IntoResponse> {
    let rows = TodoList::find()
        .filter(query)
        .page(&db, pagination)
        .await
        .context("query todo list failed")?;
    Ok(Json(rows))
}

For the complete code, please refer to sea-orm-example

Commit count: 523

cargo fmt