Dependencies
= { = "<version>", = ["postgres"] }
= { = "1.0" } # Mainly to adapt to the entity code generated by sea-orm-cli
You can replace postgres
, mysql
, sqlite
feature to select the appropriate database driver.
optional features: with-web
.
Configuration items
[]
= "postgres://root:123456@localhost:5432/pg_db" # Database address
= 1 # Minimum number of connections in the connection pool, the default value is 1
= 10 # Maximum number of connections in the connection pool, the default value is 10
= 30000 # Connection timeout, in milliseconds, default 30s
= 600000 # Connection idle time, in milliseconds, default 10min
= 1800000 # Maximum connection survival time, in milliseconds, default 30min
= 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 = 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 ;
use get;
use Component;
use Result;
use Context;
async
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.
= { = "<version>", = ["postgres", "with-web"] }
The configuration is as follows:
# sea-orm-web configuration
[]
= false # 1-based index, closed by default
= 2000 # Maximum supported page size, to avoid OOM caused by server attacks, default value 2000
= 20 # Default page size, 20
Use as follows:
async
For the complete code, please refer to sea-orm-example