|
| 1 | +# sqlx-gen |
| 2 | + |
| 3 | +Generate Rust structs from your database schema — with correct types, derives, and `sqlx::FromRow` annotations. |
| 4 | + |
| 5 | +Supports **PostgreSQL**, **MySQL**, and **SQLite**. Introspects tables, enums, composite types, and domains. |
| 6 | + |
| 7 | +[](https://crates.io/crates/sqlx-gen) |
| 8 | +[](https://docs.rs/sqlx-gen) |
| 9 | +[](LICENSE) |
| 10 | + |
| 11 | +## Features |
| 12 | + |
| 13 | +- Multi-database: PostgreSQL, MySQL, SQLite |
| 14 | +- Multi-schema support (PostgreSQL) |
| 15 | +- Generates `#[derive(sqlx::FromRow)]` structs |
| 16 | +- PostgreSQL enums → `#[derive(sqlx::Type)]` enums |
| 17 | +- PostgreSQL composite types and domains |
| 18 | +- MySQL inline ENUM detection |
| 19 | +- Correct nullable handling (`Option<T>`) |
| 20 | +- Custom derives (`--derives Serialize,Deserialize`) |
| 21 | +- Type overrides (`--type-overrides jsonb=MyType`) |
| 22 | +- Table filtering (`--tables users,orders`) |
| 23 | +- Single-file or multi-file output |
| 24 | +- Dry-run mode (preview on stdout) |
| 25 | + |
| 26 | +## Installation |
| 27 | + |
| 28 | +```sh |
| 29 | +cargo install sqlx-gen |
| 30 | +``` |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +### PostgreSQL (multi-schema) |
| 35 | +```sh |
| 36 | +sqlx-gen -u postgres://user:pass@localhost/mydb -s public,auth -o src/models |
| 37 | +``` |
| 38 | + |
| 39 | +### MySQL |
| 40 | +```sh |
| 41 | +sqlx-gen -u mysql://user:pass@localhost/mydb -o src/models |
| 42 | +``` |
| 43 | + |
| 44 | +### SQLite |
| 45 | +```sh |
| 46 | +sqlx-gen -u sqlite:./local.db -o src/models |
| 47 | +``` |
| 48 | + |
| 49 | +### With extra derives |
| 50 | +```sh |
| 51 | +sqlx-gen -u postgres://... --derives Serialize,Deserialize -o src/models |
| 52 | +``` |
| 53 | + |
| 54 | +### Dry run (preview without writing) |
| 55 | +```sh |
| 56 | +sqlx-gen -u postgres://... --dry-run |
| 57 | +``` |
| 58 | + |
| 59 | +## CLI Options |
| 60 | + |
| 61 | +| Flag | Short | Description | Default | |
| 62 | +|------|-------|-------------|---------| |
| 63 | +| `--database-url` | `-u` | Database connection URL (or `DATABASE_URL` env) | required | |
| 64 | +| `--output-dir` | `-o` | Output directory | `src/models` | |
| 65 | +| `--schemas` | `-s` | Schemas to introspect (comma-separated) | `public` | |
| 66 | +| `--derives` | | Additional derive macros (comma-separated) | none | |
| 67 | +| `--type-overrides` | | Type overrides `sql_type=RustType` (comma-separated) | none | |
| 68 | +| `--tables` | | Only generate these tables (comma-separated) | all | |
| 69 | +| `--single-file` | | Write everything to a single `models.rs` | false | |
| 70 | +| `--dry-run` | | Print to stdout, don't write files | false | |
| 71 | + |
| 72 | +## Example Output |
| 73 | + |
| 74 | +```rust |
| 75 | +// Auto-generated by sqlx-gen. Do not edit. |
| 76 | +// Table: public.users |
| 77 | + |
| 78 | +use chrono::{DateTime, Utc}; |
| 79 | +use uuid::Uuid; |
| 80 | + |
| 81 | +#[derive(Debug, Clone, sqlx::FromRow)] |
| 82 | +pub struct Users { |
| 83 | + pub id: Uuid, |
| 84 | + pub email: String, |
| 85 | + pub name: Option<String>, |
| 86 | + pub created_at: DateTime<Utc>, |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +Enums: |
| 91 | +```rust |
| 92 | +#[derive(Debug, Clone, PartialEq, sqlx::Type)] |
| 93 | +#[sqlx(type_name = "status")] |
| 94 | +pub enum Status { |
| 95 | + #[sqlx(rename = "active")] |
| 96 | + Active, |
| 97 | + |
| 98 | + #[sqlx(rename = "inactive")] |
| 99 | + Inactive, |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +## License |
| 104 | + |
| 105 | +MIT |
0 commit comments