Skip to content

Commit 754ddbd

Browse files
committed
Added limit/offset section, fixed various issues in CRUD
1 parent 33e3327 commit 754ddbd

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

Diff for: docs/rorm/crud.md

+33-11
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ If you're not comfortable with rust's async streams, you can always start using
9090
The optional `.condition(...)` method can be invoked to add a condition the returned rows must satisfy.
9191

9292
!!! note
93-
This direclty corresponds to adding a `WHERE` clause in sql
93+
This directly corresponds to adding a `WHERE` clause in sql
9494

9595
To construct conditions use a comparison method on the field syntax
9696
```
9797
User::F.username.equals("bob")
98-
^^^^^^^^^^ ^^^^^ - Value to compare against
99-
| ^^^^^^
100-
| |
101-
| Comparison operator
98+
^^^^^^^^^^^^^^^^ ^^^^^ - Value to compare against
99+
| ^^^^^^
100+
| |
101+
| Comparison operator
102102
|
103103
Field to compare
104104
```
@@ -125,7 +125,7 @@ In its most basic usage (`query!(db, User)`) the query will select every column
125125
This can be customized by changing the macro's second argument.
126126

127127
!!! note
128-
The `query!` macro is somewhat unique with regards to its second argument.
128+
The `query!` macro is somewhat unique in regard to its second argument.
129129

130130
The other macros won't behave comparibly.
131131

@@ -160,16 +160,38 @@ let posts: Vec<(String, String)> =
160160
```
161161

162162
When you want to select your relations' fields and there are a lot of them, specifying them all like this can get quite verbose.
163-
On top of that due to rust limitations regarding tuples the is a maximum length of 32 on how many fields you can query in one go.
164-
To mitigate this there is a in between syntax combining individual fields with patches:
163+
On top of that, due to rust limitations regarding tuples, the maximum number of fields you can query in one go is 32.
164+
To mitigate this, there is a syntax combining individual fields with patches:
165165

166166
```rust
167167
let posts: Vec<(String, UserWithoutPassword)> =
168168
query!(db, (Post::F.message, Post::F.user as UserWithoutPassword)).all().await?;
169169
```
170170

171-
### TODO
172-
`limit`, `offset`, `range`, `order_...`
171+
#### Limit & offset
172+
173+
Just as you would expect them, the `.limit(u64)` and `.offset(u64)` functions can be used to add limits and offsets
174+
to the SQL query. Note that `.limit()` can not be combined with `.one()`, since the latter already adds a limit of 1.
175+
Also, the `.offset()` can not be used without a limit. The following examples illustrate possible uses:
176+
177+
```rust
178+
query!(db, Post).limit(10).all().await?;
179+
query!(db, Post).offset(13).one().await?;
180+
query!(db, Post).limit(10).offset(42).all().await?;
181+
query!(db, Post).limit(100).offset(1337).stream();
182+
```
183+
184+
There is also the `.range()` function which provides a convenient way to add both the limit and offset.
185+
Mixing a range with the previous functions for limit and offset is not allowed.
186+
Thus, the following example will return at most 10 elements since it corresponds to limit 10 and offset 30:
187+
188+
```rust
189+
query!(db, Post).range(30..40).all().await?;
190+
```
191+
192+
#### Ordering
193+
194+
TODO: `order_...`
173195

174196
## Insert
175197

@@ -347,7 +369,7 @@ async fn update_user(
347369

348370
## Delete
349371

350-
In order to create new rows in the database the `delete!` macro is used:
372+
In order to delete rows from a table, the `delete!` macro is used:
351373

352374
```rust
353375
pub async fn delete_single_user(db: &Database, user: &UserPatch) -> Result<(), rorm::Error> {

0 commit comments

Comments
 (0)