Skip to content

Commit 03adace

Browse files
committed
Update README + doc comments
1 parent 57f491f commit 03adace

File tree

2 files changed

+157
-1
lines changed

2 files changed

+157
-1
lines changed

README.md

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,103 @@
77

88
## Usage
99

10-
TODO
10+
Generally, you want to instantiate a `Postgrest` struct, optionally switch
11+
schema with `.schema()`, call `.from()` (or `.rpc()` for stored procedures), do
12+
some filtering and stuff, and then call `.execute()`.
13+
14+
Simple example:
15+
16+
```rust
17+
use postgrest::Postgrest;
18+
19+
let client = Postgrest::new("https://your-postgrest-endpoint");
20+
let resp = client
21+
.from("your_table")
22+
.select("*")
23+
.execute()
24+
.await?;
25+
let body = resp
26+
.text()
27+
.await?;
28+
```
29+
30+
Using filters:
31+
32+
```rust
33+
let resp = client
34+
.from("your_table")
35+
.eq("country", "Germany")
36+
.gte("id", "20")
37+
.select("*")
38+
.execute()
39+
.await?;
40+
```
41+
42+
Updating a table:
43+
44+
```rust
45+
let resp = client
46+
.from("your_table")
47+
.eq("username", "soedirgo")
48+
.update("{\"organization\": \"supabase\"}")
49+
.execute()
50+
.await?;
51+
```
52+
53+
Executing stored procedures:
54+
55+
```rust
56+
let resp = client
57+
.rpc("add", "{\"a\": 1, \"b\": 2}")
58+
.execute()
59+
.await?;
60+
```
61+
62+
_Not enough filters_:
63+
64+
```rust
65+
let resp = client
66+
.from("countries")
67+
.eq("name", "New Zealand")
68+
.gt("id", "20")
69+
.lt("id", "20")
70+
.gte("id", "20")
71+
.lte("id", "20")
72+
.like("name", "%United%")
73+
.ilike("name", "%United%")
74+
.is("name", "null")
75+
.in_("name", vec!["China", "France"])
76+
.neq("name", "China")
77+
.fts("phrase", "The Fat Cats", Some("english"))
78+
.plfts("phrase", "The Fat Cats", None)
79+
.phfts("phrase", "The Fat Cats", Some("english"))
80+
.wfts("phrase", "The Fat Cats", None)
81+
.cs("countries", "(10,20)")
82+
.cd("countries", "(10,20)")
83+
.ov("population_range", (100, 500))
84+
.sl("population_range", (100, 500))
85+
.sr("population_range", (100, 500))
86+
.nxl("population_range", (100, 500))
87+
.nxr("population_range", (100, 500))
88+
.adj("population_range", (100, 500))
89+
.select("*")
90+
.execute()
91+
.await?;
92+
```
93+
94+
More examples incoming!
95+
96+
## Limitations
97+
98+
This library doesn't show the full extent of PostgREST, and definitely doesn't
99+
replace the need to learn PostgREST. Some known limitations are:
100+
101+
- Doesn't support `not`, `and`, and `or` in filtering
102+
- Many inputs are unsanitized (multi-column select, insert/update body, etc.)
103+
- Counting (with HEAD verb)
104+
- Resource embedding (embedded filters, etc.)
105+
106+
That said, if there are any features you want in, feel free to create an issue!
11107

12108
## Contributing
13109

src/lib.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,63 @@
1+
//! # postgrest-rs
2+
//!
3+
//! [PostgREST](https://postgrest.org) client-side library.
4+
//!
5+
//! This library brings an ORM-like interface to PostgREST.
6+
//!
7+
//! ## Usage
8+
//!
9+
//! Simple example:
10+
//! ```rust,no_run
11+
//! use postgrest::Postgrest;
12+
//!
13+
//! let client = Postgrest::new("https://your-postgrest-endpoint");
14+
//! let resp = client
15+
//! .from("your_table")
16+
//! .select("*")
17+
//! .execute()
18+
//! .await?;
19+
//! let body = resp
20+
//! .text()
21+
//! .await();
22+
//! ```
23+
//!
24+
//! Using filters:
25+
//! ```rust,no_run
26+
//! # use postgrest::Postgrest;
27+
//! # let client = Postgrest::new("https://your-postgrest-endpoint");
28+
//! let resp = client
29+
//! .from("your_table")
30+
//! .eq("country", "Germany")
31+
//! .gte("id", "20")
32+
//! .select("*")
33+
//! .execute()
34+
//! .await?;
35+
//! ```
36+
//!
37+
//! Updating a table:
38+
//! ```rust,no_run
39+
//! # use postgrest::Postgrest;
40+
//! # let client = Postgrest::new("https://your-postgrest-endpoint");
41+
//! let resp = client
42+
//! .from("your_table")
43+
//! .eq("username", "soedirgo")
44+
//! .update("{\"organization\": \"supabase\"}")
45+
//! .execute()
46+
//! .await?;
47+
//! ```
48+
//!
49+
//! Executing stored procedures:
50+
//! ```rust,no_run
51+
//! # use postgrest::Postgrest;
52+
//! # let client = Postgrest::new("https://your-postgrest-endpoint");
53+
//! let resp = client
54+
//! .rpc("add", "{\"a\": 1, \"b\": 2}")
55+
//! .execute()
56+
//! .await?;
57+
//! ```
58+
//!
59+
//! Check out the [README](https://github.com/supabase/postgrest-rs) for more examples.
60+
161
extern crate reqwest;
262

363
mod builder;

0 commit comments

Comments
 (0)