Debian Codegraph @

README.md

raw
1 [![diesel logo](https://diesel.rs/assets/images/diesel_logo_stacked_black.png)](https://diesel.rs)
2
3 # A safe, extensible ORM and Query Builder for Rust
4
5 [![Build Status](https://github.com/diesel-rs/diesel/workflows/CI%20Tests/badge.svg)](https://github.com/diesel-rs/diesel/actions?query=workflow%3A%22CI+Tests%22+branch%3Amain)
6 [![Crates.io](https://img.shields.io/crates/v/diesel.svg)](https://crates.io/crates/diesel)
7
8 API Documentation: [latest release](https://docs.rs/diesel) – [main branch](https://docs.diesel.rs/main/diesel/index.html)
9
10 [Homepage](https://diesel.rs)
11
12 Diesel gets rid of the boilerplate for database interaction and eliminates
13 runtime errors without sacrificing performance. It takes full advantage of
14 Rust's type system to create a low overhead query builder that "feels like
15 Rust."
16
17 Supported databases:
18 1. [PostgreSQL](https://docs.diesel.rs/main/diesel/pg/index.html)
19 2. [MySQL](https://docs.diesel.rs/main/diesel/mysql/index.html)
20 3. [SQLite](https://docs.diesel.rs/main/diesel/sqlite/index.html)
21
22 You can configure the database backend in `Cargo.toml`:
23
24 ```toml
25 [dependencies]
26 diesel = { version = "<version>", features = ["<postgres|mysql|sqlite>"] }
27 ```
28
29 ## Getting Started
30
31 Find our extensive Getting Started tutorial at
32 [https://diesel.rs/guides/getting-started](https://diesel.rs/guides/getting-started).
33 Guides on more specific features are coming soon.
34
35 ## Getting help
36
37 If you run into problems, you can come ask for help at in our [GitHub Discussions](https://github.com/diesel-rs/diesel/discussions) forum.
38 This is also the right place to propose new features or show your applications.
39
40 ## Usage
41
42 ### Simple queries
43
44 Simple queries are a complete breeze. Loading all users from a database:
45
46 ```rust
47 users::table.load(&mut connection)
48 ```
49
50 Executed SQL:
51
52 ```sql
53 SELECT * FROM users;
54 ```
55
56 Loading all the posts for a user:
57
58 ``` rust
59 Post::belonging_to(user).load(&mut connection)
60 ```
61
62 Executed SQL:
63
64 ```sql
65 SELECT * FROM posts WHERE user_id = 1;
66 ```
67
68 ### Complex queries
69
70 Diesel's powerful query builder helps you construct queries as simple or complex as
71 you need, at zero cost.
72
73 ```rust
74 let versions = Version::belonging_to(krate)
75 .select(id)
76 .order(num.desc())
77 .limit(5);
78 let downloads = version_downloads
79 .filter(date.gt(now - 90.days()))
80 .filter(version_id.eq_any(versions))
81 .order(date)
82 .load::<Download>(&mut conn)?;
83 ```
84
85 Executed SQL:
86
87 ```sql
88 SELECT version_downloads.* FROM version_downloads
89 WHERE date > (NOW() - '90 days')
90 AND version_id = ANY(
91 SELECT id FROM versions
92 WHERE crate_id = 1
93 ORDER BY num DESC
94 LIMIT 5
95 )
96 ORDER BY date
97 ```
98
99 ### Less boilerplate
100
101 Diesel codegen generates boilerplate for you. It lets you focus on your business logic, not mapping to and from SQL rows.
102
103 That means you can write this:
104
105 ```rust
106 #[derive(Queryable, Selectable)]
107 #[diesel(table_name = downloads)]
108 pub struct Download {
109 id: i32,
110 version_id: i32,
111 downloads: i32,
112 counted: i32,
113 date: SystemTime,
114 }
115 ```
116
117 Instead of this without Diesel:
118
119 ```rust
120 pub struct Download {
121 id: i32,
122 version_id: i32,
123 downloads: i32,
124 counted: i32,
125 date: SystemTime,
126 }
127
128 impl Download {
129 fn from_row(row: &Row) -> Download {
130 Download {
131 id: row.get("id"),
132 version_id: row.get("version_id"),
133 downloads: row.get("downloads"),
134 counted: row.get("counted"),
135 date: row.get("date"),
136 }
137 }
138 }
139 ```
140
141 ### Inserting data
142
143 It's not just about reading data. Diesel makes it easy to use structs for new records.
144
145 ```rust
146 #[derive(Insertable)]
147 #[diesel(table_name = users)]
148 struct NewUser<'a> {
149 name: &'a str,
150 hair_color: Option<&'a str>,
151 }
152
153 let new_users = vec![
154 NewUser { name: "Sean", hair_color: Some("Black") },
155 NewUser { name: "Gordon", hair_color: None },
156 ];
157
158 insert_into(users)
159 .values(&new_users)
160 .execute(&mut connection);
161 ```
162
163 Executed SQL:
164
165 ```sql
166 INSERT INTO users (name, hair_color) VALUES
167 ('Sean', 'Black'),
168 ('Gordon', DEFAULT)
169 ```
170
171 If you need data from the rows you inserted, just change `execute` to `get_result` or `get_results`. Diesel will take care of the rest.
172
173 ```rust
174 let new_users = vec![
175 NewUser { name: "Sean", hair_color: Some("Black") },
176 NewUser { name: "Gordon", hair_color: None },
177 ];
178
179 let inserted_users = insert_into(users)
180 .values(&new_users)
181 .get_results::<User>(&mut connection);
182 ```
183
184 Executed SQL:
185
186 ```sql
187 INSERT INTO users (name, hair_color) VALUES
188 ('Sean', 'Black'),
189 ('Gordon', DEFAULT)
190 RETURNING *
191 ```
192
193 ### Updating data
194
195 Diesel's codegen can generate several ways to update a row, letting you encapsulate your logic in the way that makes sense for your app.
196
197 Modifying a struct:
198
199 ```rust
200 post.published = true;
201 post.save_changes(&mut connection);
202 ```
203
204 One-off batch changes:
205
206 ```rust
207 update(users.filter(email.like("%@spammer.com")))
208 .set(banned.eq(true))
209 .execute(&mut connection)
210 ```
211
212 Using a struct for encapsulation:
213
214 ```rust
215 update(Settings::belonging_to(current_user))
216 .set(&settings_form)
217 .execute(&mut connection)
218 ```
219
220 ### Raw SQL
221
222 There will always be certain queries that are just easier to write as raw SQL, or can't be expressed with the query builder. Even in these cases, Diesel provides an easy to use API for writing raw SQL.
223
224 ```rust
225 #[derive(QueryableByName)]
226 #[diesel(table_name = users)]
227 struct User {
228 id: i32,
229 name: String,
230 organization_id: i32,
231 }
232
233 // Using `include_str!` allows us to keep the SQL in a
234 // separate file, where our editor can give us SQL specific
235 // syntax highlighting.
236 sql_query(include_str!("complex_users_by_organization.sql"))
237 .bind::<Integer, _>(organization_id)
238 .bind::<BigInt, _>(offset)
239 .bind::<BigInt, _>(limit)
240 .load::<User>(&mut conn)?;
241 ```
242
243 ## Code of conduct
244
245 Anyone who interacts with Diesel in any space, including but not limited to
246 this GitHub repository, must follow our [code of conduct](https://github.com/diesel-rs/diesel/blob/main/code_of_conduct.md).
247
248 ## License
249
250 Licensed under either of these:
251
252 * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
253 https://www.apache.org/licenses/LICENSE-2.0)
254 * MIT license ([LICENSE-MIT](LICENSE-MIT) or
255 https://opensource.org/licenses/MIT)
256
257 ### Contributing
258
259 Before contributing, please read the [contributors guide](https://github.com/diesel-rs/diesel/blob/main/CONTRIBUTING.md)
260 for useful information about setting up Diesel locally, coding style and common abbreviations.
261
262 Unless you explicitly state otherwise, any contribution you intentionally submit
263 for inclusion in the work, as defined in the Apache-2.0 license, shall be
264 dual-licensed as above, without any additional terms or conditions.
265
266 ### Notable Sponsors and Supporters
267
268 We would like to thank all of the sponsors supporting the work on Diesel. Notable large sponsors are:
269
270
271 <table>
272 <tr>
273 <td align="center" width="33%">
274 <a href="https://nlnet.nl/project/Diesel/">
275 <img src="https://diesel.rs/assets/images/nl_net_foundation_logo.svg" />
276 <br/>
277 <sub><b>NLNet Foundation</b></sub>
278 </a>
279 </td>
280 <td align="center" width="33%">
281 <a href="https://www.prototypefund.de/projects/diesel-databaseviews">
282 <img src="https://diesel.rs/assets/images/PrototypeFund_logo_dark.png" />
283 <br/>
284 <sub><b>Prototype Fund</b></sub>
285 </a>
286 </td>
287 <td align="center" width="33%">
288 <a href="https://github.blog/open-source/maintainers/securing-the-supply-chain-at-scale-starting-with-71-important-open-source-projects/">
289 <img src="https://diesel.rs/assets/images/GitHub_Logo.png" />
290 <br/>
291 <sub><b>GitHub Secure Open Source Fund</b></sub>
292 </a>
293 </td>
294 </tr>
295 <tr>
296 <td align="center" width="33%">
297 <a href="https://nlnet.nl/project/Diesel/">
298 <img src="https://diesel.rs/assets/images/NGI0Core_tag.svg" />
299 <br/>
300 <sub><b>NGI Zero Core</b></sub>
301 </a>
302 </td>
303 <td align="center" width="33%">
304 <a href="https://www.prototypefund.de/projects/diesel-databaseviews">
305 <img src="https://diesel.rs/assets/images/bmbf_logo.jpg" />
306 <br/>
307 <sub><b>Federal Ministry of Research, Technology and Space (Germany)</b></sub>
308 </a>
309 </td>
310 <td align="center" width="33%">
311 <a href="https://giga-infosystems.com/">
312 <img src="https://diesel.rs/assets/images/logo_giga.svg" />
313 <br/>
314 <sub><b>GiGa Infosystems GmbH</b></sub>
315 </a>
316 </td>
317 </tr>
318 <tr>
319 <td align="center" width="33%">
320 <a href="https://bytebase.com/?utm_source=diesel">
321 <img src="https://diesel.rs/assets/images/logo-bytebase.png" />
322 <br/>
323 <sub><b>Bytebase - Open-source Database DevSecOps</b></sub>
324 </a>
325 </td>
326 </tr>
327 </table>
328
329 Additionally we would like to thank all persons sponsoring the project on [GitHub](https://github.com/sponsors/weiznich#sponsors). Without them developing Diesel wouldn't be possible.