Skip to content

Commit 878dec6

Browse files
committed
[SeaORM] Pin 1.0.x docs
1 parent c6c5bf1 commit 878dec6

File tree

70 files changed

+5676
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+5676
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Index
2+
3+
## Introduction
4+
5+
1. Introduction
6+
7+
1.1. [What is an ORM](01-introduction/01-orm.md)
8+
9+
1.2. [Async Programming in Rust](01-introduction/02-async.md)
10+
11+
1.3. [SeaORM Concepts](01-introduction/03-sea-orm.md)
12+
13+
1.4. [Tutorial & Examples](01-introduction/04-tutorial.md)
14+
15+
## Basics
16+
17+
2. Installation & Configuration
18+
19+
2.1 [Choosing a Database & Async Runtime](02-install-and-config/01-database-and-async-runtime.md)
20+
21+
2.2 [Connection Pool](02-install-and-config/02-connection.md)
22+
23+
2.3 [Debug Log](02-install-and-config/03-debug-log.md)
24+
25+
3. Migration
26+
27+
3.1 [Setting Up Migration](03-migration/01-setting-up-migration.md)
28+
29+
3.2 [Writing Migration](03-migration/02-writing-migration.md)
30+
31+
3.3 [Running Migration](03-migration/03-running-migration.md)
32+
33+
3.4 [Seeding Data](03-migration/04-seeding-data.md)
34+
35+
4. Generating Entities
36+
37+
4.1 [Using `sea-orm-cli`](04-generate-entity/01-sea-orm-cli.md)
38+
39+
4.2 [Entity Structure](04-generate-entity/02-entity-structure.md)
40+
41+
4.3 [Expanded Entity Structure](04-generate-entity/03-expanded-entity-structure.md)
42+
43+
4.4 [Enumeration](04-generate-entity/04-enumeration.md)
44+
45+
4.5 [New Type](04-generate-entity/05-newtype.md)
46+
47+
5. Basic CRUD
48+
49+
5.1 [Basic Schema](05-basic-crud/01-basic-schema.md)
50+
51+
5.2 [SELECT: find, filter, sort, paging](05-basic-crud/02-select.md)
52+
53+
5.3 [INSERT: Model & ActiveModel, insert many](05-basic-crud/03-insert.md)
54+
55+
5.4 [UPDATE: find & save, update many](05-basic-crud/04-update.md)
56+
57+
5.5 [SAVE: insert or update](05-basic-crud/05-save.md)
58+
59+
5.6 [DELETE: delete one & delete many](05-basic-crud/06-delete.md)
60+
61+
5.7 [JSON](05-basic-crud/07-json.md)
62+
63+
5.8 [Raw SQL query](05-basic-crud/08-raw-sql.md)
64+
65+
## Advanced Topics
66+
67+
6. Relations
68+
69+
6.1 [One to One](06-relation/01-one-to-one.md)
70+
71+
6.2 [One to Many](06-relation/02-one-to-many.md)
72+
73+
6.3 [Many to Many](06-relation/03-many-to-many.md)
74+
75+
6.4 [Chained Relations](06-relation/04-chained-relations.md)
76+
77+
6.5 [Self Referencing](06-relation/05-self-referencing.md)
78+
79+
6.6 [Custom Join Condition](06-relation/06-custom-join-condition.md)
80+
81+
6.7 [Data Loader](06-relation/07-data-loader.md)
82+
83+
6.8 [Bakery Schema](06-relation/08-bakery-schema.md)
84+
85+
7. Writing Tests
86+
87+
7.1 [Robust & Correct](07-write-test/01-testing.md)
88+
89+
7.2 [Mock Interface](07-write-test/02-mock.md)
90+
91+
7.3 [Using SQLite](07-write-test/03-sqlite.md)
92+
93+
8. Advanced Queries
94+
95+
8.1 [Custom select](08-advanced-query/01-custom-select.md)
96+
97+
8.2 [Conditional expressions](08-advanced-query/02-conditional-expression.md)
98+
99+
8.3 [Aggregate functions](08-advanced-query/03-aggregate-function.md)
100+
101+
8.4 [Custom Joins](08-advanced-query/04-custom-joins.md)
102+
103+
8.5 [Sub Query](08-advanced-query/05-subquery.md)
104+
105+
8.6 [Transaction](08-advanced-query/06-transaction.md)
106+
107+
8.7 [Streaming](08-advanced-query/07-streaming.md)
108+
109+
8.8 [Custom Active Model](08-advanced-query/08-custom-active-model.md)
110+
111+
8.9 [Error Handling](08-advanced-query/09-error-handling.md)
112+
113+
9. Schema Statement
114+
115+
9.1 [Create Table](09-schema-statement/01-create-table.md)
116+
117+
9.2 [Create Enum](09-schema-statement/02-create-enum.md)
118+
119+
9.3 [Create Index](09-schema-statement/03-create-index.md)
120+
121+
10. Seaography
122+
123+
10.1 [🧭 Seaography Intro](10-seaography/01-seaography-intro.md)
124+
125+
10.2 [Getting Started](10-seaography/02-getting-started.md)
126+
127+
10.3 [Looking Forward](10-seaography/03-looking-forward.md)
128+
129+
11. Internal Design
130+
131+
11.1 [Traits and Types](11-internal-design/01-trait-and-type.md)
132+
133+
11.2 [Derive Macros](11-internal-design/02-derive-macro.md)
134+
135+
11.3 [Compare with Diesel](11-internal-design/03-diesel.md)
136+
137+
11.4 [Architecture](11-internal-design/04-architecture.md)
138+
139+
12. What's Next
140+
141+
12.1 [What's Next](12-whats-next/01-whats-next.md)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# What is an ORM
2+
3+
An Object Relational Mapper (ORM) is a programming library to help you interact with a relational database from an Object-Oriented Programming (OOP) language.
4+
5+
Tables and columns in a database are mapped to objects and attributes, while additional methods allow you to load and store data from and to the database.
6+
7+
Services built in Rust are lightweight (small binary size, low memory usage), safe (with compile-time guarantee), correct (if unit tests are well-designed), and fast (compile-time optimizations minimize runtime overhead).
8+
9+
Due to Rust being a static, strongly typed, compiled, thread-safe, non-garbage-collected, and unconventional object-oriented language, working with an ORM in Rust is a bit different from other scripting languages you are already familiar with.
10+
11+
SeaORM tries to help you in reaping the above benefits while avoiding the hiccups when programming in Rust.
12+
13+
Let's get started.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Async Programming
2+
3+
Async programming in Rust is a recent development, only having been stabilized in Rust [`1.39`](https://github.com/rust-lang/rust/releases/tag/1.39.0). The async ecosystem is rapidly evolving, and SeaORM is one of the first crates built from the ground up with async support in mind.
4+
5+
The first thing to learn is the [`Future`](https://rust-lang.github.io/async-book/02_execution/02_future.html) trait. It's a placeholder for a function that will compute and return some value in the future. It's lazy, meaning `.await` must be called for any actual work to be done. `Future` allows us to achieve concurrency with little programming effort, e.g. [`future::join_all`](https://docs.rs/futures/latest/futures/future/fn.join_all.html) to execute multiple queries in parallel.
6+
7+
Second, `async` in Rust is [multi-threaded programming](https://rust-lang.github.io/async-book/03_async_await/01_chapter.html) with syntactic sugar. A `Future` may move between threads, so any variables used in async bodies must be able to travel between threads, i.e. [`Send`](https://doc.rust-lang.org/nomicon/send-and-sync.html).
8+
9+
Third, there are multiple async runtimes in Rust. [`async-std`](https://crates.io/crates/async-std) and [`tokio`](https://crates.io/crates/tokio) are the two most widely used. SeaORM's underlying driver, [`SQLx`](https://crates.io/crates/sqlx), supports both.
10+
11+
Knowing these concepts is essential to get you up and running in async Rust.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# SeaORM Concepts
2+
3+
In SeaORM, a database with a collection of tables is called a `Schema`.
4+
5+
Each table corresponds to an [`Entity`](04-generate-entity/02-entity-structure.md#entity) in SeaORM, which helps you perform `CRUD` (Create, Read, Update, and Delete) operations on relevant tables.
6+
7+
The `Entity` trait provides an API for you to inspect its properties ([`Column`](04-generate-entity/02-entity-structure.md#column), [`Relation`](04-generate-entity/02-entity-structure.md#relation) and [`PrimaryKey`](04-generate-entity/02-entity-structure.md#primary-key)) at runtime.
8+
9+
Each table has multiple columns, which are referred to as `attribute`.
10+
11+
These attributes and their values are grouped in a Rust struct (a [`Model`](04-generate-entity/03-expanded-entity-structure.md#model)) so that you can manipulate them.
12+
13+
However, `Model` is for read operations only. To perform insert, update, or delete, you need to use [`ActiveModel`](04-generate-entity/03-expanded-entity-structure.md#active-model) which attaches meta-data on each attribute.
14+
15+
Finally, there is no singleton (global context) in SeaORM. Application code is responsible for managing the ownership of the `DatabaseConnection`. We do provide integration examples for web frameworks including [Rocket](https://github.com/SeaQL/sea-orm/tree/master/examples/rocket_example), [Actix](https://github.com/SeaQL/sea-orm/tree/master/examples/actix_example), [axum](https://github.com/SeaQL/sea-orm/tree/master/examples/axum_example) and [poem](https://github.com/SeaQL/sea-orm/tree/master/examples/poem_example) to help you get started quickly.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Tutorial & Examples
2+
3+
If you prefer a step-by-step tutorial on how to use SeaORM, you can check out our official [SeaORM Tutorial](https://www.sea-ql.org/sea-orm-tutorial/). There are also some good tutorials written by [the community](https://github.com/SeaQL/sea-orm/blob/master/COMMUNITY.md#learning-resources)!
4+
5+
You can also check out [SeaORM Cookbook](https://www.sea-ql.org/sea-orm-cookbook/) for some frequently asked questions and recommended practices of using SeaORM.
6+
7+
If you are so eager and want something grab-and-go, SeaQL maintains a set of official examples contributed by the community (we welcome more!):
8+
9+
+ [Actix Example](https://github.com/SeaQL/sea-orm/tree/master/examples/actix_example)
10+
+ [Axum Example](https://github.com/SeaQL/sea-orm/tree/master/examples/axum_example)
11+
+ [GraphQL Example](https://github.com/SeaQL/sea-orm/tree/master/examples/graphql_example)
12+
+ [jsonrpsee Example](https://github.com/SeaQL/sea-orm/tree/master/examples/jsonrpsee_example)
13+
+ [Loco Example](https://github.com/SeaQL/sea-orm/tree/master/examples/loco_example)
14+
+ [Loco REST Starter Example](https://github.com/SeaQL/sea-orm/tree/master/examples/loco_starter)
15+
+ [Poem Example](https://github.com/SeaQL/sea-orm/tree/master/examples/poem_example)
16+
+ [Rocket Example](https://github.com/SeaQL/sea-orm/tree/master/examples/rocket_example)
17+
+ [Salvo Example](https://github.com/SeaQL/sea-orm/tree/master/examples/salvo_example)
18+
+ [Tonic Example](https://github.com/SeaQL/sea-orm/tree/master/examples/tonic_example)
19+
+ [Seaography Example](https://github.com/SeaQL/sea-orm/tree/master/examples/seaography_example)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"label": "Introduction",
3+
"collapsed": false
4+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Database & Async Runtime
2+
3+
:::caution We need your support! ⭐
4+
Thank you for using SeaORM. Please star our [GitHub repo](https://github.com/SeaQL/sea-orm)! Your support is vital to the continued development and maintenance of SeaORM.
5+
:::
6+
7+
First, add `sea-orm` to the `[dependencies]` section of `Cargo.toml`.
8+
9+
```toml title="Cargo.toml"
10+
sea-orm = { version = "1.0.0-rc.5", features = [ <DATABASE_DRIVER>, <ASYNC_RUNTIME>, "macros" ] }
11+
```
12+
13+
You must choose a `DATABASE_DRIVER` and an `ASYNC_RUNTIME`. `macros` is needed if you use SeaORM's generated entities (most likely).
14+
15+
## DATABASE_DRIVER
16+
17+
You can choose one or more from:
18+
19+
+ `sqlx-mysql` - SQLx MySQL and MariaDB
20+
+ `sqlx-postgres` - SQLx PostgreSQL
21+
+ `sqlx-sqlite` - SQLx SQLite
22+
23+
See also: [SQLx docs](https://docs.rs/crate/sqlx/latest/features).
24+
25+
:::tip SQL Server (MSSQL) backend
26+
27+
The installation and configuration of MSSQL driver can be found [here](https://www.sea-ql.org/SeaORM-X/docs/install-and-config/database-and-async-runtime/).
28+
29+
:::
30+
31+
## ASYNC_RUNTIME
32+
33+
You have to choose one from:
34+
35+
`runtime-async-std-native-tls`, `runtime-tokio-native-tls`, `runtime-async-std-rustls`, `runtime-tokio-rustls`
36+
37+
Basically, they are in the form of `runtime-ASYNC_RUNTIME-TLS_LIB`:
38+
39+
+ `ASYNC_RUNTIME` can be [`async-std`](https://crates.io/crates/async-std) or [`tokio`](https://crates.io/crates/tokio)
40+
+ `TLS_LIB` can either be [`native-tls`](https://crates.io/crates/native-tls) or [`rustls`](https://crates.io/crates/rustls)
41+
42+
1. Choose the ASYNC_RUNTIME corresponding to your Rust web framework:
43+
44+
| ASYNC_RUNTIME | Web Framework |
45+
| :-----------: | :------------: |
46+
| `async-std` | [`Tide`](https://docs.rs/tide) |
47+
| `tokio` | [`Axum`](https://docs.rs/axum), [`Actix`](https://actix.rs/), [`Poem`](https://docs.rs/poem), [`Rocket`](https://rocket.rs/) |
48+
49+
2. `native-tls` uses the platform's native security facilities, while `rustls` is an (almost) pure Rust implementation.
50+
51+
## Extra features
52+
53+
+ `debug-print` - print every SQL statement to logger
54+
+ `mock` - mock interface for unit testing
55+
+ `macros` - procedural macros for your convenient
56+
+ `with-chrono` - support [`chrono`](https://crates.io/crates/chrono) types
57+
+ `with-time` - support [`time`](https://crates.io/crates/time) types
58+
+ `with-json` - support [`serde-json`](https://crates.io/crates/serde-json) types
59+
+ `with-rust_decimal` - support [`rust_decimal`](https://crates.io/crates/rust_decimal) types
60+
+ `with-bigdecimal` - support [`bigdecimal`](https://crates.io/crates/bigdecimal) types
61+
+ `with-uuid` - support [`uuid`](https://crates.io/crates/uuid) types
62+
+ `postgres-array` - support array types in Postgres (automatically enabled when `sqlx-postgres` feature is turned on)
63+
+ `sea-orm-internal` - opt-in unstable internal APIs (for accessing re-export SQLx types)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Database Connection
2+
3+
To obtain a database connection, use the [`Database`](https://docs.rs/sea-orm/*/sea_orm/struct.Database.html) interface:
4+
5+
```rust
6+
let db: DatabaseConnection = Database::connect("protocol://username:password@host/database").await?;
7+
```
8+
9+
`protocol` can be `mysql:`, `postgres:` or `sqlite:`.
10+
11+
`host` is usually `localhost`, a domain name or an IP address.
12+
13+
:::tip
14+
15+
If you can't get `localhost` to work, try putting in an IP address and port number, e.g. `127.0.0.1:3306` or even `192.168.x.x`.
16+
17+
:::
18+
19+
Under the hood, a [`sqlx::Pool`](https://docs.rs/sqlx/0.5/sqlx/struct.Pool.html) is created and owned by [`DatabaseConnection`](https://docs.rs/sea-orm/*/sea_orm/enum.DatabaseConnection.html).
20+
21+
Each time you call `execute` or `query_one/all` on it, a connection will be acquired and released from the pool.
22+
23+
Multiple queries will execute in parallel as you `await` on them.
24+
25+
## Connection String
26+
27+
Here are some tips for database specific options:
28+
29+
### MySQL
30+
31+
Can't think of any
32+
33+
### Postgres
34+
35+
#### Specify a schema
36+
37+
```
38+
postgres://username:password@host/database?currentSchema=my_schema
39+
```
40+
41+
### SQLite
42+
43+
#### In memory
44+
45+
```
46+
sqlite::memory:
47+
```
48+
49+
#### Create file if not exists
50+
51+
```
52+
sqlite://path/to/db.sqlite?mode=rwc
53+
```
54+
55+
#### Read only
56+
57+
```
58+
sqlite://path/to/db.sqlite?mode=ro
59+
```
60+
61+
## Connect Options
62+
63+
To configure the connection, use the [`ConnectOptions`](https://docs.rs/sea-orm/*/sea_orm/struct.ConnectOptions.html) interface:
64+
65+
```rust
66+
let mut opt = ConnectOptions::new("protocol://username:password@host/database");
67+
opt.max_connections(100)
68+
.min_connections(5)
69+
.connect_timeout(Duration::from_secs(8))
70+
.acquire_timeout(Duration::from_secs(8))
71+
.idle_timeout(Duration::from_secs(8))
72+
.max_lifetime(Duration::from_secs(8))
73+
.sqlx_logging(true)
74+
.sqlx_logging_level(log::LevelFilter::Info)
75+
.set_schema_search_path("my_schema"); // Setting default PostgreSQL schema
76+
77+
let db = Database::connect(opt).await?;
78+
```
79+
80+
## Checking Connection is Valid
81+
82+
Checks if a connection to the database is still valid.
83+
84+
```rust
85+
|db: DatabaseConnection| {
86+
assert!(db.ping().await.is_ok());
87+
db.clone().close().await;
88+
assert!(matches!(db.ping().await, Err(DbErr::ConnectionAcquire)));
89+
}
90+
```
91+
92+
## Closing Connection
93+
94+
The connection will be automatically closed on drop. To close the connection explicitly, call the `close` method.
95+
96+
```rust
97+
let db = Database::connect(url).await?;
98+
99+
// Closing connection here
100+
db.close().await?;
101+
```

0 commit comments

Comments
 (0)