You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+39-11
Original file line number
Diff line number
Diff line change
@@ -50,7 +50,7 @@
50
50
<br />
51
51
52
52
<divalign="center">
53
-
<sub>Built with ❤️ by <ahref="https://launchbadge.com">The LaunchBadge team</a></sub>
53
+
<small>Built with ❤️ by <ahref="https://launchbadge.com">The LaunchBadge team</a></small>
54
54
</div>
55
55
56
56
<br />
@@ -59,20 +59,23 @@ SQLx is an async, pure Rust<sub>†</sub> SQL crate featuring compile-time check
59
59
60
60
-**Truly Asynchronous**. Built from the ground-up using async/await for maximum concurrency.
61
61
62
-
-**Type-safe SQL** (if you want it) without DSLs. Use the `query!()` macro to check your SQL and bind parameters at
63
-
compile time. (You can still use dynamic SQL queries if you like.)
62
+
-**Compile-time checked queries** (if you want). See [SQLx is not an ORM](#sqlx-is-not-an-orm).
64
63
65
64
-**Database Agnostic**. Support for [PostgreSQL], [MySQL], [SQLite], and [MSSQL].
66
65
67
66
-**Pure Rust**. The Postgres and MySQL/MariaDB drivers are written in pure Rust using **zero** unsafe<sub>††</sub> code.
68
67
69
-
***Runtime Agnostic**. Works on different runtimes ([async-std](https://crates.io/crates/async-std) / [tokio](https://crates.io/crates/tokio) / [actix](https://crates.io/crates/actix-rt)) and TLS backends ([native-tls](https://crates.io/crates/native-tls), [rustls](https://crates.io/crates/rustls)).
68
+
-**Runtime Agnostic**. Works on different runtimes ([`async-std`] / [`tokio`] / [`actix`]) and TLS backends ([`native-tls`], [`rustls`]).
70
69
71
-
<sub><sup>† The SQLite driver uses the libsqlite3 C library as SQLite is an embedded database (the only way
72
-
we could be pure Rust for SQLite is by porting _all_ of SQLite to Rust).</sup></sub>
70
+
<small><small>
73
71
74
-
<sub><sup>†† SQLx uses `#![forbid(unsafe_code)]` unless the `sqlite` feature is enabled. As the SQLite driver interacts
75
-
with C, those interactions are `unsafe`.</sup></sub>
72
+
† The SQLite driver uses the libsqlite3 C library as SQLite is an embedded database (the only way
73
+
we could be pure Rust for SQLite is by porting _all_ of SQLite to Rust).
74
+
75
+
†† SQLx uses `#![forbid(unsafe_code)]` unless the `sqlite` feature is enabled. As the SQLite driver interacts
76
+
with C, those interactions are `unsafe`.
77
+
78
+
</small></small>
76
79
77
80
[postgresql]: http://postgresql.org/
78
81
[sqlite]: https://sqlite.org/
@@ -108,6 +111,8 @@ SQLx is compatible with the [`async-std`], [`tokio`] and [`actix`] runtimes; and
@@ -118,7 +123,7 @@ sqlx = { version = "0.5", features = [ "runtime-tokio-rustls" ] }
118
123
sqlx = { version = "0.5", features = [ "runtime-async-std-native-tls" ] }
119
124
```
120
125
121
-
<sub><sup>The runtime and TLS backend not being separate feature sets to select is a workaround for a [Cargo issue](https://github.com/rust-lang/cargo/issues/3494).</sup></sub>
126
+
<small><small>The runtime and TLS backend not being separate feature sets to select is a workaround for a [Cargo issue](https://github.com/rust-lang/cargo/issues/3494).</small></small>
122
127
123
128
#### Cargo Feature Flags
124
129
@@ -168,6 +173,24 @@ sqlx = { version = "0.5", features = [ "runtime-async-std-native-tls" ] }
168
173
169
174
-`tls`: Add support for TLS connections.
170
175
176
+
## SQLx is not an ORM!
177
+
178
+
SQLx supports **compile-time checked queries**. It does not, however, do this by providing a Rust
179
+
API or DSL (domain-specific language) for building queries. Instead, it provides macros that take
180
+
regular SQL as an input and ensure that it is valid for your database. The way this works is that
181
+
SQLx connects to your development DB at compile time to have the database itself verify (and return
182
+
some info on) your SQL queries. This has some potentially surprising implications:
183
+
184
+
- Since SQLx never has to parse the SQL string itself, any syntax that the development DB accepts
185
+
can be used (including things added by database extensions)
186
+
- Due to the different amount of information databases let you retrieve about queries, the extent of
187
+
SQL verification you get from the query macros depends on the database
188
+
189
+
**If you are looking for an (asynchronous) ORM,** you can check out [`ormx`], which is built on top
190
+
of SQLx.
191
+
192
+
[`ormx`]: https://crates.io/crates/ormx
193
+
171
194
## Usage
172
195
173
196
### Quickstart
@@ -336,8 +359,8 @@ Differences from `query()`:
336
359
```
337
360
338
361
The biggest downside to `query!()` is that the output type cannot be named (due to Rust not
339
-
officially supporting anonymous records).To address that, there is a `query_as!()` macro that is identical
340
-
except that you can name the output type.
362
+
officially supporting anonymous records).To address that, there is a `query_as!()` macro that is
363
+
mostly identical except that you can name the output type.
341
364
342
365
```rust
343
366
// no traits are needed
@@ -359,6 +382,11 @@ WHERE organization = ?
359
382
// countries[0].count
360
383
```
361
384
385
+
To avoid the need of having a development database around to compile the project even when no
386
+
modifications (to the database-accessing parts of the code) are done, you can enable "offline mode"
387
+
to cache the results of the SQL query analysis using the `sqlx` command-line tool. See
Copy file name to clipboardexpand all lines: sqlx-cli/README.md
+12-5
Original file line number
Diff line number
Diff line change
@@ -49,28 +49,32 @@ $ sqlx migrate run
49
49
Compares the migration history of the running database against the `migrations/` folder and runs
50
50
any scripts that are still pending.
51
51
52
-
#### Enable building in "offline" mode with `query!()`
52
+
#### Enable building in "offline mode" with `query!()`
53
53
54
54
Note: must be run as `cargo sqlx`.
55
55
56
56
```bash
57
57
cargo sqlx prepare
58
58
```
59
-
Saves query data to `sqlx-data.json` in the current directory; check this file into version control
60
-
and an active database connection will no longer be needed to build your project.
61
59
62
-
Has no effect unless the `offline` feature of `sqlx` is enabled in your project. Omitting that feature is the most likely cause if you get a `sqlx-data.json` file that looks like this:
60
+
Saves query metadata to `sqlx-data.json` in the current directory; check this file into version
61
+
control and an active database connection will no longer be needed to build your project.
62
+
63
+
Has no effect unless the `offline` feature of `sqlx` is enabled in your project. Omitting that
64
+
feature is the most likely cause if you get a `sqlx-data.json` file that looks like this:
63
65
64
66
```json
65
67
{
66
68
"database": "PostgreSQL"
67
69
}
68
70
```
69
71
70
-
----
72
+
---
73
+
71
74
```bash
72
75
cargo sqlx prepare --check
73
76
```
77
+
74
78
Exits with a nonzero exit status if the data in `sqlx-data.json` is out of date with the current
75
79
database schema and queries in the project. Intended for use in Continuous Integration.
76
80
@@ -79,3 +83,6 @@ database schema and queries in the project. Intended for use in Continuous Integ
79
83
To make sure an accidentally-present `DATABASE_URL` environment variable or `.env` file does not
80
84
result in `cargo build` (trying to) access the database, you can set the `SQLX_OFFLINE` environment
81
85
variable to `true`.
86
+
87
+
If you want to make this the default, just add it to your `.env` file. `cargo sqlx prepare` will
88
+
still do the right thing and connect to the database.
0 commit comments