Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- add new experimental filters (behind the `advanced-queries` feature flag)
- move function `paginate` behind `advanced-queries` feature flag
- split `GenerationConfig` into required and optional parts (`GenerationConfigOpts`) (fixes #92)
- added compile tests to actually verify all options compiling

## 0.0.17 (yanked)

Expand Down
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ diesel::table! {
We run:

```sh
cargo dsync -i schema.rs -o models
dsync -i schema.rs -o models
```

Now we have everything we need!
Expand All @@ -45,9 +45,9 @@ async fn demo(db: Connection) {
}
```

For a complete example, see [`test/simple_table/schema.rs`](test/simple_table/schema.rs) which generates all the code in [`test/simple_schema/models`](test/simple_table/models).
For a complete example, see [`test/simple_table_sqlite/schema.rs`](test/simple_table_sqlite/schema.rs) which generates all the code in [`test/simple_schema_sqlite/models`](test/simple_table_sqlite/models).

## Usage
## Usage as a library

1. Add this crate:

Expand All @@ -67,7 +67,10 @@ For a complete example, see [`test/simple_table/schema.rs`](test/simple_table/sc
dsync::generate_files(
PathBuf::from_iter([dir, "src/schema.rs"]),
PathBuf::from_iter([dir, "src/models"]),
GenerationConfig { /* ... your generation options ... */ }
GenerationConfig {
connection_type: "diesel::sqlite::SqliteConnection",
options: Default::default(),
}
);
}
```
Expand Down Expand Up @@ -104,11 +107,11 @@ cargo install dsync

**CLI Usage**

* `-i`: input argument: path to schema file
* `-o`: output argument: path to directory where generated code should be written
* `-c`: connection type (for example: `diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::pg::PgConnection>>`)
* `-i`: path to the diesel schema file
* `-o`: model output directory
* `-c`: connection type (for example: `diesel::sqlite::SqliteConnection`)
* `-g`: (optional, repeatable) list of columns that are automatically generated by create/update triggers (for example, `created_at`, `updated_at`)
* `--tsync`: (optional) adds `#[tsync]` attribute to generated structs (see <https://github.com/Wulf/tsync>)
* `--tsync`: (optional) adds `#[tsync]` attribute to generated structs for the [`tsync` crate](https://github.com/Wulf/tsync)
* `--model-path`: (optional) set a custom model import path, default `crate::models::`
* `--schema-path`: (optional) set a custom schema import path, default `crate::schema::`
* `--no-serde`: (optional) if set, does not output any serde related code
Expand All @@ -119,15 +122,15 @@ cargo install dsync
* `--readonly-prefix`: (optional, repeatable) A prefix to treat a table matching this as readonly *2
* `--readonly-suffix`: (optional, repeatable) A suffix to treat a table matching this as readonly *2
* `--diesel-backend`: (when the "advanced-queries" feature is enabled) The diesel backend in use (possible values include `diesel::pg::Pg`, `diesel::sqlite::Sqlite`, `diesel::mysql::Mysql`, or your custom backend type)
* note: the CLI has fail-safes to prevent accidental file overwriting

```sh
dsync -i src/schema.rs -o src/models
```

Notes:

- *2: "readonly" tables dont have `Update*` & `Create*` structs, only `*`(no suffix / prefix) structs.
- the CLI has fail-safes to prevent accidental file overwriting
- *2: "readonly" tables dont have `Update*`(`UpdateTodos`) & `Create*`(`CreateTodos`) structs, only `*`(`Todos`, no suffix / prefix) structs.
For example this is useful for Sqlite views, which are read-only (cannot be written to, but can be read)

## Experimental API
Expand All @@ -140,7 +143,9 @@ Feel free to open an issue to discuss these API and provide your feeedback.

## Docs

See `dsync --help` for more information.
See `dsync --help` for all CLI arguments and documentation.

See [docs.rs](https://docs.rs/dsync/latest/dsync/) for library documentation.

Feel free to open tickets for support or feature requests.

Expand Down
36 changes: 27 additions & 9 deletions src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,21 +482,39 @@ impl<'a> GenerationConfig<'a> {
}
}

#[cfg(feature = "advanced-queries")]
pub fn validate_config(config: &GenerationConfig) -> crate::Result<()> {
use crate::error::{Error, ErrorEnum};

const VALID_BACKENDS: [&str; 3] = [
"diesel::pg::Pg",
"diesel::sqlite::Sqlite",
"diesel::mysql::Mysql",
// NOTE: the following arrays should likely be joined at compile-time instead of at runtime, but rust does not provide such a thing in std

#[cfg(feature = "advanced-queries")]
{
const VALID_BACKENDS: [&str; 3] = [
"diesel::pg::Pg",
"diesel::sqlite::Sqlite",
"diesel::mysql::Mysql",
];

if config.diesel_backend.is_empty() {
return Err(Error::new(ErrorEnum::InvalidGenerationConfig(format!(
"Invalid diesel_backend '{}', please use one of the following: {:?}; or, a custom diesel backend type (a struct which implements `diesel::backend::Backend`).",
&config.diesel_backend,
VALID_BACKENDS.join(", ")
))));
}
}

const KNOWN_CONNECTIONS: [&str; 4] = [
"diesel::pg::PgConnection",
"diesel::sqlite::SqliteConnection",
"diesel::mysql::MysqlConnection",
"diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<_>>",
];

if config.diesel_backend.is_empty() {
if config.connection_type.is_empty() {
return Err(Error::new(ErrorEnum::InvalidGenerationConfig(format!(
"Invalid diesel_backend '{}', please use one of the following: {:?}; or, a custom diesel backend type (a struct which implements `diesel::backend::Backend`).",
&config.diesel_backend,
VALID_BACKENDS.join(", ")
"option \"connection_type\" cannot be empty. Known Connections types are:\n[{}]",
KNOWN_CONNECTIONS.join(", ")
))));
}

Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
//! - `async`: enable support for [diesel_async](https://github.com/weiznich/diesel_async)
//! - `tsync`: enable support for [tsync](https://github.com/Wulf/tsync)
//! - `backtrace`: enable attaching backtraces to dsync errors
//! - `derive-queryablebyname`: enable `diesel::QueryableByName` derives on READ structs
//! - `advanced-queries`: enable experimental pagination and filter functions ([examples](https://github.com/Wulf/dsync/tree/a44afdd08f4447e367aa47ecb91fae88b57f8944/test/advanced_queries))
//!
//! default features: `tsync`, `backtrace`
//! default features: `tsync`, `backtrace`, `derive-queryablebyname`

mod code;
pub mod error;
Expand Down Expand Up @@ -110,7 +112,6 @@ pub fn generate_files(
output_models_dir: &Path,
config: GenerationConfig,
) -> Result<Vec<FileChange>> {
#[cfg(feature = "advanced-queries")]
global::validate_config(&config)?;

let generated = generate_code(
Expand Down