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
|`--type-overrides`|`-T`| Type overrides `sql_type=RustType` (comma-separated) | none |
93
+
|`--single-file`|`-S`| Write everything to a single `models.rs`|`false`|
94
+
|`--tables`|`-t`| Only generate these tables (comma-separated) | all |
95
+
|`--exclude-tables`|`-x`| Exclude these tables/views (comma-separated) | none |
96
+
|`--views`|`-v`| Also generate structs for SQL views |`false`|
97
+
|`--dry-run`|`-n`| Print to stdout, don't write files |`false`|
98
+
99
+
## Generate CRUD
100
+
101
+
Generate a repository from an already-generated entity file. No database connection is required — the generator reads the Rust source file directly.
102
+
103
+
You must specify which methods to generate with `--methods` (`-m`):
104
+
105
+
```sh
106
+
# Generate all CRUD methods
107
+
sqlx-gen generate crud \
108
+
-f src/models/users.rs \
109
+
-d postgres \
110
+
-m '*' \
111
+
-o src/repositories
112
+
113
+
# Generate only specific methods
114
+
sqlx-gen generate crud \
115
+
-f src/models/users.rs \
116
+
-d postgres \
117
+
-m get_all,get,insert
118
+
119
+
# With explicit module path (auto-detected by default)
120
+
sqlx-gen generate crud \
121
+
-f src/models/users.rs \
122
+
-d postgres \
123
+
-e crate::models::users \
124
+
-m '*'
125
+
126
+
# With compile-time checked macros
127
+
sqlx-gen generate crud \
128
+
-f src/models/users.rs \
129
+
-d postgres \
130
+
-m '*' \
131
+
-q
132
+
```
133
+
134
+
### Module path auto-detection
135
+
136
+
The `--entities-module` (`-e`) option is **optional**. When omitted, the module path is automatically derived from the `--entity-file` path by locating `src/` and converting to a Rust module path:
Views are automatically detected via the `#[sqlx_gen(kind = "view")]` annotation — write methods (`insert`, `update`, `delete`) are never generated for views even if requested.
148
+
149
+
### Compile-time checked macros
150
+
151
+
By default, the CRUD generator uses `sqlx::query_as::<_, T>()` with `.bind()` chains (runtime). Pass `--query-macro` (`-q`) to generate `sqlx::query_as!()` / `sqlx::query!()` macros instead, which are checked at compile time (requires `DATABASE_URL` at build time).
152
+
153
+
### Available methods
154
+
155
+
| Method | Description |
156
+
|--------|------------|
157
+
|`*`| Generate all methods below |
158
+
|`get_all`|`SELECT *` returning `Vec<T>`|
159
+
|`paginate`|`SELECT *` with `LIMIT` / `OFFSET` returning `Vec<T>`|
160
+
|`get`|`SELECT *` by primary key returning `Option<T>`|
161
+
|`insert`|`INSERT` with a params struct, `RETURNING *`|
162
+
|`update`|`UPDATE` by primary key with a params struct, `RETURNING *`|
163
+
|`delete`|`DELETE` by primary key |
164
+
165
+
### mod.rs management
166
+
167
+
When writing a CRUD file (not in dry-run mode), sqlx-gen automatically updates or creates a `mod.rs` in the output directory with the corresponding `pub mod` declaration.
168
+
169
+
### Formatting
170
+
171
+
Generated files are automatically formatted with `rustfmt`. The Rust edition is detected from the nearest `Cargo.toml` in the output directory's parent chain (defaults to `2021` if not found).
172
+
173
+
### CRUD CLI Options
174
+
175
+
| Flag | Short | Description | Default |
176
+
|------|-------|-------------|---------|
177
+
|`--entity-file`|`-f`| Path to the generated entity `.rs` file | required |
0 commit comments